我建立了一个数据库,里面有“用户名”和“密码”两个字段。我想实现的效果是:数据库里存在的用户可以登陆,否则不能登陆。代码如下:
Private Sub Command1_Click()
Dim a As String
Dim b As String
a = Text1.Text
b = Text2.Text
Data1.RecordSource = "select * from 用户资料 where 用户名 = '" & a & " ' and  用户密码 = '" & b & " ' "
If Data1.Recordset.EOF = True Then
    a = MsgBox("输入有误!你无权登陆!", 0 + 48 + 0, "注意! ")
    Data1.RecordSource = "用户资料"
    Data1.Refresh
    Exit Sub
Else
    Formmain.Show
    Unload Formland
End If
End Sub
但是不管输入什么用户名或密码名都可以看到Formmain窗体,请问为什么?

解决方案 »

  1.   

    Data1.Recordset.EOF = True改成Data1.Recordset.RecordCount=0看看最好檢查一下是否Data1.Recordset.RecordCount=0???弄不好根本就是資料錯誤
      

  2.   

    If Data1.Recordset.RecordCount >0 Then
        Formmain.Show
        Unload Formland
    Else
        a = MsgBox("输入有误!你无权登陆!", 0 + 48 + 0, "注意! ")
        Data1.RecordSource = "用户资料"
        Data1.Refresh
        Exit Sub
    End If
     
      

  3.   

    还是不行,不管用户名和密码是否正确,都出现formmain窗体。
      

  4.   

    1、建议你采用Data1.Recordset.RecordCount >0
    2、建议你跟踪程序看得到的Recordset有什么?有否产生记录?
      

  5.   

    你把程序發給我看看。[email protected]我懶得和你分析代碼了
      

  6.   

    如果你在9點之前沒有發,我就沒有辦法了你判斷Data1.Recordset.RecordCount=0了就証明沒有然後exit sub怎麼還能 show?我估計你其它地方錯了,你不給看,就算了
      

  7.   

    to: daisy8675(莫依) 
    我已经把文件给你发过去了。请帮我看看。谢谢了!
      

  8.   

    無法載入 '0'
    行號 0: 無法載入檔案 C:\Documents and Settings\fft030735\桌面\新資料夾\Formmain.frm。你的這個窗體我根本沒有辦法載入,應該是你發過來的時候有問題,你是散發的。
    我要斷網了,沒有辦法給你看了,你請後面的朋友幫你看看吧另外:個人建議,少用data,用ado比較好
      

  9.   

    在自己机子上单步执行看一下。
    在If Data1.Recordset.RecordCount >0 Then之后程序是怎么走的??
      

  10.   

    Dim isuser As Boolean
    Dim pwd As String
    Dim uname As String
    Static connt As Integer
    uname = Trim(Text1.Text)
    pwd = Trim(Text2.Text)
    isuser = False
    Do While Not Data1.Recordset.EOF
       If uname = Data1.Recordset.Fields("user") Then
          If pwd = Data1.Recordset.Fields("password") Then
          Unload Me
          Formmain.Show
          Exit Sub
        Else
          If connt < 2 Then
               MsgBox "密码不正确!", vbExclamation + vbOKOnly, "错误"
               Text2.Text = ""
               Text2.SetFocus
           End If
           isuser = True
           Exit Do
         End If
       End If
           Data1.Recordset.MoveNext
       Loop
         connt = connt + 1
       If connt = 3 Then
         MsgBox "非法用户,不能使用此系统!", vbExclamation + vbOKOnly
         End
       End If
       If Not isuser Then
        MsgBox "无此用户,请重新登录!", vbExclamation + vbOKCancel
        Text1.Text = ""
        Text1.SetFocus
       End If
       Data1.Recordset.MoveFirst
    End Sub
      

  11.   

    to: lujianyu1189() 
    问题已经解决了,谢谢这位大哥(或大姐),小妹不胜感激! :-)