2016-05-20 5 views
0

Ich erhalte einen ungültigen SQL-Statement-Fehler, wenn dieser Teil des Codes ausgeführt wird.Ungültige SQL-Anweisung in VBA, Word-Formularfelder für Zugriff db

Ich habe den Code von hier Word Forms to Acces, wo ich Stücke aktualisiert habe, damit es für mich funktioniert.

Mein Problem ist, wo in meinem Code soll ich die SQL-Anweisung hinzufügen, damit es richtig funktioniert.

Dim appWord As Word.Application 
Dim doc As Word.Document 
Dim cnn As New ADODB.Connection 
Dim rst As New ADODB.Recordset 
Dim strDocName As String 
Dim blnQuitWord As Boolean 

On Error GoTo ErrorHandling 

strDocName = C:\Word file 

Set appWord = GetObject(, "Word.Application") 
Set doc = appWord.Documents.Open(strDocName) 

cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & _ 
"Data Source=N:\Direction\4-Signalliste\" & _ 
"Signal_liste.accdb;Jet OLEDB:Database Password=1234" 

rst.Open "Order", cnn, _ 
adOpenKeyset, adLockOptimistic 

With rst 
.AddNew 
!CompanyID = doc.FormFields("CompanyID").Result 
!swgNr = doc.FormFields("SWGNr").Result 
!SWG_typical = doc.FormFields("SWG_typical").Result 
!Signal_niveau = doc.FormFields("Signal_niveau").Result 
!protocol = doc.FormFields("Protocol").Result 
!Baud_rate = doc.FormFields("Baud_Rate").Result 
!casdu = doc.FormFields("CASDU").Result 
!casdu1 = doc.FormFields("CASDU1").Result 
!casdu2 = doc.FormFields("CASDU2").Result 
!IP_A = doc.FormFields("IP_A").Result 
!IP_B = doc.FormFields("IP_B").Result 
!IP = doc.FormFields("IP").Result 
!gate = doc.FormFields("Gate").Result 
!Subnet = doc.FormFields("Subnet").Result 
!Link_addr = doc.FormFields("Link_addr").Result 
.Update 
.Close 
End With 
doc.Close 
If blnQuitWord Then appWord.Quit 
cnn.Close 
MsgBox "Data Imported!" 

Cleanup: 
Set rst = Nothing 
Set cnn = Nothing 
Set doc = Nothing 
Set appWord = Nothing 
Exit Sub 
ErrorHandling: 
Select Case Err 
Case -2147022986, 429 
Set appWord = CreateObject("Word.Application") 
blnQuitWord = True 
Resume Next 
Case 5121, 5174 
MsgBox "You must select a valid Word document. " _ 
    & "No data imported.", vbOKOnly, _ 
    "Document Not Found" 
Case 5941 
MsgBox "The document you selected does not " _ 
    & "contain the required form fields. " _ 
    & "No data imported.", vbOKOnly, _ 
    "Fields Not Found" 
Case Else 
MsgBox Err & ": " & Err.Description 
End Select 
GoTo Cleanup 

End Sub 
+0

den vollständigen Fehler anzeigen. – BIDeveloper

+0

"Ungültige SQL-Anweisung; erwartet 'DELETE', 'INSERT', 'PROCEDURE', 'SELECT' oder 'UPDATE'" – Azk

Antwort

0

habe ich eine ähnliche Situation in meinem PC und dieser Code funktioniert (nur Verbindung ändern, Tabellenname und Felder):

Private Sub CommandButton1_Click() 

Dim appWord As Word.Application 
Dim doc As Word.Document 
Dim cnn As New ADODB.Connection 
Dim rst As New ADODB.Recordset 
Dim strDocName As String 
Dim blnQuitWord As Boolean 

On Error GoTo ErrorHandling 

cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & _ 
"Data Source=F:\app\test.accdb" 

rst.Open "Table1", cnn, _ 
adOpenKeyset, adLockOptimistic 

With rst 
.AddNew 
!Field1 = ActiveDocument.FormFields("Text1").Result 
!Field2 = ActiveDocument.FormFields("Text1").Result 
!Field3 = ActiveDocument.FormFields("Text1").Result 
.Update 
.Close 
End With 
'doc.Close 
If blnQuitWord Then appWord.Quit 
cnn.Close 
MsgBox "Data Imported!" 

Cleanup: 
Set rst = Nothing 
Set cnn = Nothing 
Set doc = Nothing 
Set appWord = Nothing 
Exit Sub 
ErrorHandling: 
Select Case Err 
Case -2147022986, 429 
Set appWord = CreateObject("Word.Application") 
blnQuitWord = True 
Resume Next 
Case 5121, 5174 
MsgBox "You must select a valid Word document. " _ 
    & "No data imported.", vbOKOnly, _ 
    "Document Not Found" 
Case 5941 
MsgBox "The document you selected does not " _ 
    & "contain the required form fields. " _ 
    & "No data imported.", vbOKOnly, _ 
    "Fields Not Found" 
Case Else 
MsgBox Err & ": " & Err.Description 
End Select 
GoTo Cleanup 

End Sub 
+0

Ich habe den gleichen Fehler wie zuvor mit Ihren Änderungen erhalten "Ungültige SQL-Anweisung; erwartet 'DELETE', ' INSERT ',' PROCEDURE ',' SELECT 'oder' UPDATE '" – Azk

+0

Ich kann Ihnen Dateien (Access und Word) senden, die ich dazu benutzt habe. Wenn Sie mir eine E-Mail-Adresse geben, werde ich Ihnen senden. Wurde der Verweis auf die Microsoft ActiveX Data Objects-Bibliothek hinzugefügt? –

+0

Ich habe Ihre Dateien zu arbeiten, verpasste ich die Formularobjektbibliothek 2.0 – Azk