[VBA] SQL to get data from access to excel

skorpionkz

Well-known Member
Joined
Oct 1, 2013
Messages
1,171
Office Version
  1. 2016
Hi All,

I am building VBA tool and need to incorporate report into it. Data is recorded in Access database.

User will pick status of project and project code list to see in the Report.

I build SQL which works in Access query, but fails in VBA

sample query
Code:
SELECT *  FROM Key_table WHERE (Status = 'Submitted') AND (LookUp_Key LIKE 'PRJ0001*' OR LookUp_Key LIKE 'PRJ0002*')

Any ideas how to force it to work in VBA?
Code:
Private Sub LoadKeyTable()
    Dim myConnection            As New ADODB.Connection
    
    Dim v_Item                  As Variant
    Dim str_SQL                 As String
    Dim str_List                As String
    Dim arr_List                As Variant
            
    str_List = uf_Main.txt_ReportList
    If Not str_List = Empty Then
        arr_List = Split(str_List, ",")
        
        str_List = Empty
        For Each v_Item In arr_List
            If Not v_Item = Empty Then
                If str_List = Empty Then
                    str_List = "LookUp_Key LIKE '" & v_Item & "*'"
                Else
                    str_List = str_List & " OR LookUp_Key LIKE '" & v_Item & "*'"
                End If
            End If
        Next v_Item
    End If
       
    str_SQL = "SELECT *  FROM " & DB.KEYTABLE
    
    str_SQL = str_SQL & " WHERE (Status = '" & PBL.LoadList & "')"
    
    If Not str_List = Empty Then str_SQL = str_SQL & " AND (" & str_List & ")"
    
    Set myConnection = ConnectTo_IPSDB
    Set PBL.RecordSet = myConnection.Execute(str_SQL)
      
    RecordSetToClass DB.KEYTABLE


    myConnection.Close
                
    Set PBL.RecordSet = Nothing
    Set myConnection = Nothing


End Sub

Thank you,
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Thank you,
Simple solution :) It is weird one though... MS Access SQL work with * rather than %. And if you ran query within Access * works just fine...

Any way thank you. This did the trick.
 
Upvote 0
Hi scorpionkz. I agree its curious, but you will find many varieties of SQL implemented in the world, with different syntax for things like wildcards, date formats, etc. It's a real pain for me at times because I work with access SQL, msquery sql, mySQL and others as well.
 
Upvote 0

Forum statistics

Threads
1,224,819
Messages
6,181,153
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top