Private Sub cmdCancel_Click()  先调试一下数据库语句。
    Unload Me
End SubPrivate Sub cmdOK_Click()
    Dim txtSQL As String
    Dim mrc As ADODB.Recordset
    Dim MsgText As String    If Trim(Text1(0).Text) = "" Then
        MsgBox "请输入用户名称!", vbOKOnly + vbExclamation, "警告"
        Exit Sub
        Text1(0).SetFocus
    Else
        txtSQL = "select * from user_Info "
        Set mrc = Execute SQL (txtSQL, MsgText)
        While (mrc.EOF = False)
            If Trim(mrc.Fields(0)) = Trim(Text1(0)) Then
                MsgBox "用户已经存在,请重新输入用户名!", vbOKOnly + vbExclamation, "警告"
                Text1(0).SetFocus
                Text1(0).Text = ""
                Text1(1).Text = ""
                Text1(2).Text = ""
                Exit Sub
            Else
                mrc.MoveNext
            End If
        Wend
    End If
    
    If Trim(Text1(1).Text) <> Trim(Text1(2).Text) Then
        MsgBox "两次输入密码不一样,请确认!", vbOKOnly + vbExclamation, "警告"
        Text1(1).SetFocus
        Text1(1).Text = ""
        Text1(2).Text = ""
        Exit Sub
        
    Else
        If Text1(1).Text = "" Then
            MsgBox "密码不能为空!", vbOKOnly + vbExclamation, "警告"
            Text1(1).SetFocus
            Text1(1).Text = ""
            Text1(2).Text = ""
        Else
            mrc.AddNew
            mrc.Fields(0) = Trim(Text1(0).Text)
            mrc.Fields(1) = Trim(Text1(1).Text)
            mrc.Update
            mrc.Close
            Me.Hide
            MsgBox "添加用户成功!", vbOKOnly + vbExclamation, "添加用户"
        End If
    End If
        
        
        
End Sub运行时,老是提示While (mrc.EOF = False)这一行“实时错误‘91’:
对象变量或With块变量未设置。让我进行调试,可是调试不出来。想请问一下,是哪里出了问题?语言需不需要更改?怎么样修改?

解决方案 »

  1.   


    Execute SQL是什么?
    在While (mrc.EOF = False)前先判断mrc是否为nothing,
    例如:If mrc Is Nothing Then Exit Sub
      

  2.   

    ADO  步骤   
     工程-引用 Microsoft ActiveX Data Objects (选个高版本的) 
    Dim conn As New ADODB.Connection 'connection数据库联接 
    connstr = "DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" + App.Path + "\data.mdb" 
    conn.Open connstr Dim rs As New ADODB.Recordset'查询 
    SQL = "select * from users" 
    rs.Open SQL, conn, adOpenForwardOnly 
      

  3.   

    我用的是SQL Server数据库啊,你给我说的是ACCESS数据库,怎么用吗?
      

  4.   

    Private Sub cmdOK_Click() 
        Dim txtSQL As String 
        Dim mrc As new ADODB.Recordset 'modify
        Dim MsgText As String 
        
        'CathySun118 add
        Dim conn As New ADODB.Connection 'connection数据库联接 
         
        connstr = "Driver={sql server};server=servername;uid=sa;pwd=;database=databasename"
        conn.Open connstr  
        'end add
        If Trim(Text1(0).Text) = "" Then 
            MsgBox "请输入用户名称!", vbOKOnly + vbExclamation, "警告" 
            Exit Sub 
            Text1(0).SetFocus 
        Else 
            txtSQL = "select * from user_Info " 
            mrc.Open SQL, conn, adOpenForwardOnly  'modify
            
            While (mrc.EOF = False) 
                If Trim(mrc.Fields(0)) = Trim(Text1(0)) Then 
                    MsgBox "用户已经存在,请重新输入用户名!", vbOKOnly + vbExclamation, "警告" 
                    Text1(0).SetFocus 
                    Text1(0).Text = "" 
                    Text1(1).Text = "" 
                    Text1(2).Text = "" 
                    Exit Sub 
                Else 
                    mrc.MoveNext 
                End If 
            Wend 
        End If 
         
        If Trim(Text1(1).Text)  <> Trim(Text1(2).Text) Then 
            MsgBox "两次输入密码不一样,请确认!", vbOKOnly + vbExclamation, "警告" 
            Text1(1).SetFocus 
            Text1(1).Text = "" 
            Text1(2).Text = "" 
            Exit Sub 
             
        Else 
            If Text1(1).Text = "" Then 
                MsgBox "密码不能为空!", vbOKOnly + vbExclamation, "警告" 
                Text1(1).SetFocus 
                Text1(1).Text = "" 
                Text1(2).Text = "" 
            Else 
                mrc.AddNew 
                mrc.Fields(0) = Trim(Text1(0).Text) 
                mrc.Fields(1) = Trim(Text1(1).Text) 
                mrc.Update 
                mrc.Close 
                Me.Hide 
                MsgBox "添加用户成功!", vbOKOnly + vbExclamation, "添加用户" 
            End If 
        End If 
             
             
             
    End Sub 
      

  5.   

             
            While (mrc.EOF = False)  
                If Trim(mrc.Fields(0)) = Trim(Text1(0)) Then  
                    MsgBox "用户已经存在,请重新输入用户名!", vbOKOnly + vbExclamation, "警告"  
                    Text1(0).SetFocus  
                    Text1(0).Text = ""  
                    Text1(1).Text = ""  
                    Text1(2).Text = ""  
                    Exit Sub  
                Else  
                    mrc.MoveNext  
                End If  
            Wend  
    ------------------------------------------------
    直接用 mrc.findfirst 可以吧。