Private Sub Command1_Click()
Dim sql As String
Dim conn As New ADODB.Connection
Dim rs_login As New ADODB.Recordset
If Trim(Text1.Text) = "" Then
'检测用户名正确与否
MsgBox "用户名不能为空,请重新输入!", vbOKOnly + vbExclamation, "错误"
Text1.SetFocus
Else
sql = "select * from Test where test_info
= '" & Text1.Text & "'"
rs_login.Open sql, conn, adOpenKeyset, adLockPessimistic
If rs_login.EOF = True Then
MsgBox "用户名不存在,请重新输入!", vbOKOnly + vbExclamation, "错误"
Text1 = ""
Text1.SetFocus
Else
'检测密码正确与否
If Trim(rs_login.Fields(1)) = Trim(Text2) Then
rs_login.Close
Unload Me
Form2.Show
Else
MsgBox "密码错误,请重新输入!", vbOKOnly + vbExclamation, "错误"
Text2.SetFocus
End If
End If
End If
End Sub
Private Sub Command2_Click()
MsgBox "您已成功退出!", vbOKOnly + vbExclamation, "提示"
Unload Me
End Sub
Private Sub Form_Load()
'此段代码的主要作用是当窗体打开时就连接数据库
Dim conn As New ADODB.Connection
Dim connectionstring As String
connectionstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & "E:\Test.mdb;Persist Security Info=False"
'将Data Source 处的路径改为你本机数据库所在路径即可
conn.Open connectionstring
End Sub
这是我做的登录框代码