VB+SQL登陆代码问题
PublicconnAsADODB.ConnectionSubmain()Setconn=NewADODB.Connectionconn.ConnectionString...
Public conn As ADODB.Connection
Sub main()
Set conn = New ADODB.Connection
conn.ConnectionString = "Provider=MSDASQL.1;Persist Security Info=False;User ID=sa;Data Source=ss"
conn.Open
登陆首页.Show '登录界面
End Sub
再在登录界面“确定”下写入如下代码:
Private Sub Command1_Click()
If Text1.Text = "" Then
MsgBox "用户名不能为空!", vbOKOnly + vbInformation, "友情提示"
Text1.SetFocus
Exit Sub
End If
If Text2.Text = "" Then
MsgBox "密码不能为空!", vbOKOnly + vbInformation, "友情提示"
Text2.SetFocus
Exit Sub
End If
Dim strSQl As String
strSQl = "select * from 用户表 where 用户名='" & Trim$(Text1.Text) & "' and 密码='" & Trim$(Text2.Text) & "' "
Dim str As New ADODB.Recordset
Set str = New ADODB.Recordset
str.CursorLocation = adUseClient
str.Open strSQl, conn, adOpenStatic, adLockReadOnly
With str
If .State = adStateOpen Then .Close
.Open strSQl
If .EOF Then
Try_times = Try_times + 1
If Try_times >= 3 Then
MsgBox "您已经三次尝试进入本系统,均不成功,系统将自动关闭", vbOKOnly + vbCritical, "警告"
Unload Me
Else
MsgBox "对不起,用户名不存在或密码错误 !", vbOKOnly + vbQuestion, "警告"
Text1.SetFocus
Text1.Text = ""
Text2.Text = ""
End If
Else
Unload Me
中页.Show '登录进入的另一个界面
End If
End With
End Sub
总会出现 实时错误3709 连接无法执行或者上下文关闭
提示是str.Open strSQl, conn, adOpenStatic, adLockReadOnly 这段错误 展开
Sub main()
Set conn = New ADODB.Connection
conn.ConnectionString = "Provider=MSDASQL.1;Persist Security Info=False;User ID=sa;Data Source=ss"
conn.Open
登陆首页.Show '登录界面
End Sub
再在登录界面“确定”下写入如下代码:
Private Sub Command1_Click()
If Text1.Text = "" Then
MsgBox "用户名不能为空!", vbOKOnly + vbInformation, "友情提示"
Text1.SetFocus
Exit Sub
End If
If Text2.Text = "" Then
MsgBox "密码不能为空!", vbOKOnly + vbInformation, "友情提示"
Text2.SetFocus
Exit Sub
End If
Dim strSQl As String
strSQl = "select * from 用户表 where 用户名='" & Trim$(Text1.Text) & "' and 密码='" & Trim$(Text2.Text) & "' "
Dim str As New ADODB.Recordset
Set str = New ADODB.Recordset
str.CursorLocation = adUseClient
str.Open strSQl, conn, adOpenStatic, adLockReadOnly
With str
If .State = adStateOpen Then .Close
.Open strSQl
If .EOF Then
Try_times = Try_times + 1
If Try_times >= 3 Then
MsgBox "您已经三次尝试进入本系统,均不成功,系统将自动关闭", vbOKOnly + vbCritical, "警告"
Unload Me
Else
MsgBox "对不起,用户名不存在或密码错误 !", vbOKOnly + vbQuestion, "警告"
Text1.SetFocus
Text1.Text = ""
Text2.Text = ""
End If
Else
Unload Me
中页.Show '登录进入的另一个界面
End If
End With
End Sub
总会出现 实时错误3709 连接无法执行或者上下文关闭
提示是str.Open strSQl, conn, adOpenStatic, adLockReadOnly 这段错误 展开
2个回答
展开全部
我初学、不过我有在网上下载的一个源码的图书管理数据库也是vb+sql的里面这段代码发出来给你希望对你有帮助
Private Sub cmdOK_Click()
Dim sql As String
Dim rs_login As New ADODB.Recordset
Dim conn As New ADODB.Connection
conn.Open "provider=Microsoft.Jet.OLEDB.4.0; data source=" & App.Path & "\database.mdb"
If Trim(Text1.Text) = "" Then
MsgBox "用户名不能为空,请重新输入!"
Text1.SetFocus
Else
sql = "select * from 用户表 where user_ID='" & Trim(Text1.Text) & "'"
rs_login.Open sql, conn, adOpenKeyset, adLockPessimistic
If rs_login.EOF = True Then
MsgBox "没有这个用户,请重输入!", vbOKOnly + vbExclamation, ""
Text1.Text = ""
Text1.SetFocus
Else
username = Text1.Text
If Trim(rs_login.Fields(1)) = Trim(Text2.Text) Then
Unload Me
图书馆理系统.Show
Else
MsgBox "密码不正确,请重输入!", vbOKOnly + vbExclamation, ""
Text2.SetFocus
Text2.Text = ""
End If
End If
End If
cnt = cnt + 1
If cnt = 3 Then
Unload Me
End If
End Sub
Private Sub cmdOK_Click()
Dim sql As String
Dim rs_login As New ADODB.Recordset
Dim conn As New ADODB.Connection
conn.Open "provider=Microsoft.Jet.OLEDB.4.0; data source=" & App.Path & "\database.mdb"
If Trim(Text1.Text) = "" Then
MsgBox "用户名不能为空,请重新输入!"
Text1.SetFocus
Else
sql = "select * from 用户表 where user_ID='" & Trim(Text1.Text) & "'"
rs_login.Open sql, conn, adOpenKeyset, adLockPessimistic
If rs_login.EOF = True Then
MsgBox "没有这个用户,请重输入!", vbOKOnly + vbExclamation, ""
Text1.Text = ""
Text1.SetFocus
Else
username = Text1.Text
If Trim(rs_login.Fields(1)) = Trim(Text2.Text) Then
Unload Me
图书馆理系统.Show
Else
MsgBox "密码不正确,请重输入!", vbOKOnly + vbExclamation, ""
Text2.SetFocus
Text2.Text = ""
End If
End If
End If
cnt = cnt + 1
If cnt = 3 Then
Unload Me
End If
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询