樓上的道友的方法中:
sSQL="update table set field=20"
要改為:
sSQL="update table set field1=20,...,field2=20 where id=3"
#但是如果你的資料庫中沒有記錄3就會出錯。
還有一種方法,用recordset來處理:
dim cn as new adodb.connection
dim rs as new adodb.recordset
rs.open "select * from 資料庫 where id=3",adOpenDynamic, adLockOptimistic, -1
if not rs.eof then
rs("你要修改的欄位1")="具體值1"
#...
rs("你要修改的欄位n")="具體值n"
#rs.update
end if
rs.close
我在設計時一般都是這樣,雖然麻煩些但基本上不會出錯,還有就是你在設計資料庫時最好要加上一個ID,這樣就可以保證資料的唯一性。
Private Sub Command1_Click() '借書'
Dim jc As Boolean
#jc = False
Adodc1.Recordset.MoveFirst
For i = 1 To Adodc1.Recordset.RecordCount
##If Text1.Text = Adodc1.Recordset.Fields(1).Value And Adodc1.Recordset.Fields(4).Value = True ThenAdodc1.Recordset.Fields(4).Value = Not Adodc1.Recordset.Fields(4).Value
Adodc1.Recordset.Update
jc = True
End If
Adodc1.Recordset.MoveNext
Next i
If jc Then MsgBox "借出成功" Else MsgBox "圖書已借出或圖書不存在"
End Sub
Private Sub Command2_Click() '還書'
Dim hs As Boolean
hs = False
Adodc1.Recordset.MoveFirst
For i = 1 To Adodc1.Recordset.RecordCount
##If Text1.Text = Adodc1.Recordset.Fields(1).Value And Adodc1.Recordset.Fields(4).Value = False Then
Adodc1.Recordset.Fields(4).Value = Not Adodc1.Recordset.Fields(4).Value
Adodc1.Recordset.Update
jc = True
End If
Adodc1.Recordset.MoveNext
Next i
If jc Then MsgBox "歸還成功" Else MsgBox "圖書已在庫或圖書不存在"
End Sub
Private Sub Form_Load()
Text1.Text = ""
Adodc1.Visible = False
Adodc1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\mybook.mdb;Persist Security Info =false"
Adodc1.CommandType = adCmdText
Adodc1.RecordSource = "select * from 圖書"
Adodc1.Refresh
End Sub
##########以上是如何在VB中更新資料庫記錄的值的詳細內容。更多資訊請關注PHP中文網其他相關文章!