VB和Access2003做的登陆窗体的问题

来源:百度知道 编辑:UC知道 时间:2024/05/12 04:12:01
我的代码是
Public con As New Connection '定义数据连接,公共变量
Public user, pwd As String

Sub main()
ChDir App.Path '转换相对路径

con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=InstrumentManage.mdb;Persist Security Info=False"
'定义数据库连接
con.CursorLocation = adUseClient '以客户端的方式打开

con.Open ' 打开连接
系统管理员.Show
End Sub

'登陆窗体...

Private Sub cmdOk_Click()
Dim rs As New Recordset
If (txtUser.Text = "") Or (txtPwd.Text = "") Then
MsgBox "用户名和密码不能为空!", vbOKOnly, "错误"
Exit Sub
End If
user = txtUser.Text
pwd = txtPwd.Text
sql = "select * from 系统管理员 where 用户名='" + user + "' and 密码='" + pwd + "'"
Set rs = con.Execute(sql)
If rs.EOF Then
MsgBox "用户名和密码错误!", vbOKOnly, "错误"

我认为主要错误是
con.Close
rs.Close
你先关闭了con,应该先关闭rs,再关闭con.
rs.close
con.close
我对你的程序修改如下:
Public con As New ADODB.Connection '定义数据连接,公共变量
Public user, pwd As String

Sub main()
ChDir App.Path '转换相对路径

con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=InstrumentManage.mdb;Persist Security Info=False"
'定义数据库连接
con.CursorLocation = adUseClient '以客户端的方式打开

con.Open ' 打开连接
系统管理员.Show
End Sub

'登陆窗体...

Private Sub cmdOk_Click()
Dim rs As New ADODB.Recordset
If (txtUser.Text = "") Or (txtPwd.Text = "") Then
MsgBox "用户名和密码不能为空!", vbOKOnly, "错误"
Exit Sub
End If
user = txtUser.Text
pwd = txtPwd.Text
sql = "select * from 系统管理员 where 用户名='" + user + "' and 密码='" + pwd + "'"
Set rs = c