我在下面的代码中在INSERT TO语句中收到语法错误。这样做的目的是在选项卡上将信息从word文档移动到Excel工作簿中。我有这个工作的另一个Word Doc和相同的工作簿(不同的电子表格),所以我不知道这里的问题是什么。根据我的阅读,我认为这个语法错误通常是由使用保留字引起的,但是我已经查看了保留字的JET列表,并且没有看到任何应该导致问题的内容?我很感激这里有任何帮助。我的专栏名称也与列出的内容相符,信件也是如此。
Sub TransferToExcel2()
Dim doc As Document
Dim strRendering As String
Dim strDOS_Range As String
Dim strQtr As String
Dim strE_Rate As String
Dim strE1 As String
Dim strE2 As String
Dim strE3 As String
Dim strE4 As String
Dim strE_Comments As String
Dim strNComments As String
Dim strResult_Comments As String
Dim strAuditor As String
Dim strAuditDate As String
Dim strSQL As String
Dim cnn As ADODB.Connection
Set doc = ThisDocument
On Error GoTo ErrHandler
strRendering = Chr(39) & doc.FormFields("txtRendering").Result & Chr(39)
strDOS_Range = Chr(39) & doc.FormFields("txtDOS_Range").Result & Chr(39)
strQtr = Chr(39) & doc.FormFields("txtQuarter").Result & Chr(39)
strE_Rate = Chr(39) & doc.FormFields("txtE_Rate").Result & Chr(39)
strE1 = Chr(39) & doc.FormFields("txtE1").Result & Chr(39)
strE2 = Chr(39) & doc.FormFields("txtE2").Result & Chr(39)
strE3 = Chr(39) & doc.FormFields("txtE3").Result & Chr(39)
strE4 = Chr(39) & doc.FormFields("txtE4").Result & Chr(39)
strE_Comments = Chr(39) & doc.FormFields("txtE_Comments").Result & Chr(39)
strNComments = Chr(39) & doc.FormFields("txtNComments").Result & Chr(39)
strResult_Comments = Chr(39) & doc.FormFields("txtResult_Comments").Result &
Chr(39)
strAuditor = Chr(39) & doc.FormFields("txtAuditor").Result & Chr(39)
strAuditDate = Chr(39) & doc.FormFields("txtAuditDate").Result & Chr(39)
strSQL = "INSERT INTO [Summary$]" _
& " (Rendering, DOS_Range, Qtr, E_Rate, E1, E2, E3, E4, E_Comments,
NComments, Result_Comments, Auditor, AuditDate)" _
& " VALUES (" _
& strRendering & ", " _
& strDOS_Range & ", " _
& strQtr & ", " _
& strE_Rate & ", " _
& strE1 & ", " _
& strE2 & ", " _
& strE3 & ", " _
& strE4 & ", " _
& strE_Comments & ", " _
& strNComments & ", " _
& strResult_Comments & ", " _
& strAuditor & ", " _
& strAuditDate & ", " _
& ")"
Debug.Print strSQL
Set cnn = New ADODB.Connection
With cnn
.Provider = "Microsoft.ACE.OLEDB.12.0"
.ConnectionString = "Data Source=C:\AuditTesting\AuditSpreadsheet.xlsm;" & _
"Extended Properties=Excel 8.0;"
.Open
'Transfer data.
.Execute strSQL
End With
Set doc = Nothing
Set cnn = Nothing
Exit Sub
ErrHandler:
MsgBox Err.Number & ": " & Err.Description, _
vbOKOnly, "Error"
On Error GoTo 0
On Error Resume Next
cnn.Close
Set doc = Nothing
Set cnn = Nothing
End Sub
答案 0 :(得分:-1)
尝试使用此代码:
Sub TransferToExcel2()
Dim doc As Document
Dim strRendering As String
Dim strDOS_Range As String
Dim strQtr As String
Dim strE_Rate As String
Dim strE1 As String
Dim strE2 As String
Dim strE3 As String
Dim strE4 As String
Dim strE_Comments As String
Dim strNComments As String
Dim strResult_Comments As String
Dim strAuditor As String
Dim strAuditDate As String
Dim strSQL As String
Dim cnn As ADODB.Connection
Set doc = ThisDocument
On Error GoTo ErrHandler
strRendering = Chr(39) & doc.FormFields("txtRendering").Result & Chr(39)
strDOS_Range = Chr(39) & doc.FormFields("txtDOS_Range").Result & Chr(39)
strQtr = Chr(39) & doc.FormFields("txtQuarter").Result & Chr(39)
strE_Rate = Chr(39) & doc.FormFields("txtE_Rate").Result & Chr(39)
strE1 = Chr(39) & doc.FormFields("txtE1").Result & Chr(39)
strE2 = Chr(39) & doc.FormFields("txtE2").Result & Chr(39)
strE3 = Chr(39) & doc.FormFields("txtE3").Result & Chr(39)
strE4 = Chr(39) & doc.FormFields("txtE4").Result & Chr(39)
strE_Comments = Chr(39) & doc.FormFields("txtE_Comments").Result & Chr(39)
strNComments = Chr(39) & doc.FormFields("txtNComments").Result & Chr(39)
strResult_Comments = Chr(39) & doc.FormFields("txtResult_Comments").Result & _
Chr(39)
strAuditor = Chr(39) & doc.FormFields("txtAuditor").Result & Chr(39)
strAuditDate = Chr(39) & doc.FormFields("txtAuditDate").Result & Chr(39)
strSQL = "INSERT INTO [Summary$]" _
& " (Rendering, DOS_Range, Qtr, E_Rate, E1, E2, E3, E4, E_Comments, " & _
"NComments, Result_Comments, Auditor, AuditDate)" _
& " VALUES (" _
& strRendering & ", " _
& strDOS_Range & ", " _
& strQtr & ", " _
& strE_Rate & ", " _
& strE1 & ", " _
& strE2 & ", " _
& strE3 & ", " _
& strE4 & ", " _
& strE_Comments & ", " _
& strNComments & ", " _
& strResult_Comments & ", " _
& strAuditor & ", " _
& strAuditDate & ", " _
& ")"
Debug.Print strSQL
Set cnn = New ADODB.Connection
With cnn
.Provider = "Microsoft.ACE.OLEDB.12.0"
.ConnectionString = "Data Source=C:\AuditTesting\AuditSpreadsheet.xlsm;" & _
"Extended Properties=Excel 8.0;"
.Open
'Transfer data.
.Execute strSQL
End With