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="]Option Explicit[/FONT]
[FONT="]Sub getDataFromAccess()
‘ Click on Tools, References and select
‘ the Microsoft ActiveX Data Objects 2.0 Library[/FONT]
[FONT="]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="]Cells.Clear[/FONT]
[FONT="]‘ Database path info[/FONT]
[FONT="]‘ 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="]‘ Create RecordSet
Set Recordset = New ADODB.Recordset
With Recordset
‘ Filter Data
Source = “SELECT * FROM Customers WHERE [Job Title] = ‘Owner’ ”[/FONT]
[FONT="].Open Source:=Source, ActiveConnection:=Connection[/FONT]
[FONT="]‘ MsgBox “The Query:” & vbNewLine & vbNewLine & Source[/FONT]
[FONT="]‘ 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="]End Sub
Thanks for you help, Knute[/FONT]
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="]Option Explicit[/FONT]
[FONT="]Sub getDataFromAccess()
‘ Click on Tools, References and select
‘ the Microsoft ActiveX Data Objects 2.0 Library[/FONT]
[FONT="]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="]Cells.Clear[/FONT]
[FONT="]‘ Database path info[/FONT]
[FONT="]‘ 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="]‘ Create RecordSet
Set Recordset = New ADODB.Recordset
With Recordset
‘ Filter Data
Source = “SELECT * FROM Customers WHERE [Job Title] = ‘Owner’ ”[/FONT]
[FONT="].Open Source:=Source, ActiveConnection:=Connection[/FONT]
[FONT="]‘ MsgBox “The Query:” & vbNewLine & vbNewLine & Source[/FONT]
[FONT="]‘ 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="]End Sub
Thanks for you help, Knute[/FONT]