SQL Query in VBA

AndrewKent

Well-known Member
Joined
Jul 26, 2006
Messages
889
Hi there,

I have the following code that pulls back data from Access into Excel...

Code:
Sub FindCustomerData(DBConnection As ADODB.Connection)

    Dim DBRecordSet As ADODB.Recordset
    Dim Query As String

    Query = "SELECT tblCustomerData.Record_ID, tblCustomerData.First_Name, tblCustomerData.Surname, tblCustomerData.Age, tblCustomerData.Created_Date FROM tblCustomerData  WHERE (((tblCustomerData.Record_ID)='" & Worksheets("Search Engine").Range("C4").Value & "')) OR (((tblCustomerData.First_Name)='" & Worksheets("Search Engine").Range("C5").Value & "')) OR (((tblCustomerData.Surname)='" & Worksheets("Search Engine").Range("C6").Value & "'));"
    
    Set DBRecordSet = New ADODB.Recordset
    DBRecordSet.CursorLocation = adUseServer
    DBRecordSet.Open Source:=Query, ActiveConnection:=DBConnection, CursorType:=adOpenForwardOnly, LockType:=adLockOptimistic, Options:=adCmdText
    
    Worksheets("Search Engine").Range("E5").CopyFromRecordset DBRecordSet

    DBRecordSet.Close
    Set DBRecordSet = Nothing

End Sub

problem is that all the values in the query are text values, if I update the Query with an extra OR statement...

Code:
OR (((tblCustomerData.Age)='" & Worksheets("Search Engine").Range("C7").Value & "'))

...I get errors, how to I update my code here so that it will allow me query on a number as well as a text string. I'll also need to know if there is any differences for queries based on dates as well.

The error I get is data type mismatch on this line...

Code:
DBRecordSet.Open Source:=Query, ActiveConnection:=DBConnection, CursorType:=adOpenForwardOnly, LockType:=adLockOptimistic, Options:=adCmdText

Cheers,

Andy
 
I suspect your data is actually coming across as text if the numberformat doesn't work. You could use the Format function in the query to format the dates?
 
Upvote 0

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
I've only just got my head around adding SQL queries to VBA code so without sounding too much like I need spoon fed on this one, I've no idea how to format the query. :(
 
Upvote 0
Query = "SELECT tblCustomerData.Record_ID, tblCustomerData.First_Name, tblCustomerData.Surname, tblCustomerData.Age, Format(tblCustomerData.Created_Date, "dd/mm/yyyy hh:mm:ss") As CreateDate FROM tblCustomerData WHERE (((tblCustomerData.Record_ID)='" & Worksheets("Search Engine").Range("C4").Value & "')) OR (((tblCustomerData.First_Name)='" & Worksheets("Search Engine").Range("C5").Value & "')) OR (((tblCustomerData.Surname)='" & Worksheets("Search Engine").Range("C6").Value & "'));"

for example.
 
Upvote 0

Forum statistics

Threads
1,214,932
Messages
6,122,332
Members
449,077
Latest member
jmsotelo

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