I have a database with 80 tables with all the same fields. What i am trying o do is search entire database for specific data. Here is the code i have been previously using to get data
strTable = "Table3"
user = frm1.Label2.Caption
Set con = CreateObject("ADODB.connection")
If Err.Number <> 0 Then
MsgBox "Connection was not created!", vbCritical, "Connection Error"
Exit Sub
End If
On Error GoTo 0
'Open the connection.
con.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & AccessFile
SQL = "SELECT* FROM " & strTable & " WHERE [JCI_Con] = '" & user & "' AND Time_out is null"
this is my full actual code i am using
Option Explicit
Sub RunQuery14()
Application.DisplayAlerts = False
Dim con As Object
Dim rs As Object
Dim AccessFile As String
Dim strTable As String
Dim SQL As String
Dim i As Integer
Dim user As String
On Error GoTo Errhandler
Application.ScreenUpdating = False
Sheets("DATA").Cells.Clear
AccessFile = "H:\APPLICATIONS\SEAT AUDIT\DATABASE\trial.accdb"
strTable = "Table1"
user = "????"
'Create the ADODB connection object.
Set con = CreateObject("ADODB.connection")
'Check if the object was created.
If Err.Number <> 0 Then
MsgBox "Connection was not created!", vbCritical, "Connection Error"
Exit Sub
End If
On Error GoTo 0
'Open the connection.
con.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & AccessFile
'Create the SQL statement to retrieve the data from table.
'Get the necessary information (first name etc.) for all the Canadian customers.
SQL = ""
SQL = SQL & " SELECT * FROM "
SQL = SQL & " ( "
SQL = SQL & " SELECT 'Zone1' AS MyTableName, * FROM Zone1 WHERE [Auditor_Name] = '" & user & "'"
SQL = SQL & " UNION ALL"
SQL = SQL & " SELECT 'Zone2' AS MyTableName, * FROM Zone2 WHERE [Auditor_Name] = '" & user & "'"
SQL = SQL & " ) AS First2Tables"
On Error Resume Next
'Create the ADODB recordset object.
Set rs = CreateObject("ADODB.Recordset")
'Check if the object was created.
If Err.Number <> 0 Then
'Error! Release the objects and exit.
Set rs = Nothing
Set con = Nothing
'Display an error message to the user.
Exit Sub
End If
On Error GoTo 0
'Set thee cursor location.
rs.CursorLocation = 3 'adUseClient on early binding
rs.CursorType = 1 'adOpenKeyset on early binding
'Open the recordset.
rs.Open SQL, con
'Check if the recordet is empty.
If rs.EOF And rs.BOF Then
'Close the recordet and the connection.
rs.Close
con.Close
'Release the objects.
Set rs = Nothing
Set con = Nothing
'Enable the screen.
Application.ScreenUpdating = True
'In case of an empty recordset display an error.
Exit Sub
End If
'Copy the recordset headers.
For i = 0 To rs.Fields.Count - 1
Sheets("DATA").Cells(1, i + 1) = rs.Fields(i).Name
Next i
'Write the query values in the sheet.
Sheets("DATA").Range("A2").CopyFromRecordset rs
'Close the recordet and the connection.
rs.Close
con.Close
'Release the objects.
Set rs = Nothing
Set con = Nothing
'Adjust the columns' width.
Sheets("DATA").Columns("A:Z").AutoFit
'Enable the screen.
Application.ScreenUpdating = True
Exit Sub
Errhandler:
End Sub