ADO连接和记录集=""

时间:2015-06-15 19:16:41

标签: sql vba connection ado recordset

CSV(逗号分隔的文本文件)是否有工作表名称? 我是否需要指定要复制的数据范围?

我的SQL技能很弱。

我正在尝试将我的export.csv文件中的所有文本都放到一个工作表中,其中" Price"列不为空,并按" sku"。

对其进行排序

我可以打开文件并构建SQL字符串,但一定有错误吗?

调试器将字符串显示为:

cite

当我尝试打开调试器连接和记录集时,它们都包含""的值。

"SELECT * FROM [export$] WHERE price IS NOT NULL ORDER BY sku;"

当我使用它们时,我会直接进入错误陷阱。

Set rsCon = CreateObject("ADODB.Connection")
Set rsData = CreateObject("ADODB.Recordset")

我用以下数据调用子程序:

GetData" export.csv"," export"," A:AH"," BirdFeet"," B1&# 34;," sku",True,True

这是完整的子程序。

rsCon.Open szConnect
rsData.Open szSQL, rsCon, 0, 1, 1

来自export.csv的示例数据

    Public Sub GetData(SourceFile As Variant, SourceSheet As String, SourceRange As String, _
                                              TargetSheet As String, TargetRange As String, _
                                              TargetSortColumn As String, _
                                              HaveHeader As Boolean, UseHeaderRow As Boolean)

     Dim lColumn As Long
        Dim lCount As Long
        Dim lRow As Long

        Dim rsCon As Object
        Dim rsData As Object

        Dim szConnect As String
        Dim szSQL As String

        lRow = Range(TargetRange).Row
        lColumn = Range(TargetRange).Column

        ' Create the connection string.
        If HaveHeader = False Then                                                          'No there is NOT a header row.
            If Val(Application.Version) < 12 Then
                szConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
                            "Data Source=" & SourceFile & ";" & _
                            "Extended Properties=""Excel 8.0;HDR=No"";"
            Else
                szConnect = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
                            "Data Source=" & SourceFile & ";" & _
                            "Extended Properties=""Excel 12.0;HDR=No"";"
            End If
        Else                                                                                'Yes there is a Header Row
            If Val(Application.Version) < 12 Then
                szConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
                            "Data Source=" & SourceFile & ";" & _
                            "Extended Properties=""Excel 8.0;HDR=Yes"";"
            Else
                    If (Right(SourceFile, 4) = ".csv") Then
                        szConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
                                    "Data Source=" & SourceFile & ";" & _
                                    "Extended Properties=""text; HDR=Yes; FMT=Delimited; IMEX=1;"""
                    Else
                        szConnect = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
                                    "Data Source=" & SourceFile & ";" & _
                                    "Extended Properties=""Excel 12.0;HDR=Yes"";"
                    End If
            End If
        End If

        If SourceSheet = "" Then                                                            'Create query strings

            szSQL = "SELECT * FROM " & SourceRange$ & " ORDER BY sku;"

        ElseIf SourceSheet = "DiamondAvian" Or SourceSheet = "export" Then
            szSQL = "SELECT * FROM [" & SourceSheet$ & "$" & "] WHERE price IS NOT NULL ORDER BY " & TargetSortColumn & ";"   'Drops all rows with no Price

        Else

            szSQL = "SELECT * FROM [" & SourceSheet$ & "$" & SourceRange$ & "] WHERE sku IS NOT NULL ORDER BY " & TargetSortColumn & ";"   'THIS WORKS FOR DICIONARY

        End If

        On Error GoTo SomethingWrong

        Set rsCon = CreateObject("ADODB.Connection")
        Set rsData = CreateObject("ADODB.Recordset")

        rsCon.Open szConnect
        rsData.Open szSQL, rsCon, 0, 1, 1
                                                                                             ' Check to make sure we received data and copy the data
        If Not rsData.EOF Then

            If HaveHeader = False Then
                Cells(1, 1).CopyFromRecordset rsData
            Else
                                                                                             'Add the header cell in each column if the last argument is True
                If UseHeaderRow Then
                    For lCount = 0 To rsData.Fields.Count - 1                                'Builds the Header row one column at a time.
                        Cells(lRow, lColumn + lCount).value = rsData.Fields(lCount).Name     'lcount determines the Column to paste header info in.
                    Next lCount
                Cells(lRow + 1, lColumn).CopyFromRecordset rsData                            'This is the step that copies and Pastes the data.
                Else
                    Cells(lRow + 1, lColumn).CopyFromRecordset rsData
                End If
            End If

        Else
            MsgBox "No records returned from : " & SourceFile, vbCritical
        End If

        rsData.Close                                                                        ' Clean up our Recordset object.
        Set rsData = Nothing
        rsCon.Close
        Set rsCon = Nothing

        Exit Sub

SomethingWrong:

    MsgBox "The file name, Sheet name or Range is invalid of : " & SourceFile, vbExclamation, "Error"

On Error GoTo 0

End Sub

2 个答案:

答案 0 :(得分:1)

SQL不支持Null上的<>操作。替换

"SELECT * FROM [export$] WHERE price <> NULL ORDER BY sku;"

"SELECT * FROM [export$] WHERE price IS NOT NULL ORDER BY sku;"

这个类似的问题解释了理由。 link

答案 1 :(得分:0)

我没有使用szConnect字符串中包含csv文件的目录的路径。我使用的是文件名而不是路径。

我添加了一个字符串&#34; strWorkingDir&#34;并将其设置为等于文件的目录,现在它工作正常。

https://msdn.microsoft.com/en-us/library/ms974559.aspx

CraigM