[英]Excel VBA SQL from Access no results
Using Excel 2010 to query an Access 2010 Database (via UserForms).使用 Excel 2010 查询 Access 2010 数据库(通过用户窗体)。
When I execute the code, I get my Message Box "No Results" (called near the end of the sub).当我执行代码时,我收到我的消息框“无结果”(在子程序末尾附近调用)。 However, there should be 12 records that pull up when I enter in a certain search string.但是,当我输入某个搜索字符串时,应该会出现 12 条记录。
I thought maybe my SQL string was incorrect so I wrote the SQL statement to Sheet1 Cell A2.我想可能是我的 SQL 字符串不正确,所以我将 SQL 语句写入 Sheet1 Cell A2。 I then opened up my Access Database, created a SQL query, and copy/pasted the SQL statement from cell A2 - It worked perfectly.然后我打开了我的 Access 数据库,创建了一个 SQL 查询,并从单元格 A2 中复制/粘贴了 SQL 语句——它工作得很好。 --> So it's not the SQL statement. --> 所以不是SQL语句。
Why is my code not finding the data?为什么我的代码找不到数据? The SQL statement works fine. SQL 语句工作正常。 I'm not getting any errors when I try to establish an ADODB connection.当我尝试建立 ADODB 连接时,我没有收到任何错误。
EDIT : I use the exact same database connection setup in another sub and it works fine.编辑:我在另一个子程序中使用完全相同的数据库连接设置,它工作正常。
Private Sub searchAllInJobs(searchStr As String)
Dim con As Object, rs As Object, accessFile As String, strTable As String, sql As String, keyStr As String, i As Integer
accessFile = "******************" '<--hidden on purpose
Set con = CreateObject("ADODB.connection")
If Err.Number <> 0 Then
MsgBox "Failed database connection", vbCritical, "Connection Error"
Exit Sub
End If
On Error GoTo 0
con.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & accessFile
'Wild card string
keyStr = """*" & searchStr & "*"""
'I have tested this SQL statement in the Access database and works great
sql = "SELECT * FROM tbl_job_tables WHERE ([MODELNO] LIKE " & keyStr & ");"
'Write the SQL to a cell, to make sure the statement was constructed properly
Sheets("Sheet1").Range("A2").value = sql
Set rs = CreateObject("ADODB.Recordset")
'Open the database recordset from the SQL statement
rs.Open sql, con
'Clear the current ListBox
Me.list_SearchJobs.Clear
i = 0
If Not (rs.EOF and rs.BOF) Then
'Move to the first item
rs.MoveFirst
'While going through the records, if you haven't reached the End-of-file then...
Do While Not rs.EOF
With Me.list_SearchJobs
.AddItem
.List(i, 0) = rs!JOB_NUM
.List(i, 1) = rs!customer
.List(i, 2) = rs!MODELNO
.List(i, 3) = rs!CREATE_DATE
End With
i = i + 1
rs.MoveNext
Loop
'Close the recordset and database connection
rs.Close
con.Close
'Set the objects to "Nothing" (clears the cache)
Set rs = Nothing
Set con = Nothing
Else
'Close the recordset and database connection
rs.Close
con.Close
'Set the objects to "Nothing" (clears the cache)
Set rs = Nothing
Set con = Nothing
MsgBox "No Results", vbCritical, "No results"
Exit Sub
End If
End Sub
I think I know why this is happening.我想我知道为什么会这样。 The wildcard in Access is *
, but for most other variants of SQL it is %
. Access 中的通配符是*
,但对于大多数其他 SQL 变体,它是%
。 You are using ADO here.您在这里使用 ADO。 See this看到这个
Try this instead:试试这个:
keyStr = "%" & searchStr & "%" 'Not sure if you need the extra "'s either
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.