VB连接access数据库问题
access中建立了一个用户登录表如图
2016-05-16 13:25
程序代码:Dim cn As ADODB.Connection Set cn = New ADODB.Connection Dim myrec As ADODB.Recordset cn.ConnectionString = TheConStr '替换成联系你数据库的代码 cn.Open Set myrec = New ADODB.Recordset sqltxt="Select * from 用户登录 where 用户名='" & trim(textbox1.text) & "' And 密码='" & textbox2.text & "'" '这里表名、字段名、控件名按照实际情况修改 myrec.Open Trim(sqltxt), cn, 1, 2 If myrec.RecordCount > 0 Then '用户名、密码匹配的代码 Else '用户名或者密码错误的代码 End If
2016-05-16 13:51
程序代码:Dim ASK As Integer, DLSF As Boolean
Dim cn As New ADODB.Connection, RS As New ADODB.Recordset '这部分在通用部分定义变量
Private Sub Command1_Click()
'登录
If Text1.Text = "" Then
MsgBox "你没有填写登录用户名,请填写!", 16, "提示!"
Exit Sub
End If
If Text2.Text = "" Then
MsgBox "你没有填写登录用户密码,请填写!", 16, "提示!"
Exit Sub
End If
If DLSF = "" Then
MsgBox "你没有选择用户身份,请选择!", 16, "提示!"
Exit Sub
End If
Dim SQLM As String
cn.ConnectionString = "DRIVER=Microsoft Access Driver (*.mdb);dbQ=" & App.Path & "\你的数据库的名称.mdb"
cn.Open
SQLM = "Select * From 用户登录 Where 用户名='" & Text1.Text & "' And 身份='" & DLSF & "'"
RS.Open SQLM, cn, 2, 2
If Not RS.EOF Then
If RS!密码 = Text2.Text Then
MsgBox "祝贺你!你已经成功登录!", 64, "登录成功!"
If DLSF = True Then
Form1.Show '进入管理员界面
ElseIf DLSF = False Then
Form2.Show '进入用户界面
End If
Unload Me
Else
MsgBox "对不起!你输入的用户密码不正确,请重新输入!", 16, "密码错误!"
ASK = ASK + 1
Text2.Text = ""
Text2.SetFocus
End If
Else
MsgBox "对不起!你输入的用户名不正确,请重新输入!", 16, "用户名错误!"
ASK = ASK + 1
Text1.Text = ""
Text2.Text = ""
Text1.SetFocus
End If
RS.Close
cn.Close
If ASK >= 3 Then
MsgBox "对不起,你已经连续三次输入错误,不能继续登录,程序就退出!", 16, "登录超次!"
End
End If
End Sub
Private Sub Command2_Click()
End
End Sub
Private Sub Option1_Click(Index As Integer)
If Option1(0).Value = True Then
DLSF = True
ElseIf Option1(1).Value = True Then
DLSF = False
End If
End Sub

2016-05-16 13:52
2016-05-18 09:26