VB登录界面代码
方法一:
VB登录界面代码
Option Explicit
Private Sub cmdCancel_Click()
Dim intResult As Integer
'请求用户确认是否真的退出系统登录
intResult = MsgBox("你选择了退出系统登录,退出将不能启动企业人事管理系统!" & vbcrlf_ & "是否真的退出?", vbYesNo, "登录验证")
If intResult = vbYes Then End
'根据用户选择结束应用程序
End Sub
Private Sub CmdOK_Click()
Dim UserName As String
Dim userpassword As String
Dim str As String
Dim nTryCount As Integer
Dim rs As New ADODB.Recordset
Set rs = New ADODB.Recordset
UserName = Trim(txtUserName.Text)
userpassword = Trim(txtpassword.Text)
str = "select * from 用户信息表where 用户名='" & UserName & "' and 用户密码= '" & userpassword & " '"
rs.Open str, connectString, adOpenKeyset, 2
If rs.EOF Then '登录失败
MsgBox "对不起,无此用户或者密码不正确!请重新输入!!", vbCritical, "错误"
txtUserName.Text = ""
txtpassword.Text = ""
txtUserName.SetFocus
nTryCount = nTryCount + 1
If nTryCount >= 3 Then
MsgBox "您无权操作本系统!", vbCritical, "无权限"
Unload Me
End If
Else '登陆成功
主界面.Show
Unload Me
End If
End Sub
方法二:
Private Sub Command1_Click()
Dim username As String
Dim userpassword As String
Dim try_times As Integer
try_times = 0
username = Trim$(user.text)
userpassword = Trim$(password.text)
If user.text = "" Then
MsgBox "用户名不能为空!", vbOKOnly + vbInformation, "友情提示"
user.SetFocus
Exit Sub
End If
If password.text = "" Then
MsgBox "密码不能为空!", vbOKOnly + vbInformation, "友情提示"
password.SetFocus
Exit Sub
End If
Dim strSQL As String
strSQL = "select * from staff where sno='" & username & "' and password='" & userpassword & "'"
Dim str As New ADODB.Recordset
Set str = New ADODB.Recordset
str.CursorLocation = adUseClient
str.Open strSQL, conn, adOpenStatic, adLockReadOnly
With str
If .State = adStateOpen Then .Close
.Open strSQL
If .EOF Then
try_times = try_times + 1
If try_times = 3 Then
MsgBox "您已经三次尝试进入本系统,均不成功,系统将会自动关闭", vbOKOnly + vbCritical, "警告"
Unload Me
Else
MsgBox "对不起,用户名不存在或密码不正确!", vbOKOnly + vbQuestion, "警告"
user.SetFocus
user.text = ""
password.text = ""
End If
Else
Unload Me
frm_main.Show
End If
End With
End Sub
Private Sub Command2_Click()
Unload Me
End Sub
Private Sub Form_Load()
Set conn = New ADODB.Connection
conn.ConnectionString = "dsn=LMS"
conn.Open
Login.Show
End Sub
Private Sub password_KeyPress(KeyAscii As Integer) If KeyAscii = 13 Then
Command1.SetFocus
End If
End Sub。