aryanaveen
New member
- Local time
- Today, 19:20
- Joined
- Aug 8, 2016
- Messages
- 9
Hi All,
Please help me with the below, I have a excel VBA code which will open access database and filter based on SQL criteria and copy filtered data and paste it in excel file.
Option Explicit
Sub CreateAndRunQuery()
Dim con As Object
Dim rs As Object
Dim AccessFile As String
Dim strTable As String
Dim SQL As String
Dim i As Integer
Application.ScreenUpdating = False
AccessFile = "C:\Honnesh\Data dump.accdb"
strTable = "Table1"
On Error Resume Next
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
con.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & AccessFile
SQL = "SELECT Client ID" & strTable & " WHERE Client id='ABC'"
On Error Resume Next
Set rs = CreateObject("ADODB.Recordset")
If Err.Number <> 0 Then
Set rs = Nothing
Set con = Nothing
MsgBox "Recordset was not created!", vbCritical, "Recordset Error"
Exit Sub
End If
On Error GoTo 0
rs.CursorLocation = 3
rs.CursorType = 1
rs.Open SQL, con -------- I AM GETTING ERROR in this line
If rs.EOF And rs.BOF Then
rs.Close
con.Close
Set rs = Nothing
Set con = Nothing
Application.ScreenUpdating = True
MsgBox "There are no records in the recordset!", vbCritical, "No Records"
Exit Sub
End If
For i = 0 To rs.Fields.Count - 1
Sheets("New Query").Cells(1, i + 1) = rs.Fields(i).Name
Next i
Sheets("New Query").Range("A2").CopyFromRecordset rs
rs.Close
con.Close
Set rs = Nothing
Set con = Nothing
Sheets("New Query").Columns("A:E").AutoFit
Application.ScreenUpdating = True
MsgBox "Done"
End Sub
--------------------------------------------------------------------------------
But I am betting run time error
RUn time error : Syntax error(missing operator) in query expression 'ClientIDTable1 WHERE Client ID='ABC".
Please help
Please help me with the below, I have a excel VBA code which will open access database and filter based on SQL criteria and copy filtered data and paste it in excel file.
Option Explicit
Sub CreateAndRunQuery()
Dim con As Object
Dim rs As Object
Dim AccessFile As String
Dim strTable As String
Dim SQL As String
Dim i As Integer
Application.ScreenUpdating = False
AccessFile = "C:\Honnesh\Data dump.accdb"
strTable = "Table1"
On Error Resume Next
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
con.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & AccessFile
SQL = "SELECT Client ID" & strTable & " WHERE Client id='ABC'"
On Error Resume Next
Set rs = CreateObject("ADODB.Recordset")
If Err.Number <> 0 Then
Set rs = Nothing
Set con = Nothing
MsgBox "Recordset was not created!", vbCritical, "Recordset Error"
Exit Sub
End If
On Error GoTo 0
rs.CursorLocation = 3
rs.CursorType = 1
rs.Open SQL, con -------- I AM GETTING ERROR in this line
If rs.EOF And rs.BOF Then
rs.Close
con.Close
Set rs = Nothing
Set con = Nothing
Application.ScreenUpdating = True
MsgBox "There are no records in the recordset!", vbCritical, "No Records"
Exit Sub
End If
For i = 0 To rs.Fields.Count - 1
Sheets("New Query").Cells(1, i + 1) = rs.Fields(i).Name
Next i
Sheets("New Query").Range("A2").CopyFromRecordset rs
rs.Close
con.Close
Set rs = Nothing
Set con = Nothing
Sheets("New Query").Columns("A:E").AutoFit
Application.ScreenUpdating = True
MsgBox "Done"
End Sub
--------------------------------------------------------------------------------
But I am betting run time error
RUn time error : Syntax error(missing operator) in query expression 'ClientIDTable1 WHERE Client ID='ABC".
Please help