Microsoft Access 2010 VBA插入语法错误

时间:2012-02-17 14:17:15

标签: vb.net ms-access

我对Microsoft Access和VB完全不熟悉,我想要做的是我有一个带有无界文本框的表单,我想点击一个按钮,将文本保存在其独特字段中的每个文本框中。

我设法在网上资源的帮助下编写了这段代码,但是如果有人能指出我正确的方法,它会不断给我语法错误。

CurrentDb.Execute "INSERT INTO UserInformation(" & _
    "FirstName, LastName, Company, JobTtile, PhoneNumber, Mobile, Email, Fax, " & _
    "IT-DEC-MAKER-FNAME, IT-DEC-MAKER-LNAME) " & _
  "VALUES('" & Me.qfirstname & "','" & Me.qlastname & "','" & Me.qcompany & "','" & _
    Me.qjob & "','" & Me.qphone & "','" & Me.qmobile & "','" & Me.qemail & "','" & _
    Me.qfax & "','" & Me.qitfirstname & "','" & Me.qitlastname & "');"

2 个答案:

答案 0 :(得分:2)

由于IT-DEC-MAKER-FNAMEIT-DEC-MAKER-LNAME是SQL中的无效标识符,因此您必须将它们括在括号中([ ]

CurrentDb.Execute "INSERT INTO UserInformation(" & _
    "FirstName, LastName, Company, JobTtile, PhoneNumber, Mobile, Email, Fax, " & _
    "[IT-DEC-MAKER-FNAME], [IT-DEC-MAKER-LNAME]) " & _
  "VALUES('" & Me.qfirstname & "','" & Me.qlastname & "','" & Me.qcompany & "','" & _
    Me.qjob & "','" & Me.qphone & "','" & Me.qmobile & "','" & Me.qemail & "','" & _
    Me.qfax & "','" & Me.qitfirstname & "','" & Me.qitlastname & "');"

<强>更新

我有两个很好的辅助功能来处理这类事情。

这个用字符串

替换字符串中的占位符
Function Build(ByVal s As String, ParamArray args()) As String
    ' Build("LastName = {0}, FirstName = {1}","Dow","John") --> "LastName = Dow, FirstName = John".
    ' "\n" will be converted to vbCrLf.

    Dim i As Long

    s = Replace(s, "\n", vbCrLf)
    For i = 0 To UBound(args)
        s = Replace(s, "{" & i & "}", Nz(args(i)))
    Next i
    Build = s
End Function

这个将应该包含文本的变量值转换为SQL值

Public Function SqlStr(ByVal v As Variant) As String
    ' NULL    Returns: NULL
    ' ""      Returns: NULL
    ' "abc"   Returns: 'abc'
    ' "x'y"   Returns: 'x''y'

    Dim s As String

    s = Nz(v)
    If s = "" Then
        SqlStr = "NULL"
    Else
        SqlStr = "'" & Replace(s, "'", "''") & "'"
    End If
End Function

然后,您可以像这样使用它们,使您的代码更安全,更易于理解。

Dim template As String, sql As String

template = "INSERT INTO UserInformation(" & _
  "FirstName, LastName, Company, JobTtile, PhoneNumber, Mobile, Email, Fax, " & _
  "[IT-DEC-MAKER-FNAME], [IT-DEC-MAKER-LNAME]) " & _
  "VALUES({0},{1},{2},{3},{4},{5},{6},{7},{8},{9});"
sql = Build(template, _
  SqlStr(Me.qfirstname), SqlStr(Me.qlastname), _
  SqlStr(Me.qjob), SqlStr(Me.qphone), _
  SqlStr(Me.qmobile), SqlStr(Me.qemail), _
  SqlStr(Me.qfax), SqlStr(Me.qitfirstname), _
  SqlStr(Me.qitlastname))
CurrentDb.Execute sql

答案 1 :(得分:1)

我建议你像这样列出你的代码:

sSQL = "INSERT INTO UserInformation(" _
& "FirstName, LastName, Company, JobTtile, " _
& "PhoneNumber, Mobile, Email, Fax, " _
& "IT-DEC-MAKER-FNAME, IT-DEC-MAKER-LNAME) " _
& "VALUES('" _
& Me.qfirstname & "','" & Me.qlastname & "','" & Me.qcompany & "','" & Me.qjob _
& "','" & Me.qphone & "','" & Me.qmobile & "','" & Me.qemail & "','" & Me.qfax _
& "','" & Me.qitfirstname & "','" & Me.qitlastname & "');"

CurrentDB.Execute sSQL, dbFailOnError

它使得更容易看到错误,并且可以将sSQL字符串打印到即时窗口以进行调试。你会看到你有JobTtile。

通常最好使用CurrentDB的实例:

Dim db As DAO.Database
Set db = CurrentDB

我想知道你为什么不绑定记录集?