OptionExplicit Const conString ="Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=dssopt_sms;Data Source=." Dim cn As ADODB.Connection PrivateSub Command1_Click() Static i AsInteger'控制登陸的次數 Dim Username AsString Dim Password AsString Dim rs As ADODB.Recordset Dim be AsBoolean Dim sql AsString i = i +1 Username = txtUser ‘為了防止注入,要判斷所取的長度、特殊字符替換 Password = txtPassword '判斷用戶名是否存在 sql ="select * from 登陸 where 用戶名='"& Username &"'" set rs = new ADODB.Recordset Call rs.Open(sql, cn, 1, 1) be = (rs.EOF And rs.BOF) rs.Close If be Then If i <3Then MsgBox"無效的用戶名", , "系統登陸" txtUser.SetFocus SendKeys "{Home}+{end}" ExitSub Else MsgBox"多次錯誤登陸,再見!", , "系統登陸" End EndIf EndIf '判斷密碼是否正確 sql ="select * from 登陸 where 用戶名='"& Username &"' and 密碼='"& Password &"'" Call rs.Open(sql, cn, 1, 1) be = (rs.EOF And rs.BOF) rs.Close set rs = noting IfNot be Then 'frmPic.Show 'Denglu_name = Username 'Load MDIForm1 MsgBox"Login successful!" Unload Me Else If i <3Then MsgBox"無效的密碼", , "系統登陸" txtPassword.SetFocus SendKeys "{Home}+{end}" Else MsgBox"多次輸入錯誤,請正確輸入!", , "系統登陸" End EndIf EndIf End Sub PrivateSub Form_Load() Set cn =New ADODB.Connection cn.Open conString End Sub