Dlaczego pojawia się błąd w tym module Excela, który uruchamia zapytania dostępu i kopiuje wyniki do skoroszytu?

1

Napisałem moduł w programie Excel do uruchamiania 6 zapytań dostępu i kopiowania ich do skoroszytu programu Excel. Mój kod działa idealnie, dopóki nie przejdę do 5. i 6. zapytania, a następnie pojawi się błąd;

Błąd czasu wykonania „-2147217900 (80040e14) Niepoprawna instrukcja SQL; oczekiwane „DELETE”, „INSERT”, „PROCEDURE”, „SELECT” lub „UPDATE”.

Nie jestem pewien dlaczego, jakieś myśli?

Option Explicit

Sub RunExistingQuery()

'------------------------------------------------------------------------------------
'This macro opens the FACLoadView.accdb database and runs the (existing) queries
'Then, it copies all the query results back in the Excel sheet.
'The code uses late binding, so no reference to external library is required.

'------------------------------------------------------------------------------------

'Declaring the necessary variables.
Dim con         As Object
Dim rs          As Object
Dim AccessFile  As String
Dim strQuery    As String
Dim i           As Integer

'Disable screen flickering.
Application.ScreenUpdating = False

'Specify the file path of the accdb file. You can also use the full path of the file like:
AccessFile = "C:\Users\lynn.sferrino\Documents\CBF\CBF Databases\FLV 2015-04-22.accdb"

'Set the name of the 1st query you want to run and retrieve the data.
strQuery = "BOH"

On Error Resume Next
'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

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.
    MsgBox "Recordset was not created!", vbCritical, "Recordset Error"
    Exit Sub
End If
On Error GoTo 0

'Set the cursor location.
rs.CursorLocation = 3 'adUseClient on early  binding
rs.CursorType = 1 'adOpenKeyset on early  binding

'Open the recordset.
rs.Open strQuery, 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.
    MsgBox "There are no records in the recordset!", vbCritical, "No Records"
    Exit Sub
End If

'Copy the recordset headers.
For i = 0 To rs.Fields.Count - 1
    Sheets("BOH").Cells(1, i + 1) = rs.Fields(i).Name
Next i

'Write the query values in the sheet.
 Sheets("BOH").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.
Columns("A:B").AutoFit

'Enable the screen.
Application.ScreenUpdating = True

'Inform the user that the macro was executed successfully.
MsgBox "All data were  successfully retrieved from the '" & strQuery & "' query!", vbInformation, "Done"

'Set the name of the 2nd query you want to run adn retrieve the data.
strQuery = "REC"

On Error Resume Next
'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

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.
    MsgBox "Recordset was not created!", vbCritical, "Recordset Error"
    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 strQuery, 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.
    MsgBox "There are no records in the recordset!", vbCritical, "No Records"
    Exit Sub
End If

'Copy the recordset headers.
For i = 0 To rs.Fields.Count - 1
    Sheets("REC").Cells(1, i + 1) = rs.Fields(i).Name
Next i

'Write the query values in the sheet.
 Sheets("REC").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.
Columns("A:B").AutoFit

'Enable the screen.
Application.ScreenUpdating = True

'Inform the user that the macro was executed successfully.
MsgBox "All data were  successfully retrieved from the '" & strQuery & "' query!", vbInformation, "Done"

'Set the name of the 3rd query you want to run adn retrieve the data.
strQuery = "COM"

On Error Resume Next
'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

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.
    MsgBox "Recordset was not created!", vbCritical, "Recordset Error"
    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 strQuery, 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.
    MsgBox "There are no records in the recordset!", vbCritical, "No Records"
    Exit Sub
End If

'Copy the recordset headers.
For i = 0 To rs.Fields.Count - 1
    Sheets("COM").Cells(1, i + 1) = rs.Fields(i).Name
Next i

'Write the query values in the sheet.
 Sheets("COM").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.
Columns("A:B").AutoFit

'Enable the screen.
Application.ScreenUpdating = True

'Inform the user that the macro was executed successfully.
MsgBox "All data were  successfully retrieved from the '" & strQuery & "' query!", vbInformation, "Done"

'Set the name of the 4th query you want to run adn retrieve the data.
strQuery = "NOH"

On Error Resume Next
'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

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.
    MsgBox "Recordset was not created!", vbCritical, "Recordset Error"
    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 strQuery, 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.
    MsgBox "There are no records in the recordset!", vbCritical, "No Records"
    Exit Sub
End If

'Copy the recordset headers.
For i = 0 To rs.Fields.Count - 1
    Sheets("NOH").Cells(1, i + 1) = rs.Fields(i).Name
Next i

'Write the query values in the sheet.
 Sheets("NOH").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.
Columns("A:B").AutoFit

'Enable the screen.
Application.ScreenUpdating = True

'Inform the user that the macro was executed successfully.
MsgBox "All data were  successfully retrieved from the '" & strQuery & "' query!", vbInformation, "Done"



'Set the name of the 5th query you want to run and retrieve the data.
strQuery = "Networks Idle"

On Error Resume Next
'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

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.
    MsgBox "Recordset was not created!", vbCritical, "Recordset Error"
    Exit Sub
End If
On Error GoTo 0

'Set the cursor location.
rs.CursorLocation = 3 'adUseClient on early  binding
rs.CursorType = 1 'adOpenKeyset on early  binding

'Open the recordset.
rs.Open strQuery, 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.
    MsgBox "There are no records in the recordset!", vbCritical, "No Records"
    Exit Sub
End If

'Copy the recordset headers.
For i = 0 To rs.Fields.Count - 1
    Sheets("Networks Idle").Cells(1, i + 1) = rs.Fields(i).Name
Next i

'Write the query values in the sheet.
 Sheets("Networks Idle").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.
Columns("A:B").AutoFit

'Enable the screen.
Application.ScreenUpdating = True

'Inform the user that the macro was executed successfully.
MsgBox "All data were  successfully retrieved from the '" & strQuery & "' query!", vbInformation, "Done"

'Set the name of the 1st query you want to run and retrieve the data.
strQuery = "RB Query"

On Error Resume Next
'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

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.
    MsgBox "Recordset was not created!", vbCritical, "Recordset Error"
    Exit Sub
End If
On Error GoTo 0

'Set the cursor location.
rs.CursorLocation = 3 'adUseClient on early  binding
rs.CursorType = 1 'adOpenKeyset on early  binding

'Open the recordset.
rs.Open strQuery, 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.
    MsgBox "There are no records in the recordset!", vbCritical, "No Records"
    Exit Sub
End If

'Copy the recordset headers.
For i = 0 To rs.Fields.Count - 1
    Sheets("RB Query").Cells(1, i + 1) = rs.Fields(i).Name
Next i

'Write the query values in the sheet.
 Sheets("RB Query").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.
Columns("A:B").AutoFit

'Enable the screen.
Application.ScreenUpdating = True

'Inform the user that the macro was executed successfully.
MsgBox "All data were  successfully retrieved from the '" & strQuery & "' query!", vbInformation, "Done"


End Sub
Lynn
źródło
jaki wiersz ma błąd?
Raystafarian,
zaczynając od miejsca, w którym jest napisane: „Ustaw nazwę piątego zapytania, które chcesz uruchomić, i pobierz dane.
Lynn