以下这段代码是连接SQL的登录语句private sub cmdLogon() If Trim(txtcUserName.Text) = "" Then MsgBox "请输入登录用户!", 48, "提示" Screen.MousePointer = vbDefault txtcUserName.SetFocus Exit Sub End If
StrSql = " select cUserCode,cUserName,cUserpwd,bUserAdmin,bUserFbd from users " _ & " where cUserCode='" & Trim(txtcUserName.Text) & "'" If ar_Tmp.State <> adStateClosed Then ar_Tmp.Close ar_Tmp.Open StrSql, g_strDBConn, adOpenKeyset, adLockReadOnly If ar_Tmp.RecordCount = 0 Then MsgBox "登录用户或密码错误!", 48, "提示" Screen.MousePointer = vbDefault ar_Tmp.Close Exit Sub Else If Not (IsNull(ar_Tmp!cUserPwd) Or ar_Tmp!cUserPwd = "") Then If Trim(ar_Tmp!cUserPwd) <> Trim(txtcPassword.Text) Then MsgBox "登录用户或密码错误!", 48, "提示" If ar_Tmp.State <> adStateClosed Then ar_Tmp.Close Screen.MousePointer = vbDefault Exit Sub End If Else If Trim(txtcPassword.Text) <> "" Then MsgBox "登录用户或密码错误!", 48, "提示" If ar_Tmp.State <> adStateClosed Then ar_Tmp.Close Screen.MousePointer = vbDefault Exit Sub End If End If
g_strUserCode = ar_Tmp!cUserCode g_strUserName = ar_Tmp!cUserName '是否系统管理员 If ar_Tmp!bUserAdmin = True Then g_blnUserAdmin = True Else g_blnUserAdmin = False End If End If If ar_Tmp.State <> adStateClosed Then ar_Tmp.Close end sub
请参考这个吧 Private Sub Cmd_OK_Click() Dim SetRec As ADODB.Recordset Dim SetSql As String Dim SetStr As String 'On Error Resume Next On Error GoTo ErrDcs '判断用户名和密码是否为空 UserName = "" 'UserName是全局变量 If Trim(TxtLguser.text = "") Then MsgBox "请输入你的用户名!", vbOKOnly + vbInformation, "提示信息!" TxtLguser.SetFocus Exit Sub End If If Trim(TxtLgpwd.text = "") Then '判断用户名是否为空 MsgBox "请输入你的密码!", vbOKOnly + vbInformation, "提示信息!" TxtLgpwd.SetFocus Exit Sub End If '不为空的程序 SetSql = "select * from user where UserName='" & TxtLguser & "'" Set SetRec = executesql(SetSql, SetStr) If SetRec.EOF = True Then MsgBox "用户名错误,请重新输入!", vbOKOnly + vbExclamation, "警告" TxtLguser.SetFocus TxtLguser.text = "" Else If Trim(SetRec.Fields(2)) = Trim(TxtLgpwd.text) Then OK = True Else MsgBox "密码错误,请重新输入!", vbOKOnly + vbExclamation, "警告" TxtLgpwd.SetFocus TxtLgpwd.text = "" End If End If UserName = Trim(TxtLguser.text) '获得全局变量 UserName If OK = False Then miCount = miCount + 1 '登录次数加 1 If miCount = 3 Then Me.Hide SetRec.Close Set SetRec = Nothing Else Me.Hide End If Exit Sub ErrDcs: MsgBox Err & ":" & Error, vbInformation, Me.Caption, Err.Description Exit Sub End Sub
MsgBox "请输入登录用户!", 48, "提示"
Screen.MousePointer = vbDefault
txtcUserName.SetFocus
Exit Sub
End If
StrSql = " select cUserCode,cUserName,cUserpwd,bUserAdmin,bUserFbd from users " _
& " where cUserCode='" & Trim(txtcUserName.Text) & "'"
If ar_Tmp.State <> adStateClosed Then ar_Tmp.Close
ar_Tmp.Open StrSql, g_strDBConn, adOpenKeyset, adLockReadOnly
If ar_Tmp.RecordCount = 0 Then
MsgBox "登录用户或密码错误!", 48, "提示"
Screen.MousePointer = vbDefault
ar_Tmp.Close
Exit Sub
Else
If Not (IsNull(ar_Tmp!cUserPwd) Or ar_Tmp!cUserPwd = "") Then
If Trim(ar_Tmp!cUserPwd) <> Trim(txtcPassword.Text) Then
MsgBox "登录用户或密码错误!", 48, "提示"
If ar_Tmp.State <> adStateClosed Then ar_Tmp.Close
Screen.MousePointer = vbDefault
Exit Sub
End If
Else
If Trim(txtcPassword.Text) <> "" Then
MsgBox "登录用户或密码错误!", 48, "提示"
If ar_Tmp.State <> adStateClosed Then ar_Tmp.Close
Screen.MousePointer = vbDefault
Exit Sub
End If
End If
g_strUserCode = ar_Tmp!cUserCode
g_strUserName = ar_Tmp!cUserName
'是否系统管理员
If ar_Tmp!bUserAdmin = True Then
g_blnUserAdmin = True
Else
g_blnUserAdmin = False
End If
End If
If ar_Tmp.State <> adStateClosed Then ar_Tmp.Close
end sub
Private Sub Cmd_OK_Click()
Dim SetRec As ADODB.Recordset
Dim SetSql As String
Dim SetStr As String
'On Error Resume Next
On Error GoTo ErrDcs
'判断用户名和密码是否为空
UserName = "" 'UserName是全局变量
If Trim(TxtLguser.text = "") Then
MsgBox "请输入你的用户名!", vbOKOnly + vbInformation, "提示信息!"
TxtLguser.SetFocus
Exit Sub
End If
If Trim(TxtLgpwd.text = "") Then '判断用户名是否为空
MsgBox "请输入你的密码!", vbOKOnly + vbInformation, "提示信息!"
TxtLgpwd.SetFocus
Exit Sub
End If
'不为空的程序
SetSql = "select * from user where UserName='" & TxtLguser & "'"
Set SetRec = executesql(SetSql, SetStr)
If SetRec.EOF = True Then
MsgBox "用户名错误,请重新输入!", vbOKOnly + vbExclamation, "警告"
TxtLguser.SetFocus
TxtLguser.text = ""
Else
If Trim(SetRec.Fields(2)) = Trim(TxtLgpwd.text) Then
OK = True
Else
MsgBox "密码错误,请重新输入!", vbOKOnly + vbExclamation, "警告"
TxtLgpwd.SetFocus
TxtLgpwd.text = ""
End If
End If
UserName = Trim(TxtLguser.text) '获得全局变量 UserName
If OK = False Then
miCount = miCount + 1 '登录次数加 1
If miCount = 3 Then Me.Hide
SetRec.Close
Set SetRec = Nothing
Else
Me.Hide
End If
Exit Sub
ErrDcs:
MsgBox Err & ":" & Error, vbInformation, Me.Caption, Err.Description
Exit Sub
End Sub