修改后的结果,应该没有问题了:
Public conn As ADODB.Connection '这个语句最好放在模块中
Dim Try_times As Integer
Private Sub Command1_Click()
If id(0).Text = "" Then
MsgBox "用户名不能为空!", vbOKOnly + vbInformation, "友情提示"
id(0).SetFocus
Exit Sub
End If
If Password.Text = "" Then
MsgBox "密码不能为空!", vbOKOnly + vbInformation, "友情提示"
Password.SetFocus
Exit Sub
End If
Call main(conn) '调用连接数据库的过程
Dim strSQl As String
strSQl = "select * from Users where users_name='" & Trim$(Text2) & "' and password='" & Trim$(Text3) & "' "
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, "警告"
id(0).SetFocus
id(0).Text = ""
Password.Text = ""
End If
Else
MsgBox "祝贺你!你已经成功登录 !", 64, "马上进入主程序"
Unload Me
Form2.Show '登录进入的另一个界面
End If
End With
str.Close '关闭记录集
conn.Close '关闭数据库连接
End Sub
Private Sub Form_Load()
id(0).Text = ""
Password.Text = ""
End Sub
Sub main(conn) '该过程最好放在模块中
conn.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=False; User ID=zxy;password=123;Initial Catalog=jwglxt;Data Source=127.0.0.1"
conn.Open
End Sub
conn 没有定义,也没有连接到数据库,你抄的代码不完整
3001一般是调用参数错误,这里是recordset的Open方法第二个参数是adodb.connection,你没有初始化这个连接