在Access 2010中的INSERT INTO语句中获取SQL语法错误

时间:2014-06-12 09:10:56

标签: sql syntax insert access-vba ms-access-2010

我在Access 2010 VBA中编写了以下Insert Into语句:

Private Sub AddBPSSButton_Click()

'    CurrentDb.Execute "INSERT INTO TabClearDetail(C_Site) VALUES(" & Me.C_Site & ")"

Dim strSQL As String

'MsgBox Me.[Clearance Applying For]
'MsgBox Me.[Contract Applying for]
'MsgBox Me.[C_Site]
'MsgBox Me.[C_SponsorSurname]
'MsgBox Me.[C_SponsorForename]
'MsgBox Me.[C_SponsorContactDetails]
'MsgBox Me.[C_EmploymentDetail]
'MsgBox Me.[C_SGNumber]
'MsgBox Me.[C_REF1DateRecd]
'MsgBox Me.[C_REF2DateRecd]
'MsgBox Me.[C_IDDateRecd]
'MsgBox Me.[C_IDNum]
'MsgBox Me.[C_CriminalDeclarationDate]
'MsgBox Me.[Credit Check Consent]
'MsgBox Me.[C_CreditCheckDate]
'MsgBox Me.[Referred for Management Decision]
'MsgBox Me.[Management Decision Date]
'MsgBox Me.[C_Comment]
'MsgBox Me.[C_DateCleared]
'MsgBox Me.[C_ClearanceLevel]
'MsgBox Me.[C_ContractAssigned]
'MsgBox Me.[C_ExpiryDate]
'MsgBox Me.[C_LinKRef]
'MsgBox Me.[C_OfficialSecretsDate]

strSQL = "INSERT INTO TabClearDetail(Clearance Applying For, Contract Applying for, " & _
"C_Site, C_SponsorSurname, C_SponsorForename, C_SponsorContactDetails, C_EmploymentDetail, " & _
"C_SGNumber, C_REF1DateRecd, C_RED2DateRecd, C_IDDateRecd, C_IDNum, " & _
"C_CriminalDeclarationDate, Credit Check Consent, C_CreditCheckDate, Referred for Management Decision, " & _
"Management Decision Date, C_Comment, C_DateCleared, C_ClearanceLevel, C_ContractAssigned, " & _
"C_ExpiryDate, C_LinkRef, C_OfficialSecretsDate) VALUES('" & Me.[Clearance Applying For] & "', " & _
"'" & Me.[Contract Applying for] & "', '" & Me.[C_Site] & "', '" & Me.[C_SponsorSurname] & "', " & _
"'" & Me.[C_SponsorForename] & "', '" & Me.[C_SponsorContactDetails] & "', " & _
"'" & Me.[C_EmploymentDetail] & "', '" & Me.[C_SGNumber] & "', '" & Me.[C_REF1DateRecd] & "', " & _
"'" & Me.[C_REF2DateRecd] & "', '" & Me.[C_IDDateRecd] & "', '" & Me.[C_IDNum] & "', " & _
"'" & Me.[C_CriminalDeclarationDate] & "', '" & Me.[Credit Check Consent] & "', '" & Me.[C_CreditCheckDate] & "', " & _
"'" & Me.[Referred for Management Decision] & "', '" & Me.[Management Decision Date] & "', " & _
"'" & Me.[C_Comment] & "', '" & Me.[C_DateCleared] & "', '" & Me.[C_ClearanceLevel] & "', " & _
"'" & Me.[C_ContractAssigned] & "', '" & Me.[C_ExpiryDate] & "', '" & Me.[C_LinKRef] & "', " & _
"'" & Me.[C_OfficialSecretsDate] & "');"

DoCmd.RunSQL (strSQL)

'MsgBox strSQL

End Sub

所有MsgBox调用都有效,所以我相信我已正确输入所有列名和文本框名。当我到达DoCmd.RunSQL行时,我收到语法错误。一直在盯着这个试图看我是否错过了逗号或语音标记或其他什么,但我希望也许另一组眼睛会看到我的错误。

Here is the MsgBox that is returned

非常感谢任何帮助。

谢谢!

2 个答案:

答案 0 :(得分:2)

如果对象名称中有空格,则需要将全名括在括号中。所以你在哪里:

INSERT INTO TabClearDetail(Clearance Applying For, Contract Applying for,...

应该是

INSERT INTO TabClearDetail([Clearance Applying For], [Contract Applying for],...

对于它的价值,我个人无法忍受对象名称中的特殊字符,正是出于这个原因,如果不是太晚,你应该考虑重命名你的列。对于化合物名称,Pascal Case通常足够清晰,例如ClearanceApplyingFor。或使用得分 - Clearance_Applying_For。

我还认为Access中的约定是用#而不是单引号来限定日期,即#01/01/2014#,而不是'01/01/2014'

答案 1 :(得分:0)

您可以尝试用"#"封闭日期字段。而不是"'"或更改数据库的SQL Server兼容语法"属性。