源代码:
Dim DB1 As New ADODB.Connection
Dim userDB As New ADODB.Recordset
Private Sub Command1_Click()
DB1.Open "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=" & App.Path & "\Mydb.mdb" '数据库名字
userDB.Open "select * From UserInfo", DB1, adOpenKeyset, adLockOptimistic
userDB.Fields.Refresh
userDB.MoveFirst
Do While Not userDB.EOF
If Text1.Text = userDB.Fields("PassWord") And Combo1.Text = userDB.Fields("UserName") Then
MsgBox "OK", , "提示"
userDB.Close
DB1.Close
'该处可以插入登陆成功后执行的语句
Unload Me
Exit Sub
Exit Do
End If
userDB.MoveNext
Loop
MsgBox "密码或用户名错误", 16, "错误提示" '未登陆成功,循环结束,提示登陆失败,该处还可以加入控制输错密码的次数的语句
userDB.Close
DB1.Close
End Sub
Private Sub Command1_KeyPress(KeyAscii As Integer)
If KeyAscii = 27 Then Unload Me
End Sub
Private Sub Command2_Click()
Unload Me
End Sub
Private Sub Form_Load()
DB1.Open "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=" & App.Path & "\Mydb.mdb"
userDB.Open "select * From UserInfo", DB1, adOpenKeyset, adLockOptimistic
userDB.Fields.Refresh
userDB.MoveFirst
Do While Not userDB.EOF
Combo1.AddItem userDB.Fields("UserName")
userDB.MoveNext
Loop
Combo1.ListIndex = 0
userDB.Close
DB1.Close
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then Command1_Click
If KeyAscii = 27 Then Unload Me
End Sub
详细资料见附件(源代码+数据库文件+工程文件)
追问可以不使用数据库吗
追答可以不用数据库,但是登陆人员的名字,密码只能直接写在程序里,后期人员调整时只能更改源代码,若是放在数据库里,人员调整只调整数据库文件即可,看你需要