想用VB做一个登陆界面如何和access相连接?

2024-12-02 10:43:58
推荐回答(3个)
回答1:

1、首先在部件-控件里找到Microsoft ADO DataControl 6.0,打勾,然后在窗体上添加Adodc1。
2. 添加两个文本输入框text1,text2,分别用来接收用户输入用户名和密码。
3、在Adodc1控件的属性窗口中单击Connectionstring右侧的…按钮,打开ADO的属性页,选择“使用连接字符串”,单击“生成”按钮,打开数据链接属性对话框,在“提供程序”页中选择Microsoft Jet 4.0 OLE DB Provider,单击“下一步”,在“连接”页中,输入数据库名称XXXX.mdb(前提是将数据库文件和工程文件保存在同一目录下),单击“测试连接”,如果连接成功,单击“确定”即可。
4、将以下代码添加到登录按钮:
Private Sub Command1_Click()
Adodc1.RecordSource = "select * from 数据表名称 where username='" & Text1.Text & "'"
Adodc1.Refresh
If Adodc1.Recordset.EOF Then
MsgBox "没有这个用户,请重新输入正确的用户名", , "错误提示"
Else
If Text2.Text = Adodc1.Recordset.Fields("password") Then
MsgBox "登录成功!", , "错误提示"
Else
MsgBox "你的密码错误,请输入正确的用户密码!", , "错误提示"
End If
End If
End Sub

回答2:

按照以下代码把你的表名、Username和Password改下就行了。。
Option Explicit
Public appdisk As String
Public conn As New adodb.Connection
Public rs As New adodb.Recordset
Public Db As String
Private sSQL As String

Private Sub CommandButton1_Click()
If Trim(Text1.Text) = "" And Trim(Text2.Text) = "" Then
MsgBox "用户名和密码不能为空!!!", vbCritical, "错误!!"
Exit Sub
End If
If Trim(Text1.Text) = "" Then
MsgBox "用户名不能为空!!!", vbCritical, "错误!!"
Text1.SetFocus
Exit Sub
End If
If Trim(Text2.Text) = "" Then
MsgBox "密码不能为空!!!", vbCritical, "错误!!"
Text2.SetFocus
Exit Sub
End If
sSQL = "select * from 用户权限更改 where 用户名='" & Trim(Text1.Text) & "'"
rs.Open sSQL, conn, adOpenKeyset, adLockPessimistic
If rs.EOF = True Then
MsgBox "没有这个用户!!!", vbCritical, "错误!!"
Text1.SetFocus
rs.Close: Set rs = Nothing
Exit Sub
End If
rs.Close: Set rs = Nothing
sSQL = "select * from 用户权限更改 where 用户名='" & Trim(Text1.Text) & "' and 密码='" & Trim(Text2.Text) & "'"
rs.Open sSQL, conn, adOpenKeyset, adLockPessimistic
If rs.EOF = True Then
MsgBox "密码错误!!!", vbCritical, "错误!!"
Text2.SetFocus
rs.Close: Set rs = Nothing
Exit Sub
Else
user = Text1.Text
Unload Me
Form2.Show
End If
rs.Close: Set rs = Nothing
End Sub

Private Sub Form_Load()
Dim i As Integer
On Error GoTo Err
appdisk = Trim(App.Path)
If Right(appdisk, 1) <> "\" Then appdisk = appdisk & "\"
Db = appdisk
Db = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=" & Db & "xld.mdb"
conn.CursorLocation = adUseClient
conn.Open Db
Exit Sub
Err:
MsgBox Err.Number
Unload Me
End Sub

回答3:

1. 利用Adodc控件来实现,首先在部件-控件里找到Microsoft ADO DataControl 6.0,打勾,然后在窗体上添加Adodc,设置visible 为 false。
2. 添加textbox 两个,text1,text2 (将text2的PasswordChar属性值设置为*)
3. 添加botton一个,command1

Private Sub Form_Load()
'连接数据库的信息(Jet OLEDB 4.0, DataSource - 数据库地址,这里用到了App.Path 意思是说在应用程序目录下的)
Adodc1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\Biodoor.mdb;Persist Security Info=False"
End Sub

Private Sub Command1_Click()
'单击command1开始连接数据库,利用SQL语句来查询表(这儿暂时用Access数据库中的sys_Users数据表)
Adodc1.RecordSource = "Select * From sys_Users Where UserName ='" & Text1.Text & "'"
Adodc1.Refresh

'判断是否存在数据
If Adodc1.Recordset.EOF = True Then
'如果不存在跳出窗口说 Invalid Login
MsgBox "Invalid Login!"
Else
'如果存在继续判断密码是否为text2的文本
If Adodc1.Recordset.Fields("UserPassword") = Text2.Text Then
'如果是跳出窗口说 Welcome
MsgBox "Welcome!"
Else
'否则跳出窗口说 Invalid Login
MsgBox "Invalid Login!"
End If
End If
End Sub