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.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代码如上。简单描述一下,功能是修改密码用的,在password 表中,一个帐号存在多条记录,密码是相同的,修改密码的话会同时更新该帐号所有的记录。
问题是这样的:执行上面的程序,数据库中的记录没有被更新。程序也不报错。求高人指点~~小弟刚开始学习VB。

解决方案 »

  1.   

    怎么没人回答啊,就那么难么??奇怪。
    还有个小问题,当我这样使用的时候: 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
    就能更新,求解啊!!!
      

  2.   

    1,password是系统保留字,用作表名或字段名时要用方括号
       select * from [password] where
    2,多行更新可以用一名SQL语句来实现:
       conn.execute " update [password] set password002='"& txtPassword1.text &"' where password001='"& txtUserId.text &"'"