查询值和目标字段的数量不同。错误信息

时间:2014-02-14 04:01:44

标签: visual-studio

我的Visual Basic代码出现错误信息。请帮忙。感谢。

错误说:

Number of query values and destination fields are not the same.

Private Sub btnAdd_Click(ByVal sender As System.Object,ByVal e As System.EventArgs)处理btnAdd.Click     Dim cmd As New OleDb.OleDbCommand

If Not cnn.State = ConnectionState.Open Then
    cnn.Open()
End If

cmd.Connection = cnn

If Me.txtID.Tag & "" = "" Then
    cmd.CommandText = "INSERT INTO ProfessorListTable(ID,LastName,FirstName,MI,Gender,Department,ContactNo,Address,EmailAddress,YearEmployed)" & _
        " VALUES(" & Me.txtID.Text & ",'" & Me.txtLName.Text & "','" & _
        Me.txtFName.Text & "','" & Me.txtMI.Text & "','" & _
        Me.txtGender.Text & "','" & Me.txtDept.Text & "','" & _
        Me.txtNo.Text & "','" & Me.txtAddress.Text & "','" & _
        Me.txtEAdd.Text & "','" & Me.txtYear.Text & "',')"

    cmd.ExecuteNonQuery()
Else
    cmd.CommandText = "UPDATE ProfessorListTable " & _
    " SET txtID=" & Me.txtID.Text & _
    ", LastName='" & Me.txtLName.Text & "'" & _
    ", FirstName='" & Me.txtFName.Text & "'" & _
    ", MI='" & Me.txtMI.Text & "'" & _
    ", Gender='" & Me.txtGender.Text & "'" & _
    ", Department='" & Me.txtDept.Text & "'" & _
    ", ContactNo='" & Me.txtNo.Text & _
    ", Address='" & Me.txtAddress.Text & "'" & _
    ", EmailAddress='" & Me.txtEAdd.Text & "'" & _
    ", YearEmployed='" & Me.txtYear.Text & _
    " WHERE stdid=" & Me.txtID.Tag
    cmd.ExecuteNonQuery()


End If


RefreshData()

End Sub

1 个答案:

答案 0 :(得分:1)

你这里有额外的逗号(,)

& Me.txtYear.Text & "','

所以它导致11值而不是10

地址是保留字,因此将其用作[地址]

试试这个

Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click Dim cmd As New OleDb.OleDbCommand

If Not cnn.State = ConnectionState.Open Then
    cnn.Open()
End If

cmd.Connection = cnn

If Me.txtID.Tag & "" = "" Then
    cmd.CommandText = "INSERT INTO ProfessorListTable(ID,LastName,FirstName,MI,Gender,Department,ContactNo,[Address],EmailAddress,YearEmployed)" & _
        " VALUES(" & Me.txtID.Text & ",'" & Me.txtLName.Text & "','" & _
        Me.txtFName.Text & "','" & Me.txtMI.Text & "','" & _
        Me.txtGender.Text & "','" & Me.txtDept.Text & "','" & _
        Me.txtNo.Text & "','" & Me.txtAddress.Text & "','" & _
        Me.txtEAdd.Text & "','" & Me.txtYear.Text & "')"

    cmd.ExecuteNonQuery()
Else
    cmd.CommandText = "UPDATE ProfessorListTable " & _
    " SET txtID=" & Me.txtID.Text & _
    ", LastName='" & Me.txtLName.Text & "'" & _
    ", FirstName='" & Me.txtFName.Text & "'" & _
    ", MI='" & Me.txtMI.Text & "'" & _
    ", Gender='" & Me.txtGender.Text & "'" & _
    ", Department='" & Me.txtDept.Text & "'" & _
    ", ContactNo='" & Me.txtNo.Text & _
    ", [Address]='" & Me.txtAddress.Text & "'" & _
    ", EmailAddress='" & Me.txtEAdd.Text & "'" & _
    ", YearEmployed='" & Me.txtYear.Text & _
    " WHERE stdid=" & Me.txtID.Tag
    cmd.ExecuteNonQuery()


End If
RefreshData()
End Sub