因此,我正在使用連接到Access 2010數據庫的Visual Basic 2010執行我的第一個計算項目,並且我一直在在我的項目中的兩個不同的錯誤代碼,我似乎無法左右我的頭:「INSERT INTO語句中的語法錯誤」和「沒有給出一個或多個必需參數的值」
在我的按鈕的代碼,允許用戶更新信息,我得到錯誤信息
對於一個或多個所需參數沒有給出值。
當我點擊按鈕時,它正在調試。
下面是按鈕的代碼:
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
這就是說你在db表中有更多的列,而不是你在代碼中設置的,或者你在代碼中有一個是null,並且你在表中使它不爲空。從這裏看不到它,但這就是你應該尋找的。 –
嘗試相關問題中的建議[這裏](http://stackoverflow.com/q/19557920/2144390),看看它是否有幫助。 –