Querying an Access database using the values in a range in excel.

whult

New Member
Joined
Oct 31, 2010
Messages
1
Querying an Access database using the values in a range in excel.

See i have some values in say column A of sheet1. I have to use these values to Query an Access database. I am trying to connect to the database using ADODB.

Another thing is that, I tried to use the SQL IN command to query the database. But that ended in vain.

can anyone help me out
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
It's a lot your asking but to try to get to the issue of the values in Excel directly, it depends on what kind of data/values they are.

It's possible you need to create a subquery for the IN clause:
SELECT W, X, Y FROM ACCESS_TABLE
WHERE X IN (SELECT Z FROM EXCEL_TABLE)

If you're using ADO you could possible construct the query string with the values as literals:

Code:
Dim x(2) As String
Dim myString As String
Dim mySQL As String
x(0) = Range("A1").Value
x(1) = Range("A2").Value
x(2) = Range("A3").Value
[COLOR="RoyalBlue"]myString [/COLOR]= "(" & x(0) & "," & x(1) & "," & x(2) & ")"
MySQL = "SELECT W, X, Y FROM ACCESS_TABLE WHERE X IN " & [COLOR="royalblue"]myString[/COLOR]
 
Upvote 0
A variation on Xenou's approach...

Code:
Sub Test()

    Dim arValues()
    Dim x As String
    Dim myString As String
    Dim mySQL As String
    
    arValues = WorksheetFunction.Transpose(Range("A:A").SpecialCells(xlCellTypeConstants))
    
    'comment out whichever does not apply
    
    'for text values:
    x = Join(arValues, "','")
    myString = "('" & x & "')"
    
    'for numbers:
    x = Join(arValues, ",")
    myString = "(" & x & ")"
    
    
    mySQL = "SELECT W, X, Y FROM ACCESS_TABLE WHERE X IN " & myString
    
    'create the connection here, run the query
End Sub

You may also find this page helpful.

Denis
 
Upvote 0

Forum statistics

Threads
1,216,773
Messages
6,132,633
Members
449,740
Latest member
tinkdrummer

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