我正在使用与.accdb文件接口的VB.Net应用程序来创建和(最终)更新同一数据库中两个表的记录。我能够将新信息写入表中没有问题,但是它正在更新/更改/添加附加信息到表I中遇到问题的同一行。我将更新写入现有行的代码位于我的帖子的底部。
我遇到的最大问题是,在执行此子例程后,它在objCmd.ExecuteNonQuery()
失败并显示错误消息IErrorInfo.GetDescription failed with E_FAIL(0x80004005)
。我已经在这里和Google进行了梳理,尝试了不同的方法和移动的东西,我无法弄清楚我错过了什么。据我所知,我在SQL查询中没有使用任何保留字。 Else
语句下的块确实可以用于创建新行(我的程序的那一侧没有问题),也许执行UPDATE命令的语法不同?非常感谢任何帮助/见解。
Private Sub WriteToDatabase()
strcs = txtSerialNumber.Text
strOrderType = orderType
strPoRMA = txtPoRMA.Text
strtech = cboTech.Text
strDate = calendarTest.SelectionStart
'Write to database if Production
If strOrderType = "PO" Then
'Check database for duplicate record
strSQL = "SELECT * FROM [New Camera Database] WHERE cameraSer=" & strcs
objCmd = New OleDbCommand(strSQL, dbconn)
dr = objCmd.ExecuteReader
dr.Read()
If dr("calCompleteDate").ToString <> "" Then
MsgBox("Camera S/N " & strcs & " completed " & dr("calCompleteDate") & ". Use Lookup to reprint Cert. of Compliance", vbOK + vbExclamation,
"Camera S/N " & strcs & " already completed")
exitFlag = True
Else
'Write to New Camera Database Table
strSQL = "UPDATE [New Camera Database] SET poNum=@poNum , calCompleteDate=@calCompleteDate, calCompleteTech=@calCompleteTech WHERE cameraSer=" & strcs
objCmd = New OleDbCommand(strSQL, dbconn)
objCmd.Parameters.AddWithValue("@poNum", strPoRMA)
objCmd.Parameters.AddWithValue("@calCompleteDate", strcs)
objCmd.Parameters.AddWithValue("@calCompleteTech", strtech)
objCmd.ExecuteNonQuery()
'Write to up2DateTravelers Table
strSQL = "UPDATE up2DateTravelers SET poRMANum = @poRMANum, calCompleteDate = @calCompleteDate, calCompleteTech = @calCompleteTech WHERE cameraSer=" & strcs
objCmd = New OleDbCommand(strSQL, dbconn)
objCmd.Parameters.AddWithValue("@poRMANum", strPoRMA)
objCmd.Parameters.AddWithValue("@calCompleteDate", strcs)
objCmd.Parameters.AddWithValue("@calCompleteTech", strtech)
objCmd.ExecuteNonQuery()
End If
ElseIf strOrderType = "RMA" Then
'Create new functions, userform, etc (TBD)
End If
btnClear.PerformClick()
End Sub
答案 0 :(得分:2)
我猜这一行:
objCmd.Parameters.AddWithValue("@calCompleteDate", strcs)
是一个错误,您想使用日期:
objCmd.Parameters.AddWithValue("@calCompleteDate", strDate)
另外,使用Using和parametrized queries:
'Write to New Camera Database Table
strSQL = "UPDATE [New Camera Database] SET poNum=@poNum , calCompleteDate=@calCompleteDate, calCompleteTech=@calCompleteTech WHERE cameraSer=@cameraSer"
Using objCmd As New OleDbCommand(strSQL, dbconn)
objCmd.Parameters.AddWithValue("@poNum", strPoRMA)
objCmd.Parameters.AddWithValue("@calCompleteDate", strDate)
objCmd.Parameters.AddWithValue("@calCompleteTech", strtech)
objCmd.Parameters.AddWithValue("@cameraSer", strcs)
objCmd.ExecuteNonQuery()
End Using
'Write to up2DateTravelers Table
strSQL = "UPDATE up2DateTravelers SET poRMANum = @poRMANum, calCompleteDate = @calCompleteDate, calCompleteTech = @calCompleteTech WHERE cameraSer=@cameraSer"
Using objCmd As New OleDbCommand(strSQL, dbconn)
objCmd.Parameters.AddWithValue("@poRMANum", strPoRMA)
objCmd.Parameters.AddWithValue("@calCompleteDate", strDate)
objCmd.Parameters.AddWithValue("@calCompleteTech", strtech)
objCmd.Parameters.AddWithValue("@cameraSer", strcs)
objCmd.ExecuteNonQuery()
End Using