Use a cell value to query an external Access DB

knute_gis

New Member
Joined
Jul 5, 2019
Messages
7
I use this forum all the time and it is awesome but couldn't find an answer this time.

I got the below vba macro to work but want to use a cell value as
the search value instead of typing it directly in the macro.
I tried several different things but always returned an error

I changed the text color red that would be the cell.



[FONT=&quot]Option Explicit[/FONT]
[FONT=&quot]Sub getDataFromAccess()
‘ Click on Tools, References and select
‘ the Microsoft ActiveX Data Objects 2.0 Library[/FONT]

[FONT=&quot]Dim DBFullName As String
Dim Connect As String, Source As String
Dim Connection As ADODB.Connection
Dim Recordset As ADODB.Recordset
Dim Col As Integer[/FONT]

[FONT=&quot]Cells.Clear[/FONT]
[FONT=&quot]‘ Database path info[/FONT]
[FONT=&quot]‘ Your path will be different
DBFullName = “C:\Users\takyar\Documents\NorthWind.accdb”
‘ Open the connection
Set Connection = New ADODB.Connection
Connect = “Provider=Microsoft.ACE.OLEDB.12.0;”
Connect = Connect & “Data Source=” & DBFullName & “;”
Connection.Open ConnectionString:=Connect[/FONT]

[FONT=&quot]‘ Create RecordSet
Set Recordset = New ADODB.Recordset
With Recordset
‘ Filter Data
Source = “SELECT * FROM Customers WHERE [Job Title] = ‘Owner’ ”[/FONT]

[FONT=&quot].Open Source:=Source, ActiveConnection:=Connection[/FONT]
[FONT=&quot]‘ MsgBox “The Query:” & vbNewLine & vbNewLine & Source[/FONT]
[FONT=&quot]‘ Write field names
For Col = 0 To Recordset.Fields.Count – 1
Range(“A1”).Offset(0, Col).Value = Recordset.Fields(Col).Name
Next

‘ Write recordset
Range(“A1”).Offset(1, 0).CopyFromRecordset Recordset
End With
ActiveSheet.Columns.AutoFit
Set Recordset = Nothing
Connection.Close
Set Connection = Nothing
End Sub
Set Connection = Nothing[/FONT]

[FONT=&quot]End Sub

Thanks for you help, Knute[/FONT]
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
hi, Knute
one way maybe
regards, Fazza

Source = replace$(“SELECT * FROM Customers WHERE [Job Title] =
‘Owner’ ”, "Owner", range("B2").value2)
 
Upvote 0
Hi,
You can pass variables into the query like this"

JobTitle1 = Range("A1")

Source = “SELECT * FROM Customers WHERE [Job Title] = ' &JobTitle1& ' ”
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,152
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