【问题标题】:Query Excel worksheet in MS-Access VBA (using ADODB recordset)在 MS-Access VBA 中查询 Excel 工作表(使用 ADODB 记录集)
【发布时间】:2011-01-06 08:56:41
【问题描述】:

我想在 VBA 中查询 Excel 工作表指定条件。

简单的查询"SELECT * FROM [PCR$]" 完美运行,但我不知道如何添加 WHERE 子句。

我试过cmd2.CommandText = "SELECT * FROM [PCR$] WHERE ([B1] IS NOT NULL)",但它抱怨缺少参数。

这是完整的代码:


Dim rs2 As New ADODB.Recordset
Dim cnn2 As New ADODB.Connection
Dim cmd2 As New ADODB.Command
Dim intField As Integer
Dim strFile As String

strFile = fncOpenFile
If strFile = "" Then Exit Sub

With cnn2
    .Provider = "Microsoft.Jet.OLEDB.4.0"
    .ConnectionString = "Data Source='" & strFile & "'; " & "Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'"
    .Open
End With

Set cmd2.ActiveConnection = cnn2
cmd2.CommandType = adCmdText
cmd2.CommandText = "SELECT * FROM [PCR$]"
rs2.CursorLocation = adUseClient
rs2.CursorType = adOpenDynamic
rs2.LockType = adLockOptimistic

rs2.Open cmd2

【问题讨论】:

    标签: sql excel ms-access vba


    【解决方案1】:

    在你的连接字符串中你说

     Excel 8.0;HDR=Yes
    

    这意味着第一行将被视为标题,无论它包含什么。如果你想使用 F1、F2 等,请说

    Excel 8.0;HDR=No
    

    【讨论】:

      【解决方案2】:

      因为您有HDR=Yes 选项,所以列名应该是第一行中的数据。

      http://support.microsoft.com/kb/316934

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-12-12
        • 1970-01-01
        • 2021-07-06
        • 2021-12-10
        • 1970-01-01
        • 2017-09-25
        • 1970-01-01
        相关资源
        最近更新 更多