小弟最近用VB6.0做了个登陆界面,控件有两个文本框:TEXT1(输入工号),TEXT2(输入密码);两个Command按钮,command1(登陆),command2(离开) 
连接一个名为Data.mdb的数据库,Data中有一张名为login的表,表的结构如下:  user_id     user_gh      user_xm    user_jb    user_mm    1          001          张三       管理员        123 
   
   2          002          李四       操作员        456 
 
已实现的功能: 
        在TEXT1中输入工号,(TEXT1显示的是号码),在TEXT2中输入密码登陆。(我用的是ADODB,没有加ADO控件) 
希望实现的功能: 
    在TEXT1中输入工号,按回车后,TEXT1中显示的是用户的姓名(user_xm相应的字段),然后在TEXT2中输入密码登陆。 要求:
     1、尽量用代码完成,少用控件。
      2、生成的程序与ACCESS数据库在同一文件目录下,不受路径限制。
      3、请将需要引用到的控件列明。(新手,莫怪!)
      4、最好能说明一下每段代码实现的功能!
谢谢!!!!

解决方案 »

  1.   

    sConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\db1.mdb"
        
        Dim adoConn As ADODB.Connection
        Dim adoRS As ADODB.Recordset
        Dim SqlQuery As String
            
        Set adoConn = New ADODB.Connection
        adoConn.Open sConnString
            
        Set adoRS = New ADODB.Recordset
        SqlQuery = "select user_xm from login where user_gh = 1"
        Set adoRS = adoConn.Execute(SqlQuery)
        
        MsgBox adoRS.Fields(0).Value
      

  2.   

    Option Explicit
        Dim cn As New ADODB.Connection '定义数据库的连接
        Dim rs As New ADODB.Recordset
        Dim sql As String
        Dim password As StringPrivate Sub Form_Load()
        Text1.TabIndex = 0
        Text1 = ""
        Text2 = ""
    End SubPrivate Sub Text1_KeyPress(KeyAscii As Integer)
        If KeyAscii = 13 Then
            sql = "select * from login where user_gh='" & Text1.Text & "'"
            cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\data.mdb;Persist Security Info=False"
            cn.Open
            rs.CursorLocation = adUseClient
            rs.Open sql, cn, adOpenDynamic, adLockOptimistic
            Text1 = rs.Fields(2)
            password = rs.Fields(4)
            Text2.SetFocus
        End If
    End SubPrivate Sub Text2_KeyPress(KeyAscii As Integer)
        If KeyAscii = 13 And password = Text2.Text Then
            MsgBox ("登录成功!")
            cn.Close
        End If
    End Sub
      

  3.   

    1.可以直接用ADO,不需要控件,直接访问access
    2.GetModuleFileName() 来获得当前exe的路径,截取出当前目录路径,然后根据access也在当前目录,拼接access路径,这样就可以不受目录路径,搬到哪里都可以
      

  4.   

    我在程序中加入了2楼的朋友的代码,
    调试时,"cn.open"这段代码显黄色,请问怎么解决?
    我的ACCESS设置了密码,密码是:123456.
      

  5.   

    '在工程中引用:Microsoft ActiveX Data Objects 2.5 Libraryprivate conn as new adodb.connection
    private rs   as new adodb.recordset
    private strsql    as stringPrivate Sub Form_Load()
    '与数据库建立连接
    on error goto ErrHandle
        Text1.TabIndex = 0
        Text1 = ""
        Text2 = ""
        with conn    
            If .State = adStateOpen Then .Close
            .ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source="& App.path & "\data.mdb;Jet OLEDB:Database Password=123456"
            .CommandTimeout = 300
            .Open
        end with
        exit sub
    '错误处理
    ErrHaldle:
        msgbox "与数据库连接失败,错误原因为:" & err.description,vbexclamation,"提示"
        exit sub
    End SubPrivate Sub txtcUserName_KeyDown(KeyCode As Integer, Shift As Integer)
    on error goto ErrHandle
        '按下了回车键
        if keycode=vbkeyreturn then
            if trim(text1.text)="" then
                msgbox "请输入工号!",48,"提示"
                text1.setfocus
                exit sub
            else
                '在text1输入框中可能会输入工号或姓名,根据工号或姓名来判断
                strsql=" select user_xm from login where (user_gh='"& trim(text1.text) &"' or user_xm='"& trim(text1.text) &"')"
                if rs.state<>adstateclosed then rs.close
                rs.open strsql,conn,adopenkeyset,adlockreadonly
                if rs.recordcount=0 then
                    msgbox "输入的工号或姓名不存在,请重新输入!",48,"提示"
                    rs.close
                    text1.setfocus
                    exit sub
                else
                    text1.text=rs!user_xm & ""
                end if
                rs.close
            end if
        end if
        exit sub
    '错误处理
    ErrHandle:
        if rs.state<>adstateclosed then rs.close
        msgbox "操作失败,错误原因为:" & err.description,vbexclamation,"提示"
        exit sub            
    End Sub
      

  6.   

    你既然已经实现了登陆功能,现在只需要显示一个名字而已
    仅需要在text1的keydown事件中做一个取名字的过程就行了
    也不需要重新连接access,因为你已经连接了吧
    当然要看你的连接变量是不是全局变量了
      

  7.   

    楼上的说得对。
    以下是我的整段代码,请帮忙修改一下。谢谢!Private Sub Command1_Click()
    Dim ConStr As String
       If Text1.Text = "" Then
          MsgBox "请输入用户名!", vbOKOnly + vbExclamation, "登陆错误"
          Text1.SetFocus
          Exit Sub
       End If
        
       Set cn = New ADODB.Connection
       Set rs = New ADODB.Recordset
       ConStr = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & App.Path & "\Data.Mdb" & " ;Jet OLEDB:Database password=123456"
       cn.Open ConStr
       cn.CursorLocation = adUseServer
       rs.Open "Select * from login", cn, adOpenKeyset, adLockPessimistic
       If rs.RecordCount > 0 Then
          If Text1.Text <> "" Then
             Set rs1 = New ADODB.Recordset
             Dim TextUserName
             TextUserName = Left(Text1Text, 128)
             rs1.Open "Select * From login Where user_gh= '" & TextUserName & "'", cn, adOpenKeyset, adLockPessimistic
             If rs1.RecordCount > 0 Then
                Tex1.Text = Left(Text1.Text, 128)
                Text2.SetFocus
                If Text2 <> "" Then
     If rs1.Fields("user_gh") = TextUserName And rs1.Fields("user_mm") = Text2.Text Then
     main.Show
     Unload Me
     Else
     MsgBox "密码错误!", vbExclamation + vbOKCancel, "登陆错误"
    Text1.Text = ""
    Text2 = ""
     Text1.SetFocus
     End If
     Else
     MsgBox "请输入密码!", vbExclamation + vbOKCancel, "登陆错误"
    End If
     Else
     MsgBox "沒有该用戶信息,请核对!", vbExclamation + vbOKCancel, "登陆错误"
     Text1.Text = ""
     Text2 = ""
     Text1.SetFocus
     Exit Sub
    End If
    rs.Close
    End If
    End If
    End SubPrivate Sub Command2_Click()
    Unload Me
    End SubPrivate Sub Form_Load()
    If App.PrevInstance Then
        MsgBox "不要重复开启程序"
    End 
    End If
    End SubPrivate Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = 13 Then
    Text2.SetFocus
    End If
    End SubPrivate Sub Text2_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = 13 Then
    Command1.SetFocus
    End If
    End SubPS:
        其中,main_form是登陆后所显示的主界面。
      

  8.   

    我的目的是输入工号后,按回车键,TEXT1中显示的是操作员的姓名,然后在TEXT2中输入密码,登陆.
    有办法吗?
      

  9.   

    Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer) 
    If KeyCode = 13 Then 
    ''工号得到名字
    Set cn = New ADODB.Connection 
      Set rs = New ADODB.Recordset 
      ConStr = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & App.Path & "\Data.Mdb" & " ;Jet OLEDB:Database password=123456" 
      cn.Open ConStr 
      cn.CursorLocation = adUseServer 
      rs.Open "Select * from login", cn, adOpenKeyset, adLockPessimistic 
      If rs.RecordCount > 0 Then 
            Set rs1 = New ADODB.Recordset 
            Dim TextUserName 
            TextUserName = Left(Text1Text, 128) 
            rs1.Open "Select * From login Where user_gh= '" & TextUserName & "'", cn, adOpenKeyset, adLockPessimistic 
            If rs1.RecordCount > 0 Then 
                Tex1.Text = rs1("user_xm") 
                Text2.SetFocus 
            Else 
                MsgBox "沒有该用戶信息,请核对!", vbExclamation + vbOKCancel, "登陆错误" 
                Text1.Text = "" 
                Text2 = "" 
                Text1.SetFocus 
                Exit Sub 
            End If 
            rs.Close 
       End If 
    End If command1事件里再换成用姓名验证密码
    rs1.Open "Select * From login Where user_xm= '" & TextUserName & "'", cn, adOpenKeyset, adLockPessimistic 
    End Sub 
      

  10.   

    楼上正解.输入工号和回车符显示名字,必须连接数据库.整个代码在TEXT1_KEYDOWN事件里实现.
    判断是否输入了回车符.再在数据库里比对工号,取出相应的姓名.再在登录按钮事件里验证姓名和密码.
      

  11.   


    '在工程中引用:Microsoft ActiveX Data Objects 2.x Library
    '窗体上放一个 ComboBox,Style 属性设置为 Simple Combo,不要拉宽高度,使形如 TextBox,取代 Text1Dim cn As ADODB.Connection, rs As ADODB.Recordset'预存用户工号-姓名对照表
    Private Sub Form_Load()
        Set cn = New ADODB.Connection 
        cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\Data.Mdb" & " ;Jet OLEDB:Database password=123456" 
        Set rs = cn.Execute("Select user_gh,user_xm from login")
        Do Until rs.EOF
            Combo1.AddItem rs!user_xm
            Combo1.ItemData(Combo1.NewIndex) = Val(rs!user_gh)
            rs.MoveNext
        Loop
        set rs = Nothing
        cn.Close
        Set cn = Nothing
        Combo1.Text = ""
    End Sub'查表显示
    Private Sub Combo1_KeyPress(KeyAscii As Integer)
    Dim i As Integer    If KeyAscii = 13 And IsNumeric(Combo1.Text) Then
            For i = 0 To Combo1.ListCount - 1
                If Combo1.ItemData(i) = Val(Combo1.Text) Then
                    Combo1.Text = Combo1.List(i)
                    Text2.SetFocus
                    Exit For
                End If
            Next i
        End If
    End Sub