楼上的道友的方法中:
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 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 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中文网其他相关文章!