ADO 连接和记录集 = ""

ADO Connection and Recordset = ""

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

我的SQL技能很弱。

我正在尝试将我的 export.csv 文件中的所有文本放入作品 sheet 中,其中 "Price" 列不为空,并按 "sku" 排序。

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

调试器将字符串显示为:

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

当我尝试打开 Connection 和 Recordset 调试器时,它们都包含“”的值。

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

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

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

我正在使用以下数据调用子例程:

GetData "export.csv", "export", "A:AH", "BirdFeet", "B1", "sku", 真, 真

这是完整的子程序。

    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

示例数据来自 export.csv

sku    price    post_date        post_date_gmt
B1     - M      4/19/2015 11:15   4/19/2015 15:15
B8     8.06     4/19/2015 11:11   4/19/2015 15:11
B1     10.79    4/19/2015 11:08   4/19/2015 15:08
B2     11.65    4/19/2015 11:08   4/19/2015 15:08
B3     11.98    4/19/2015 11:08   4/19/2015 15:08
B3B    12.74    4/19/2015 11:08   4/19/2015 15:08
B4     16.24    4/19/2015 11:08   4/19/2015 15:08
SB     770      4/3/2015 12:37    4/3/2015 16:37

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

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

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

这个类似的问题解释了基本原理。 link

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

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

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

克雷格