VB中关于Recordset.update 的问题。求教!

2024-12-03 03:37:35
推荐回答(5个)
回答1:

你的这个程序看起来头大:
1、CreateRecordset中的这条语句Set theRecordSet.ActiveConnection = Nothing将theRecordSet关闭了,那么rst就是Nothing,根本无法调用,这是你问题的根本。
2、rst.MoveFirst是多余的,ADODB.Recordset在Open后就在First处,不用再Move,除非你用了Move、MoveNext、MoveLast等方法Move了游标。
3、批量的修改建议用SQL的Update语句,比如:rst.open "update password set 。。。 where 。。。
4、你在MsgBox后直接Exit Sub了,rst没有Close,这是一个很不好的习惯,另外直接Exit Sub了,frmLogin.MousePointer也没有改为0。
5、If Err.Number <> 0 Then的判断是否应该放在CreateRecordset,因为你每次CreateRecordset后都要判断是否连接成功的。
6、If txtNPassword1.Text = txtNPassword2.Text Then,该判断应放在ADODB.Recordset的Open即CreateRecordset之前,如果俩密码不一致,直接提示,一致后再进行数据库的操作。
7、strSQL = "select * from password where password001 = '" & txtUserID.Text & "'",如果 txtUserID.Text 中包含有单引号,该语句将出错。 txtUserID.Text 是用户输入的,是你不可控的:Replace(txtUserID.Text ,"'","")。

回答2:

Set theRecordSet.ActiveConnection = Nothing 这一句为将记录集设定为离线记录集,不支持更新,如果想更新在
strSQL = "select * from password where password001 = '" & txtUserID.Text & "'"
Set rst = CreateRecordset(conn, strSQL)
此处加上:Set theRecordSet.ActiveConnection = conn 这样应该就可以了

回答3:

你的程序编写习惯不大好,容易从思路上乱
Private Sub cmdOK_Click()
Dim strSQL As String
Dim rst As ADODB.Recordset
frmLogin.MousePointer = 11
strSQL = "select * from password where password001 = '" & txtUserID.Text & "'"
Set rst = CreateRecordset(conn, strSQL)
If Err.Number <> 0 Then
frmLogin.MousePointer = 0
MsgBox "修改密码时,开启数据库失败!" & Chr(13) & "错误描述:" & Err.Description
Exit Sub
End If
If rst.RecordCount > 0 Then
rst.MoveFirst
While Not rst.EOF

' MsgBox rst.Fields("password002").Value
If rst.Fields("password002").Value = txtOPassword.Text Then
If txtNPassword1.Text = txtNPassword2.Text Then
rst.Fields("password002").Value = txtNPassword1.Text
Else
frmLogin.MousePointer = 0
MsgBox "请确认新密码是否相同!!!", 48, "警告"
Exit Sub
End If
Else
frmLogin.MousePointer = 0
MsgBox "密码错误!!!" & Chr(13) & "请注意大小写!!!", 48, "警告"
Exit Sub
End If
在这里加一句rst.Updata
rst.MoveNext
Wend
' rst.UpdateBatch 把这一句删掉,试试
Else
frmLogin.MousePointer = 0
MsgBox "系统内没有你的帐号!!!", 48, "警告"
Exit Sub
End If

frmLogin.MousePointer = 0

MsgBox "更新成功!!!", 48, "提示"
frmEditPassword.Hide
frmMain.Show
End Sub

Public Function CreateRecordset(ByRef aConn As ADODB.Connection, ByVal aSQLString As String) As ADODB.Recordset
On Error GoTo suberrhdl
Dim theRecordSet As ADODB.Recordset
If UCase(Trim(TypeName(aConn))) <> "CONNECTION" Then
Exit Function
End If
Set theRecordSet = New ADODB.Recordset
theRecordSet.CursorLocation = adUseClient
Set theRecordSet.ActiveConnection = aConn
' theRecordSet.CursorType = adOpenStatic
theRecordSet.CursorType = adUseClient
' theRecordSet.LockType = adLockPessimistic
theRecordSet.LockType = adLockBatchOptimistic
theRecordSet.Source = aSQLString
theRecordSet.Open
Set theRecordSet.ActiveConnection = Nothing 记录集刚刚打开为什么要关闭连接?
Set CreateRecordset = theRecordSet
Exit Function
suberrhdl:
If Not aConn Is Nothing Then
Set aConn = Nothing
End If
End Function
问题补充:
当我这样写道时候就可以更新
Dim rst As New ADODB.Recordset
frmLogin.MousePointer = 11
strSQL = "select * from cppassword where cppassword001 = '" & txtUserID.Text & "'"
‘ Set rst = CreateRecordset(conn, strSQL)
rst.Open strSQL, conn, 3, 4, adCmdText

回答4:

在rst.Fields("password002").Value = txtNPassword1.Text
前加上
rst.Recordset.Edit
rst.Fields("password002").Value = txtNPassword1.Text

回答5:

.CursorLocation = adUseClient 这个在开始的时候就该告诉计算机