Private Sub modifySure_Click()
  Dim rs As New ADODB.Recordset
  Dim rs2 As New ADODB.Recordset
  Dim sql As String
  Dim sql2 As String  If Trim(username.Text = "") Then
    MsgBox "没有输入用户名,请重新输入!", vbOKOnly + vbExclamation, "警告"   '弹出的对话框中只有一个确定按钮和一个警告的图标
    username.SetFocus
  
    sql = "select * from user_info where user = '" & username.Text & "'and pw = '" & oldPw.Text & "'"
    Set rs = TransactSQL(sql)
  Else
    If rs.EOF = True Then
      MsgBox "用户密码不匹配,无权更改!", vbOKOnly + vbExclamation, "警告"
      username.SetFocus
    Else
      If newPw1.Text <> newPw2.Text Then
        MsgBox "两次输入密码不一致!", vbOKOnly
      Else
        sql2 = "update user_info set pw = '" & newPw1.Text & "' where user = '" & username.Text & "'"
        sql2 = "select * from user_info where user ='" & username.Text & "'"
        Set rs2 = TransactSQL(sql)
        rs2.Fields(1) = newPw1.Text
        rs2.Update
        rs2.Close
        MsgBox "密码修改成功!", vbOKOnly, "修改密码"
        Me.Hide
      End If
    End If
  End If
End Sub
这是一个修改密码的窗体代码,每次运行到if rs.eof=true then这句的时候就会弹出错误提示,请问各位老师这是什么原因,该怎么修改呢?谢谢了!也许这里面还有很多错误,我是新手,麻烦各位了

解决方案 »

  1.   

    TransactSQL 函数里面配置有问题。一般是数据库路径不对,未找到,所以数据连接就关闭了。所有当RS调用的时候就会错误。最好把TransactSQL函数帖出来看看。
      

  2.   

    谢谢您了,这里是transactSQL函数,不过在别的窗体运行都没问题,就只有这个窗体出问题了
    Public Function TransactSQL(ByVal sql As String) As ADODB.Recordset '执行SQL语句,并返回记录集对象
      Dim cont As ADODB.Connection '声明一个连接
      Dim rs As ADODB.Recordset '声明一个数据集对象
      
      Dim strConnection As String
      Dim strArray() As String
      
      Set cont = New ADODB.Connection '创建连接
      Set rs = New ADODB.Recordset
      
      On Error GoTo transactsql_error '异常处理
        strConnection = "provider = microsoft.jet.oledb.4.0;data source = " & App.Path & "\magazineMIS.mdb" '返回一个数据库连接
        strArray = Split(sql) '用split函数产生一个包含各个子串的数组
        cont.Open strConnection '打开连接
        If StrComp(UCase$(strArray(0)), "select", vbTextCompare) = 0 Then
          rs.Open Trim$(sql), cont, adOpenKeyset, adLockOptimistic
          Set TransactSQL = rs '返回记录集对象
          iflag = 1
        Else
          cont.Execute sql '执行查询语句
          iflag = 1
        End If
    transactsql_exit:
        Set rs = Nothing
        Set cont = Nothing
        Exit Function
    transactsql_error:
        MsgBox "查询错误:" & Err.Description
        iflag = 2
        Resume transactsql_exit
    End Function
      

  3.   

    仔细看了一下你的代码好象IF有问题啊    sql = "select * from user_info where user = '" & username.Text & "'and pw = '" & oldPw.Text & "'" 
        Set rs = TransactSQL(sql) 
      Else改成  Else
        sql = "select * from user_info where user = '" & username.Text & "'and pw = '" & oldPw.Text & "'"
        Set rs = TransactSQL(sql)
      

  4.   

    我觉得是你的sql有问题sql = "select * from user_info where user = '" & username.Text & "'and pw = '" & oldPw.Text & "'"
    改一下
    sql = "select * from user_info where user = '" & trim(username.Text) & "'and pw = '" & (oldPw.Text)) & "'"
    此外你也可以把“If rs.EOF = True Then”改一下if rs.recordcount=0 then
    再试试,如果还不行.....我还认为是传进去的sql语句有问题,好好检查一下字段的名称是否拼错?
      

  5.   

    手误
    sql = "select * from user_info where user = '" & trim(username.Text) & "'and pw = '" & trim(oldPw.Text) & "'"