我想做一个用户登陆窗口测试,首先判断用户是否存在,如果存在则提示用户存在,如果无则提示无此用户
可是查到数据库中的数据如果有就很正常,没有的时候就报错,请高手指点。
关键代码如下:
Dim b As Variant
b = Text1.Text
Set rs = New ADODB.Recordset
rs.Open "select * from login where user=""" & b & """", conn, adOpenDynamic, adLockOptimistic
If Text1.Text = rs!user Then
Text2.Text = "该用户存在"
Unload Me
Else
Text2.Text = "无此用户"
End If

解决方案 »

  1.   

    没有的时候 rs =空 
      If rs.Count=0 then 
        Text2.Text = "无此用户"
        Exit Sub
      Else
        Text2.Text = "该用户存在"
      Endif
      

  2.   

    用rs.eof来判断是否有数据就可以了
      

  3.   

    If rs.EOF then 
        Text2.Text = "无此用户"
        Exit Sub
      Else
        Text2.Text = "该用户存在"
      Endif
      

  4.   

    不好意思
    If rs.RecordCount=0 then 
        Text2.Text = "无此用户"
        Exit Sub
      Else
        Text2.Text = "该用户存在"
      Endif还有user=""" & b & """" ==>user="'" & b & "'" 
     dim b as string 
      

  5.   

    Dim b As Variant
    b = trim(Text1.Text)
    if instr(1,b,"'")>0 then
        msgbox "不能含有 “ '“ 此符号!"
        exit sub
    end if
    Set rs = New ADODB.Recordset
    rs.Open "select * from login where user='" & b & "'", conn, adOpenDynamic, adLockOptimistic
    If not rs.eof Then
        Text2.Text = "该用户存在"
        Unload Me
    Else
        Text2.Text = "无此用户"
    End If
      

  6.   

    其实道理很简单就是判断你使用sql语句得到的Recordset的记录集中是否有记录
    即当
       rs.recordcount>0 
    表示,数据库中有该记录,继而还可以判断记录的个数
    当然使用
       rs.eof and rs.bof
    也可以判断rs是否为空此外需要注意的是,你在处理用户名和密码是需要对此进行处理
    即需要将其中的'号处理,
    使用
    Replace(text1.Text, "'", "''")函数将一个引号变成两个引号
    这样做是为了避免sql语句的漏洞
      

  7.   

    Dim b As Variant
    b = Text1.Text
    Set rs = New ADODB.Recordset
    rs.Open "select * from login where user=""" & b & """", conn, adOpenDynamic, adLockOptimistic
    if rs.recordcount>=1 then
     Text1.Text = rs!user 
    Text2.Text = "该用户存在"else
    text2 = "无此用户"
    exit sub
    End If
      

  8.   

    dim rs As ADODB.Recordset
         Set rsuser = New ADODB.Recordset
          rsuser.Open "select * from login  where user=""" & b & """" , conn, adOpenStatic, adLockOptimistic
         
         If rsuser.RecordCount > 0 Then
            Text2.Text = "该用户存在"
            Unload Me
         Else
            Text2.Text = "无此用户"
            Exit sub
         Endif