gpt4 book ai didi

sql - 在 MS-Access VBA 中查询 Excel 工作表(使用 ADODB 记录集)

转载 作者:行者123 更新时间:2023-12-01 22:47:55 25 4
gpt4 key购买 nike

我想在 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

最佳答案

在你的连接字符串中你说

 Excel 8.0;HDR=Yes

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

Excel 8.0;HDR=No

关于sql - 在 MS-Access VBA 中查询 Excel 工作表(使用 ADODB 记录集),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2086234/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com