Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel VBA SQL来自Access无结果_Sql_Excel_Vba_Ms Access - Fatal编程技术网

Excel VBA SQL来自Access无结果

Excel VBA SQL来自Access无结果,sql,excel,vba,ms-access,Sql,Excel,Vba,Ms Access,使用Excel 2010查询Access 2010数据库(通过用户表单) 当我执行代码时,我会收到消息框“No Results”(在子代码末尾附近调用)。但是,当我输入某个搜索字符串时,应该会有12条记录 我认为我的SQL字符串可能不正确,所以我将SQL语句写入Sheet1单元格A2。然后,我打开Access数据库,创建了一个SQL查询,并从单元格A2复制/粘贴了SQL语句——它工作得非常好。-->所以这不是SQL语句 为什么我的代码找不到数据?SQL语句工作正常。当我尝试建立ADODB连接时,

使用Excel 2010查询Access 2010数据库(通过用户表单)

当我执行代码时,我会收到消息框“No Results”(在子代码末尾附近调用)。但是,当我输入某个搜索字符串时,应该会有12条记录

我认为我的SQL字符串可能不正确,所以我将SQL语句写入Sheet1单元格A2。然后,我打开Access数据库,创建了一个SQL查询,并从单元格A2复制/粘贴了SQL语句——它工作得非常好。-->所以这不是SQL语句

为什么我的代码找不到数据?SQL语句工作正常。当我尝试建立ADODB连接时,没有收到任何错误

编辑:我在另一个子系统中使用了完全相同的数据库连接设置,效果很好

Private子searchAllInJobs(searchStr作为字符串)
Dim con作为对象,rs作为对象,accessFile作为字符串,strTable作为字符串,sql作为字符串,keyStr作为字符串,i作为整数

accessFile=“****************”我想我知道为什么会发生这种情况。Access中的通配符是
*
,但对于大多数其他SQL变体,它是
%
。您在这里使用的是ADO。看

请尝试以下方法:


keyStr=“%”&searchStr&“%”不确定是否需要额外的“%”

我想我知道为什么会发生这种情况。Access中的通配符是
*
,但对于大多数其他SQL变体,它是
%
。您在这里使用ADO。请参阅

请尝试以下方法:


keyStr=“%”&searchStr&“%”不确定是否需要额外的“%”

如果无法创建ADODB.connection对象,而不是无法连接到数据库,则会生成错误消息。如果无法创建ADODB.connection对象,则会生成失败的连接。在执行测试之前,请移动
con.Open
语句,查看是否可以打开它,然后如果收到“数据库连接失败”消息,请通知我们。祝您好运。不,没有生成错误。感谢您捕捉到这一点。如果您无法创建ADODB.connection对象,而不是无法连接到数据库,则会生成错误消息re a failed connection。在执行测试之前,请移动
con.Open
语句,查看是否可以打开它,然后如果收到“数据库连接失败”消息,请通知我们。祝您好运。不,没有生成错误。谢谢你抓住了这个机会,你说得对。奇怪的是,我以前用“%”,但在Access 2010中,通配符实际上是星号“*”。我想知道他们为什么如此不同你是对的。奇怪的是,我以前用“%”,但在Access 2010中,通配符实际上是星号“*”。我想知道他们为什么如此不同
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