我有一个初始UserForm
,提示用户输入他/她的登录凭据,并根据访问数据库(.mdb)对其进行检查。正如您所想象的那样,如果凭据不匹配,则不会允许他们登录,如果他们这样做,则会登录。
在此之后," Home" UserForm
显示(我只有AddUser
按钮,空Settings
按钮)。用户点击["添加用户..."]后,会显示AddUserForm
。在此,有用户名(UsernameTextbox
),密码(PasswordTextbox
),名字(FirstNameTextbox
),姓氏(LastNameTextbox
),部门({{1})的文本框})和位置(DepartmentList
)。 (参考图片)
在测试" INSERT INTO"我的OleDbCommand的命令文本,我只使用了FirstName和LastName PositionList
,它成功地将TextBoxes
添加到FirstName列,将FirstNameTextbox.Text
添加到Database中的LastName列。
然而,一旦我添加了剩余标准,我就收到此错误消息,说明INSERT INTO语句中存在语法错误,但我不确定是什么。在使用F8逐步执行程序时,它会在没有打嗝的情况下完成Try列表,但会捕获异常。如何解决这个问题?
以下是代码:
LastNameTextbox.Text
以下是错误的屏幕截图和Private Sub AddButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddUserButton.Click
Try
Dim SQL As String = "INSERT INTO userTable (Username, Password, LastName, FirstName, Department, " & _
"UserPrivileges) VALUES (@Username, @Password, @LastName, @FirstName, @Department, @UserPrivileges);"
Dim sqlCom As New OleDbCommand(SQL)
sqlCom.Parameters.Add("@Username", OleDbType.VarChar).Value = UsernameTextbox.Text
If PasswordTextbox.Text = ConfirmPasswordTextbox.Text Then 'Check if the passwords match
sqlCom.Parameters.Add("@Password", OleDbType.VarChar).Value = PasswordTextbox.Text
End If
sqlCom.Parameters.Add("@LastName", OleDbType.VarChar).Value = LastNameTextbox.Text
sqlCom.Parameters.Add("@FirstName", OleDbType.VarChar).Value = FirstNameTextbox.Text
sqlCom.Parameters.Add("@Department", OleDbType.VarChar).Value = DepartmentList.Text
sqlCom.Parameters.Add("@UserPrivileges", OleDbType.VarChar).Value = PositionList.Text
Debug.Print(DepartmentList.Text)
'Open Database Connection
sqlCom.Connection = Conn
Conn.Open()
sqlCom.ExecuteNonQuery()
'Dim sqlRead As OleDbDataReader = sqlCom.ExecuteReader()
Catch ex As Exception
MessageBox.Show("AddUserForm: Failed to connect to Database... System Error Message: " & ex.Message, "Database Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
: