将数据从Excel用户窗体复制到Access表时的语法错误

时间:2019-06-03 09:47:28

标签: sql excel vba ms-access

当尝试通过SQL将数据从Excel用户格式复制到Access表中时,我在数据库中是“是” /“否”类型的字段上遇到错误。如何正确设置信息格式,以便更新数据库?

我看过其他一些将数据复制到Access中“是/否”字段中的示例,以及尝试使用多种不同格式的代码,包括双引号,单引号和如下所示

Dim oConn As ADODB.Connection
Dim oCm As ADODB.Command
Dim iRecAffected As Integer

If Me.TBField.Visible = False Then
'Update office users

Init1 = Left(Me.TxtName1, 1) & Mid$(Me.TxtName1, InStr(Me.TxtName1, " ") + 1, 1)
    Select Case Me.LblFraOffice.Caption
        Case Is = "Add User"
            Set oConn = New ADODB.Connection
            oConn.Open sConn
            Set oCm = New ADODB.Command
            oCm.ActiveConnection = oConn
            If RbUser.Value = True Then
            oCm.CommandText = "INSERT Into Users (Name, Username, Initials, Process, FLM, FLM_ID, User, JobRole) " & _
            "VALUES ('" & TxtName1.Value & "','" & TxtUname1.Value & "','" & Init1 & "','" & TxtProcess1.Value & "','" & TxtFLM.Value & "','" & TxtFLMID.Value & "', True ,'" & TxtPosition1.Value & "')"
            ElseIf RbAdmin.Value = True Then
            oCm.CommandText = "INSERT Into Users (Name, Username, Initials, Process, FLM, FLM_ID, JobRole, User) " & _
            "VALUES ('" & TxtName1.Value & "','" & TxtUname1.Value & "','" & Init1 & "','" & TxtProcess1.Value & "','" & TxtFLM.Value & "','" & TxtFLMID.Value & "', True ,'" & TxtPosition1.Value & "')"
            ElseIf RbManager.Value = True Then
            oCm.CommandText = "INSERT Into Users (Name, Username, Initials, Process, FLM, FLM_ID, JobRole, User) " & _
            "VALUES ('" & TxtName1.Value & "','" & TxtUname1.Value & "','" & Init1 & "','" & TxtProcess1.Value & "','" & TxtFLM.Value & "','" & TxtFLMID.Value & "', True ,'" & TxtPosition1.Value & "')"
            End If

            'MsgBox oCm.CommandText
            oCm.Execute iRecAffected
            oConn.Close

在用户窗体上按Submit时,应将一个新创建的用户添加到数据库表中。如果删除“用户”字段(以及相关的true / false),它将起作用。但是我的用户可以是用户,管理员或经理

目前我收到一条错误消息

  

运行时错误'-2147217900(80040e14)':INSERT INTO语句中的语法错误

1 个答案:

答案 0 :(得分:2)

列名char* help(char *s){ char *helpString="This is the help string\n"; return helpString; } reserved keyword。您可以在关键字周围添加方括号来解决此问题吗?

User

或者您可以将列名重命名为非保留关键字以解决此错误。