所以我使用连接到Access 2010数据库的Visual Basic 2010来完成我的第一个计算项目,并且我在项目中得到了两个不同的错误代码,我似乎无法抓住我的头脑:
在我的按钮代码中,允许用户更新信息,我收到错误消息
没有给出一个或多个必需参数的值。
在调试时单击按钮。
这里是按钮的代码:
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
Dim cbMember As New OleDb.OleDbCommandBuilder(daMember) 'returns items from text box to dataset
dsMember.Tables("Members").Rows(MemberRowNumber).Item(0) = txtMemberID.Text
dsMember.Tables("Members").Rows(MemberRowNumber).Item(1) = txtForename.Text
dsMember.Tables("Members").Rows(MemberRowNumber).Item(2) = txtSurname.Text
dsMember.Tables("Members").Rows(MemberRowNumber).Item(3) = txtDOB.Text
dsMember.Tables("Members").Rows(MemberRowNumber).Item(4) = txtSex.Text
dsMember.Tables("Members").Rows(MemberRowNumber).Item(5) = txtPhone.Text
dsMember.Tables("Members").Rows(MemberRowNumber).Item(6) = txtEmail.Text
dsMember.Tables("Members").Rows(MemberRowNumber).Item(7) = txtAddress.Text
dsMember.Tables("Members").Rows(MemberRowNumber).Item(8) = txtPostcode.Text
daMember.Update(dsMember, "Members") 'updates items to database
MsgBox("Data updated")`
我收到错误消息的代码行是daMember.Update(dsMember, "Members")
行。我怎样才能让它发挥作用,因为我觉得我已经尝试了一切。
在我的按钮代码中添加了一条新记录到数据库,我收到一条错误消息
INSERT INTO语句中的语法错误
当我在调试时单击按钮,再一次我不知道如何修复它。单击按钮时收到错误消息。
这里是按钮的代码:
Private Sub btnCommit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCommit.Click
If MemberRowNumber <> -1 Then 'checks for a valid record to add
Dim cbMember As New OleDb.OleDbCommandBuilder(daMember) 'declare new variables specifically for this statement
Dim dsMemberNewRow As DataRow
dsMemberNewRow = dsMember.Tables("Members").NewRow() 'adds new row
dsMemberNewRow.Item("MemberID") = txtMemberID.Text
dsMemberNewRow.Item("Forename") = txtForename.Text 'contains values to add to new row
dsMemberNewRow.Item("Surname") = txtSurname.Text
dsMemberNewRow.Item("DOB") = txtDOB.Text
dsMemberNewRow.Item("Sex") = txtSex.Text
dsMemberNewRow.Item("Phone_Number") = txtPhone.Text
dsMemberNewRow.Item("E-Mail_Address") = txtEmail.Text
dsMemberNewRow.Item("Home_Address") = txtAddress.Text
dsMemberNewRow.Item("Postcode") = txtPostcode.Text
dsMember.Tables("Members").Rows.Add(dsMemberNewRow) 'creates new object
daMember.Update(dsMember, "Members")
MsgBox("New Record added to the Database") 'display message once process is completed
End If
End Sub
错误消息似乎与之相关的代码行再次是daMember.Update(dsMember, "Members")
行。请帮忙,因为它让我沮丧地撕掉我的头发!
另外,如果这是我第一次在本网站上发布,这是措辞不好,我很抱歉,感谢您的耐心等待。提前谢谢!
如果有帮助,请填写完整的代码:
Public Class member_details
Dim ParkwoodConnection As New OleDb.OleDbConnection 'this variable will now hold the control object
Dim Provider As String
Dim dbSource As String
Dim dsMember As New DataSet 'set up DataSet for members table
Dim daMember As OleDb.OleDbDataAdapter 'set up DataAdapter for members table
Dim dsRunning As New DataSet 'set up DataSet for Running table
Dim daRunning As OleDb.OleDbDataAdapter 'set up DataAdapter for Running table
Dim dsCycling As New DataSet 'set up DataSet for Cycling table
Dim daCycling As OleDb.OleDbDataAdapter 'set up DataAdapter for Cycling table
Dim dsSwimming As New DataSet 'set up DataSet for Swimming table
Dim daSwimming As OleDb.OleDbDataAdapter 'set up DataAdapter for Swimming table
Dim sqlMemberQuery As String
Dim sqlRunningQuery As String
Dim sqlCyclingQuery As String
Dim sqlSwimmingQuery As String
Dim MemberRowNumber As Integer
Dim MemberNumberOfRows As Integer
Dim dtRunning As New DataTable
Dim dtCycling As New DataTable
Dim dtSwimming As New DataTable
Private Sub AccessProject1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Provider = "PROVIDER=Microsoft.ACE.OLEDB.12.0;" 'technology used to conntect to database
dbSource = "Data Source = training_log.accdb" 'pathname for database
ParkwoodConnection.ConnectionString = Provider & dbSource 'establish connection between form and database
ParkwoodConnection.Open() 'open database
sqlMemberQuery = "SELECT * FROM tblMembers" 'select all records from database
daMember = New OleDb.OleDbDataAdapter(sqlMemberQuery, ParkwoodConnection) 'pass object to daMember variable
daMember.Fill(dsMember, "Members") 'fills dataset with records from table
ParkwoodConnection.Close() 'close database
MemberNumberOfRows = dsMember.Tables("Members").Rows.Count 'get how many rows are in tblMembers
MemberRowNumber = 0 'set the intial row number to 0 when the form opens
DisplayData() 'display the data on to the form
Provider = "Provider = Microsoft.ACE.OLEDB.12.0;" 'Set up link to the database
dbSource = "Data Source = Training_Log.accdb"
ParkwoodConnection.ConnectionString = Provider & dbSource 'Open the database
ParkwoodConnection.Open()
sqlRunningQuery = "SELECT * FROM tblRunning WHERE MemberID =" & txtMemberID.Text 'Use sql to get the data from the database table
daRunning = New OleDb.OleDbDataAdapter(sqlRunningQuery, ParkwoodConnection) 'Set up data adapter
dtRunning.Clear()
daRunning.Fill(dtRunning)
grdRunResults.DataSource = dtRunning 'Populate the datagrid
ParkwoodConnection.Close() 'Close the connection
Provider = "Provider = Microsoft.ACE.OLEDB.12.0;" 'Set up link to the database
dbSource = "Data Source = Training_Log.accdb"
ParkwoodConnection.ConnectionString = Provider & dbSource 'Open the database
ParkwoodConnection.Open()
sqlSwimmingQuery = "SELECT * FROM tblSwimming WHERE MemberID =" & txtMemberID.Text 'Use sql to get the data from the database table
daSwimming = New OleDb.OleDbDataAdapter(sqlSwimmingQuery, ParkwoodConnection) 'Set up data adapter
dtSwimming.Clear()
daSwimming.Fill(dtSwimming)
grdSwimResults.DataSource = dtSwimming 'Populate the datagrid
ParkwoodConnection.Close() 'Close the connection
Provider = "Provider = Microsoft.ACE.OLEDB.12.0;" 'Set up link to the database
dbSource = "Data Source = Training_Log.accdb"
ParkwoodConnection.ConnectionString = Provider & dbSource 'Open the database
ParkwoodConnection.Open()
sqlCyclingQuery = "SELECT * FROM tblCycling WHERE MemberID =" & txtMemberID.Text 'Use sql to get the data from the database table
daCycling = New OleDb.OleDbDataAdapter(sqlCyclingQuery, ParkwoodConnection) 'Set up data adapter
dtCycling.Clear()
daCycling.Fill(dtCycling)
grdCycleResults.DataSource = dtCycling 'Populate the datagrid
ParkwoodConnection.Close() 'Close the connection
End Sub
Private Sub DisplayData()
txtMemberID.Text = dsMember.Tables("Members").Rows(MemberRowNumber).Item(0) 'displays the members' ID in a text box
txtForename.Text = dsMember.Tables("Members").Rows(MemberRowNumber).Item(1) 'displays the members' forename in a text box
txtSurname.Text = dsMember.Tables("Members").Rows(MemberRowNumber).Item(2) 'displays the members' surname in a text box
txtDOB.Text = dsMember.Tables("Members").Rows(MemberRowNumber).Item(3) 'displays the members' date of birth in a text box
txtSex.Text = dsMember.Tables("Members").Rows(MemberRowNumber).Item(4) 'displays the members' sex
txtPhone.Text = dsMember.Tables("Members").Rows(MemberRowNumber).Item(5) 'displays the members' phone number in a text box
txtEmail.Text = dsMember.Tables("Members").Rows(MemberRowNumber).Item(6) 'displays the members' Emails in a text box
txtAddress.Text = dsMember.Tables("Members").Rows(MemberRowNumber).Item(7) 'displays the members' address in a text box
txtPostcode.Text = dsMember.Tables("Members").Rows(MemberRowNumber).Item(8) 'displays the members' postcode in a text box
End Sub
Private Sub btnNextRecord_Click(sender As System.Object, e As System.EventArgs) Handles btnNextRecord.Click
If MemberRowNumber <> MemberNumberOfRows - 1 Then 'moves forward to next row
MemberRowNumber = MemberRowNumber + 1
DisplayData()
Provider = "Provider = Microsoft.ACE.OLEDB.12.0;" 'Set up link to the database
dbSource = "Data Source = Training_Log.accdb"
ParkwoodConnection.ConnectionString = Provider & dbSource 'Open the database
ParkwoodConnection.Open()
sqlRunningQuery = "SELECT * FROM tblRunning WHERE MemberID =" & txtMemberID.Text 'Use sql to get the data from the database table
daRunning = New OleDb.OleDbDataAdapter(sqlRunningQuery, ParkwoodConnection) 'Set up data adapter
dtRunning.Clear()
daRunning.Fill(dtRunning)
grdRunResults.DataSource = dtRunning 'Populate the datagrid
ParkwoodConnection.Close() 'Close the connection
Provider = "Provider = Microsoft.ACE.OLEDB.12.0;" 'Set up link to the database
dbSource = "Data Source = Training_Log.accdb"
ParkwoodConnection.ConnectionString = Provider & dbSource 'Open the database
ParkwoodConnection.Open()
sqlSwimmingQuery = "SELECT * FROM tblSwimming WHERE MemberID =" & txtMemberID.Text 'Use sql to get the data from the database table
daSwimming = New OleDb.OleDbDataAdapter(sqlSwimmingQuery, ParkwoodConnection) 'Set up data adapter
dtSwimming.Clear()
daSwimming.Fill(dtSwimming)
grdSwimResults.DataSource = dtSwimming 'Populate the datagrid
ParkwoodConnection.Close() 'Close the connection
Provider = "Provider = Microsoft.ACE.OLEDB.12.0;" 'Set up link to the database
dbSource = "Data Source = Training_Log.accdb"
ParkwoodConnection.ConnectionString = Provider & dbSource 'Open the database
ParkwoodConnection.Open()
sqlCyclingQuery = "SELECT * FROM tblCycling WHERE MemberID =" & txtMemberID.Text 'Use sql to get the data from the database table
daCycling = New OleDb.OleDbDataAdapter(sqlCyclingQuery, ParkwoodConnection) 'Set up data adapter
dtCycling.Clear()
daCycling.Fill(dtCycling)
grdCycleResults.DataSource = dtCycling 'Populate the datagrid
ParkwoodConnection.Close() 'Close the connection
Else
MsgBox("No More Rows") 'display message when last row is reached
End If
End Sub
Private Sub btnPreviousRecord_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPreviousRecord.Click
If MemberRowNumber > 0 Then 'moves to previous row
MemberRowNumber = MemberRowNumber - 1
DisplayData()
Provider = "Provider = Microsoft.ACE.OLEDB.12.0;" 'Set up link to the database
dbSource = "Data Source = Training_Log.accdb"
ParkwoodConnection.ConnectionString = Provider & dbSource 'Open the database
ParkwoodConnection.Open()
sqlRunningQuery = "SELECT * FROM tblRunning WHERE MemberID =" & txtMemberID.Text 'Use sql to get the data from the database table
daRunning = New OleDb.OleDbDataAdapter(sqlRunningQuery, ParkwoodConnection) 'Set up data adapter
dtRunning.Clear()
daRunning.Fill(dtRunning)
grdRunResults.DataSource = dtRunning 'Populate the datagrid
ParkwoodConnection.Close() 'Close the connection
Provider = "Provider = Microsoft.ACE.OLEDB.12.0;" 'Set up link to the database
dbSource = "Data Source = Training_Log.accdb"
ParkwoodConnection.ConnectionString = Provider & dbSource 'Open the database
ParkwoodConnection.Open()
sqlSwimmingQuery = "SELECT * FROM tblSwimming WHERE MemberID =" & txtMemberID.Text 'Use sql to get the data from the database table
daSwimming = New OleDb.OleDbDataAdapter(sqlSwimmingQuery, ParkwoodConnection) 'Set up data adapter
dtSwimming.Clear()
daSwimming.Fill(dtSwimming)
grdSwimResults.DataSource = dtSwimming 'Populate the datagrid
ParkwoodConnection.Close() 'Close the connection
Provider = "Provider = Microsoft.ACE.OLEDB.12.0;" 'Set up link to the database
dbSource = "Data Source = Training_Log.accdb"
ParkwoodConnection.ConnectionString = Provider & dbSource 'Open the database
ParkwoodConnection.Open()
sqlCyclingQuery = "SELECT * FROM tblCycling WHERE MemberID =" & txtMemberID.Text 'Use sql to get the data from the database table
daCycling = New OleDb.OleDbDataAdapter(sqlCyclingQuery, ParkwoodConnection) 'Set up data adapter
dtCycling.Clear()
daCycling.Fill(dtCycling)
grdCycleResults.DataSource = dtCycling 'Populate the datagrid
ParkwoodConnection.Close() 'Close the connection
Else
MsgBox("First Record") 'displays message if first row is reached
End If
End Sub
Private Sub btnLastRecord_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLastRecord.Click
If MemberRowNumber <> MemberNumberOfRows - 1 Then 'moves to last record
MemberRowNumber = MemberNumberOfRows - 1
DisplayData()
Provider = "Provider = Microsoft.ACE.OLEDB.12.0;" 'Set up link to the database
dbSource = "Data Source = Training_Log.accdb"
ParkwoodConnection.ConnectionString = Provider & dbSource 'Open the database
ParkwoodConnection.Open()
sqlRunningQuery = "SELECT * FROM tblRunning WHERE MemberID =" & txtMemberID.Text 'Use sql to get the data from the database table
daRunning = New OleDb.OleDbDataAdapter(sqlRunningQuery, ParkwoodConnection) 'Set up data adapter
dtRunning.Clear()
daRunning.Fill(dtRunning)
grdRunResults.DataSource = dtRunning 'Populate the datagrid
ParkwoodConnection.Close() 'Close the connection
Provider = "Provider = Microsoft.ACE.OLEDB.12.0;" 'Set up link to the database
dbSource = "Data Source = Training_Log.accdb"
ParkwoodConnection.ConnectionString = Provider & dbSource 'Open the database
ParkwoodConnection.Open()
sqlSwimmingQuery = "SELECT * FROM tblSwimming WHERE MemberID =" & txtMemberID.Text 'Use sql to get the data from the database table
daSwimming = New OleDb.OleDbDataAdapter(sqlSwimmingQuery, ParkwoodConnection) 'Set up data adapter
dtSwimming.Clear()
daSwimming.Fill(dtSwimming)
grdSwimResults.DataSource = dtSwimming 'Populate the datagrid
ParkwoodConnection.Close() 'Close the connection
Provider = "Provider = Microsoft.ACE.OLEDB.12.0;" 'Set up link to the database
dbSource = "Data Source = Training_Log.accdb"
ParkwoodConnection.ConnectionString = Provider & dbSource 'Open the database
ParkwoodConnection.Open()
sqlCyclingQuery = "SELECT * FROM tblCycling WHERE MemberID =" & txtMemberID.Text 'Use sql to get the data from the database table
daCycling = New OleDb.OleDbDataAdapter(sqlCyclingQuery, ParkwoodConnection) 'Set up data adapter
dtCycling.Clear()
daCycling.Fill(dtCycling)
grdCycleResults.DataSource = dtCycling 'Populate the datagrid
ParkwoodConnection.Close() 'Close the connection
End If
End Sub
Private Sub btnFirstRecord_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirstRecord.Click
If MemberRowNumber <> 0 Then 'moves to first record
MemberRowNumber = 0
DisplayData()
Provider = "Provider = Microsoft.ACE.OLEDB.12.0;" 'Set up link to the database
dbSource = "Data Source = Training_Log.accdb"
ParkwoodConnection.ConnectionString = Provider & dbSource 'Open the database
ParkwoodConnection.Open()
sqlRunningQuery = "SELECT * FROM tblRunning WHERE MemberID =" & txtMemberID.Text 'Use sql to get the data from the database table
daRunning = New OleDb.OleDbDataAdapter(sqlRunningQuery, ParkwoodConnection) 'Set up data adapter
dtRunning.Clear()
daRunning.Fill(dtRunning)
grdRunResults.DataSource = dtRunning 'Populate the datagrid
ParkwoodConnection.Close() 'Close the connection
Provider = "Provider = Microsoft.ACE.OLEDB.12.0;" 'Set up link to the database
dbSource = "Data Source = Training_Log.accdb"
ParkwoodConnection.ConnectionString = Provider & dbSource 'Open the database
ParkwoodConnection.Open()
sqlSwimmingQuery = "SELECT * FROM tblSwimming WHERE MemberID =" & txtMemberID.Text 'Use sql to get the data from the database table
daSwimming = New OleDb.OleDbDataAdapter(sqlSwimmingQuery, ParkwoodConnection) 'Set up data adapter
dtSwimming.Clear()
daSwimming.Fill(dtSwimming)
grdSwimResults.DataSource = dtSwimming 'Populate the datagrid
ParkwoodConnection.Close() 'Close the connection
Provider = "Provider = Microsoft.ACE.OLEDB.12.0;" 'Set up link to the database
dbSource = "Data Source = Training_Log.accdb"
ParkwoodConnection.ConnectionString = Provider & dbSource 'Open the database
ParkwoodConnection.Open()
sqlCyclingQuery = "SELECT * FROM tblCycling WHERE MemberID =" & txtMemberID.Text 'Use sql to get the data from the database table
daCycling = New OleDb.OleDbDataAdapter(sqlCyclingQuery, ParkwoodConnection) 'Set up data adapter
dtCycling.Clear()
daCycling.Fill(dtCycling)
grdCycleResults.DataSource = dtCycling 'Populate the datagrid
ParkwoodConnection.Close() 'Close the connection
End If
End Sub
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
Dim cbMember As New OleDb.OleDbCommandBuilder(daMember) 'returns items from text box to dataset
dsMember.Tables("Members").Rows(MemberRowNumber).Item(0) = txtMemberID.Text
dsMember.Tables("Members").Rows(MemberRowNumber).Item(1) = txtForename.Text
dsMember.Tables("Members").Rows(MemberRowNumber).Item(2) = txtSurname.Text
dsMember.Tables("Members").Rows(MemberRowNumber).Item(3) = txtDOB.Text
dsMember.Tables("Members").Rows(MemberRowNumber).Item(4) = txtSex.Text
dsMember.Tables("Members").Rows(MemberRowNumber).Item(5) = txtPhone.Text
dsMember.Tables("Members").Rows(MemberRowNumber).Item(6) = txtEmail.Text
dsMember.Tables("Members").Rows(MemberRowNumber).Item(7) = txtAddress.Text
dsMember.Tables("Members").Rows(MemberRowNumber).Item(8) = txtPostcode.Text
daMember.Update(dsMember, "Members") 'updates items to database
MsgBox("Data updated")
End Sub
Private Sub btnAddNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddNew.Click
txtMemberID.Clear()
txtForename.Clear() 'clears the forename text box
txtSurname.Clear() 'clears the surname text box
txtDOB.Clear() 'clears the Date of Birth text box
txtSex.Clear() 'clears the sex text box
txtPhone.Clear() 'clears the phone number text box
txtEmail.Clear() 'clears the email text box
txtAddress.Clear() 'clears the Address text box
txtPostcode.Clear() 'clears the postcode text box
dtRunning.Clear() 'clears the running results table
dtCycling.Clear() 'clears the cycling results table
dtSwimming.Clear() 'clears the swimming results table
End Sub
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
MemberRowNumber = 0 'clears fields
DisplayData()
End Sub
Private Sub btnCommit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCommit.Click
If MemberRowNumber <> -1 Then 'checks for a valid record to add
Dim cbMember As New OleDb.OleDbCommandBuilder(daMember) 'declare new variables specifically for this statement
Dim dsMemberNewRow As DataRow
dsMemberNewRow = dsMember.Tables("Members").NewRow() 'adds new row
dsMemberNewRow.Item("MemberID") = txtMemberID.Text
dsMemberNewRow.Item("Forename") = txtForename.Text 'contains values to add to new row
dsMemberNewRow.Item("Surname") = txtSurname.Text
dsMemberNewRow.Item("DOB") = txtDOB.Text
dsMemberNewRow.Item("Sex") = txtSex.Text
dsMemberNewRow.Item("Phone_Number") = txtPhone.Text
dsMemberNewRow.Item("E-Mail_Address") = txtEmail.Text
dsMemberNewRow.Item("Home_Address") = txtAddress.Text
dsMemberNewRow.Item("Postcode") = txtPostcode.Text
dsMember.Tables("Members").Rows.Add(dsMemberNewRow) 'creates new object
daMember.Update(dsMember, "Members")
MsgBox("New Record added to the Database") 'display message once process is completed
End If
End Sub
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
If MessageBox.Show("Do you really want to Delete this Record?", _
"Delete", MessageBoxButtons.YesNo, _
MessageBoxIcon.Warning) = Windows.Forms.DialogResult.No Then
MsgBox("Operation Cancelled") 'display message if record is not deleted
Exit Sub
End If
Dim cbMember As New OleDb.OleDbCommandBuilder(daMember)
dsMember.Tables("Members").Rows(MemberRowNumber).Delete() 'selects row to delete
MemberNumberOfRows = MemberNumberOfRows - 1 'reduce number of rows
MemberRowNumber = 0 'display first record
DisplayData()
daMember.Update(dsMember, "Members") 'updates database
End Sub
End Class
答案 0 :(得分:0)
我多次遇到这个问题。 SQL查询的问题。这就是为什么消息显示是因为SQL解析器无法识别SQL查询中的标记并将其解释为您必须或需要提供值的WHICH参数。
给出价值。计算数据库的列,然后计算您拥有的值。然后,给它一个参数,并确保你没有错过其中一个。