如何使用vba从对Excel的访问权限中检索数据?

时间:2019-06-29 10:31:47

标签: excel vba ms-access

我正在尝试使用excel-vba从访问中检索数据,但问题是当我想在SQL查询中使用AND时抛出错误。

这有效

SQL = "SELECT " & col_name & " FROM PhoneList WHERE Item LIKE '" & var & "%" & "'"

这不是

SQL = "SELECT " & col_name & " FROM PhoneList WHERE Item LIKE '" & item & "%" & "'" & " AND Size Like '" & size & "%" & "'"

即使这不起作用

SQL = "SELECT Standard FROM PhoneList WHERE Item = 'Shank Button' AND Size = '17'"

这将引发

  

错误-2147467259方法'open'object'_recordset'失败

在excel中,如果我在访问查询中尝试的话,效果很好

SELECT Standard FROM PhoneList WHERE Item = 'Shank Button' AND Size = '17'

这是我的表格[PhoneList]结构

  

ID |项目|尺寸标准|定制标准优质|定制溢价

在这里,Item和Size的组合使一条记录变得唯一。

我认为只有AND部分会造成问题,或者记录集粘贴到excel工作表时出了点问题。

我试图从数据库中仅检索一个值,其中项目,大小和类别类型(标准或自定义标准或高级)将从excel用户窗体的组合框中获取,我将在单元格或文本字段中显示该值。

我们将不胜感激任何帮助。

这是我的代码...

 Option Explicit
Private Sub CommandButton1_Click()
    'Declaring the necessary variables.
    Dim cnn As ADODB.Connection 'dim the ADO collection class
    Dim rs As ADODB.Recordset 'dim the ADO recordset class
    Dim dbPath As String
    Dim SQL As String
    Dim i As Integer
    Dim item As String

    'add error handling
    On Error GoTo errHandler:

    'Disable screen flickering.
    Application.ScreenUpdating = False
    'Clear the old data
    Sheet2.Range("A2:G10000").ClearContents

    'Variables
    dbPath = Sheet1.Range("I3").Value
    item = ComboBox1.Text

    'Initialise the collection class variable
    Set cnn = New ADODB.Connection

    'Connection class is equipped with a —method— named Open
    '—-4 aguments—- ConnectionString, UserID, Password, Options
    'ConnectionString formula—-Key1=Value1;Key2=Value2;Key_n=Value_n;
    cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & dbPath

    'Create the SQL statement to retrieve the data from table.
    Dim col_name As String
    Dim Isize As String

    Isize = "17"
    col_name = ComboBox3.Text

    If Sheet2.Range("J2").Value = "Yes" Then
        SQL = "SELECT * FROM PhoneList WHERE Item = '" & item & "'"
        Else
        SQL = "SELECT " & col_name & " FROM PhoneList WHERE Item LIKE '" & item & "%" & "'" & " AND Size LIKE '" & Isize & "%" & "'"
        'SQL = "SELECT " & col_name & " FROM PhoneList t WHERE t.Item LIKE '" & item & "*' AND t.Size = " & size
    End If

    'Create the ADODB recordset object.
    Set rs = New ADODB.Recordset 'assign memory to the recordset

    'ConnectionString Open '—-5 aguments—-
    'Source, ActiveConnection, CursorType, LockType, Options
    rs.Open SQL, cnn

    'Check if the recordset is empty.
    If rs.EOF And rs.BOF Then
        'Close the recordet and the connection.
        rs.Close
        cnn.Close
        'clear memory
        Set rs = Nothing
        Set cnn = Nothing
        'Enable the screen.
        Application.ScreenUpdating = True
        'In case of an empty recordset display an error.
        MsgBox "There are no records in the recordset!", vbCritical, "No Records"
        Exit Sub
    End If

    'Write the reocrdset values in the sheet.
    Sheet2.Range("a2").CopyFromRecordset rs

    'Close the recordset and the connection.
    rs.Close
    cnn.Close
    'clear memory
    Set rs = Nothing
    Set cnn = Nothing

    'Update the worksheet
    Application.ScreenUpdating = True

    'Inform the user that the macro was executed successfully.
    MsgBox "Congratulation the data has been successfully Imported", vbInformation, "Data Imported"
    On Error GoTo 0
    Exit Sub
errHandler:
    'clear memory
    Set rs = Nothing
    Set cnn = Nothing
    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure Import_Data"

End Sub

谢谢

2 个答案:

答案 0 :(得分:0)

AND前应该有一个空格:

SQL = "SELECT " & col_name & " FROM PhoneList WHERE Item LIKE '" & item & "%" & "'" & " AND Size Like '" & item & "%" & "'"

答案 1 :(得分:0)

这里有一些问题-

  1. MS Access使用星号*作为通配符来表示任何字符序列(或无字符),而不是其他RDBMS使用的百分号%

  2. size是MS Access中的reserved word,因此,除非用方括号括起来或以表限定符作为前缀,否则可能会引起问题。

  3. 假设Size字段是数字字段(顾名思义,该字段),您应该不要用单引号引起来,因此代码可能变为:

    SQL = "SELECT " & col_name & " FROM PhoneList t WHERE t.Item LIKE '" & item & "*' AND t.Size = " & size