Public Cn as adodb.connection
      sub main
          frmLogin.show 1
      end sub    在FrmLogin 中有两个文本框分别是用户名(TxtUser)、密码(txtPwd)和登陆(CmdLogin)和退出按钮(CmdExit)
sub    Form_Load()
       set cn=new adodb.connection
       cn.open connstring ' connstring参考Msdn中ado
end sub
     sub CmdLogin_click()
           dim Rs as new adodb.recordset
           if (trim(txtUser.text)<>"") then
               Rs.open "select UserName,Pwd From sysUser where UserName='" & trim(txtUser.text) & "' ,cn,3,3
           if (not Rs.eof and Rs.bof) then
                 if txtPwd.text <>rs.("Pwd").value then
                     Msgbox "Error Pwd"  '提示错误
                 else
                       FrmMain.show 1  '显示主窗口                 end if
           else
                 strsql="Insert into sysUser (UserName,Pwd) values('" & trim(txtUser.text) & "','" & TxtPwd.text & "' "
               cn.execute strsql
                   FrmMain.show 1 '显示主窗口
           end if
           end if
     end sub
这是简单的例子。

解决方案 »

  1.   

    还有就是那些text控件和command控件怎么设置阿,大哥最好能帮我做一个最简单的例子给我看看,谢谢了,或者哪里有,告诉我也行
      

  2.   

    我以前编的一个程序的程序段:Private Sub Command1_Click()
       Dim SQLstr As String
       Dim msgstring As String
       Dim rs As New ADODB.Recordset
       
       If Text1(0).Text = "" Or Text1(1).Text = "" Then
          MsgBox "必须输入操作员编号和口令!!!", 48, "错误"
          Text1(0).SetFocus
       Else
          SQLstr = "select * from userID where operatorNo='" & Trim(Text1(0).Text) & "'"
          Set rs = ExecuteSQL(SQLstr, msgstring)
          If rs.EOF = True Then
             MsgBox "操作员编码输入错误!!!", 48, "错误"
             Text1(0).Text = ""
             Text1(1).Text = ""
             Text1(0).SetFocus
          Else
             If Trim(rs.Fields(2)) = Trim(Text1(1).Text) Then
                bh = Text1(0).Text
                Load MDIBookMIs
                MDIBookMIs.Show
                Unload login
                rs.Close
                Set rs = Nothing
             Else
                MsgBox "密码输入错误!!!", 48, "错误"
                Text1(1).Text = ""
                Text1(1).SetFocus
             End If
          End If
       End If
       
    End Sub
      

  3.   

    VERSION 5.00
    Begin VB.Form frmLogin 
       BorderStyle     =   1  'Fixed Single
       Caption         =   "人员登录"
       ClientHeight    =   1410
       ClientLeft      =   2865
       ClientTop       =   3555
       ClientWidth     =   3570
       Icon            =   "frmLogin.frx":0000
       KeyPreview      =   -1  'True
       LinkTopic       =   "Form1"
       LockControls    =   -1  'True
       MaxButton       =   0   'False
       MinButton       =   0   'False
       ScaleHeight     =   1410
       ScaleWidth      =   3570
       Begin VB.CommandButton Command2 
          Caption         =   "取 消"
          Height          =   345
          Left            =   2550
          TabIndex        =   5
          Top             =   960
          Width           =   885
       End
       Begin VB.CommandButton Command1 
          Caption         =   "确 定"
          Height          =   345
          Left            =   1680
          TabIndex        =   4
          Top             =   960
          Width           =   885
       End
       Begin VB.TextBox Text1 
          Alignment       =   2  'Center
          BeginProperty Font 
             Name            =   "宋体"
             Size            =   10.5
             Charset         =   134
             Weight          =   400
             Underline       =   0   'False
             Italic          =   0   'False
             Strikethrough   =   0   'False
          EndProperty
          Height          =   315
          IMEMode         =   3  'DISABLE
          Index           =   1
          Left            =   1890
          PasswordChar    =   "*"
          TabIndex        =   3
          Top             =   510
          Width           =   1515
       End
       Begin VB.TextBox Text1 
          Alignment       =   2  'Center
          BeginProperty Font 
             Name            =   "宋体"
             Size            =   10.5
             Charset         =   134
             Weight          =   400
             Underline       =   0   'False
             Italic          =   0   'False
             Strikethrough   =   0   'False
          EndProperty
          Height          =   315
          Index           =   0
          Left            =   1890
          TabIndex        =   1
          Top             =   120
          Width           =   1515
       End
       Begin VB.Image Image1 
          Height          =   630
          Left            =   90
          Picture         =   "frmLogin.frx":0442
          Top             =   60
          Width           =   630
       End
       Begin VB.Label Label2 
          AutoSize        =   -1  'True
          Caption         =   "用户口令:"
          Height          =   180
          Left            =   870
          TabIndex        =   2
          Top             =   615
          Width           =   900
       End
       Begin VB.Label Label1 
          AutoSize        =   -1  'True
          Caption         =   "用户名称:"
          Height          =   180
          Left            =   870
          TabIndex        =   0
          Top             =   225
          Width           =   900
       End
    End
    Attribute VB_Name = "frmLogin"
    Attribute VB_GlobalNameSpace = False
    Attribute VB_Creatable = False
    Attribute VB_PredeclaredId = True
    Attribute VB_Exposed = False
      

  4.   

    Private Sub Command1_Click()
        If Text1(0).Text = "" Then
            MsgBox "请输入用户名!", 64, cProgramName
        Else
            With Rs_Dm_Level
                If .State = adStateOpen Then
                    .Close
                End If
                .ActiveConnection = adoCN
                .CursorLocation = adUseClient
                .CursorType = adOpenDynamic
                .LockType = adLockBatchOptimistic
                .Source = "SELECT * FROM Dm_Level order by code"
                .Open
            End With
            With RsUsers
                If .State = adStateOpen Then
                    .Close
                End If
                .ActiveConnection = adoCN
                .CursorLocation = adUseClient
                .CursorType = adOpenDynamic
                .LockType = adLockBatchOptimistic
                .Source = "SELECT * FROM Dm_Users WHERE staffname='" & Trim(Text1(0).Text) & "' and password='" & Text1(1).Text & "'"
                .Open
                If .RecordCount = 1 Then
                    If .Fields("already") = True Then
                        MsgBox "此用户已在工作站[" & Trim(.Fields("comuser")) & "]上登录,请重新输入!", 16, cProgramName
                        .Close
                        Text1(0).SetFocus
                        Text1(0).Text = ""
                        Text1(1).Text = ""
                    Else
                        cDeptCode = .Fields("deptcode")
                        cStaffCode = .Fields("staffcode")
                        cStaffName = .Fields("staffname")
                        cUserLevelXYX = .Fields("xyxlevel")
                        If Not IsNull(.Fields("levelcode")) Then
                            Rs_Dm_Level.Find "code='" & Trim(.Fields("levelcode")) & "'"
                            If Not Rs_Dm_Level.EOF Then
                                cUserLevel = Rs_Dm_Level.Fields("namec")
                            Else
                                cUserLevel = ""
                            End If
                        Else
                            cUserLevel = ""
                        End If
                  '      .Fields("already") = true
                        .Fields("comuser") = cComputerName
                        .Fields("logondate") = Now
                        .Update
                        .UpdateBatch
                        .Close
                        With RsDept
                            If .State = adStateOpen Then
                                .Close
                            End If
                            .ActiveConnection = adoCN
                            .CursorLocation = adUseClient
                            .CursorType = adOpenDynamic
                            .LockType = adLockBatchOptimistic
                            .Source = "SELECT * FROM Dm_Dept WHERE deptcode='" & cDeptCode & "'"
                            .Open
                            If .RecordCount > 0 Then
                                If Not IsNull(.Fields("contract")) Then
                                    cBDeptCode = .Fields("contract")
                                Else
                                    cBDeptCode = ""
                                End If
                                cDeptName = .Fields("deptname")
                            End If
                            .Close
                        End With
                        Unload Me
                        F_Main.Show
                    End If
                Else
                    MsgBox "您的口令不正确,请重新输入!", 16, cProgramName
                    Text1(1).SetFocus
                    Text1(1).SelStart = 0
                    Text1(1).SelLength = Len(Text1(1).Text)
                End If
            End With
        End If
    End SubPrivate Sub Command2_Click()
        End
    End SubPrivate Sub Form_KeyPress(KeyAscii As Integer)
        If KeyAscii = 27 Then
            End
        End If
    End SubPrivate Sub Form_Load()
        Dim cForm As String
        cForm = FormSet(Me, 5)
    End SubPrivate Sub Text1_GotFocus(Index As Integer)
        Text1(Index).SelStart = 0
        Text1(Index).SelLength = Len(Text1(Index).Text)
    End SubPrivate Sub Text1_KeyPress(Index As Integer, KeyAscii As Integer)
        If KeyAscii = 13 And Text1(Index).Text <> "" Then
            SendKeys "{tab}"
        End If
    End SubPrivate Sub Text1_LostFocus(Index As Integer)
        Select Case Index
            Case 0
                With RsUsers
                    If .State = adStateOpen Then
                        .Close
                    End If
                    .ActiveConnection = adoCN
                    .CursorLocation = adUseClient
                    .CursorType = adOpenDynamic
                    .LockType = adLockBatchOptimistic
                    .Source = "SELECT * FROM DM_USERS WHERE staffcode='" & Trim(Text1(0).Text) & "' or staffname='" & Trim(Text1(0).Text) & "' or bcode='" & Trim(Text1(0).Text) & "'"
                    .Open
                    If Not (.BOF And .EOF) Then
                        Text1(0).Text = Trim(.Fields("staffname")) & ""
                        Text1(1).Text = ""
                        Text1(1).SelStart = 0
                        Text1(1).SelLength = Len(Text1(1).Text)
                    Else
                        If Text1(0).Text <> "" Then
                            MsgBox "错误的用户名!", 16, cProgramName
                            Text1(0).Text = ""
                            Text1(0).SetFocus
                        End If
                    End If
                    .Close
                End With
        End Select
    End Sub