Private Sub Command1_Click() On Error Resume Next Dim DBC As New DataBaseConnection If db.State = 1 Then MsgBox "成功连接数据库!" db.Close End If db.ConnectionString = DBC.SqlConnectString(Text1.Text, Text2.Text, Text3.Text) rs.CursorType = adOpenDynamic rs.CursorLocation = adUseClient rs.LockType = adLockOptimistic db.CursorLocation = adUseClient db.Open Set cmd.ActiveConnection = db If Err.Number Then MsgBox Err.Description, 16 + vbOKOnly, Err.Number Exit Sub End If db.DefaultDatabase = "你的数据库名称" rs.CursorType = adOpenDynamic rs.LockType = adLockOptimistic If Err.Number Then MsgBox Err.Description, 16 + vbOKOnly, Err.Number Exit Sub End If Form1.Show Unload Me End SubPrivate Sub Command2_Click() Unload Me End Sub以上是连接服务器代码 Public Function MdbConnectString(MdbFile As String, Optional UserName As String, Optional password As String) As String MdbConnectString = "Provider=MSDASQL.1;Extended Properties=" + """" + "DBQ=" + MdbFile + ";Driver={Microsoft Access Driver (*.mdb)};MaxBufferSize=2048;MaxScanRows=8;PageTimeout=5;SafeTransactions=0;Threads=3;UID=" + UserName + ";Pwd=" + password + """" End FunctionPublic Function XlsConnectString(XlsFile As String) As String XlsConnectString = "Provider=MSDASQL.1;Extended Properties=" + """" + "DBQ=" + XlsFile + ";Driver={Microsoft Excel Driver (*.xls)};MaxBufferSize=2048;MaxScanRows=8;PageTimeout=5;SafeTransactions=0;Threads=3" + """" End FunctionPublic Function CsvConnectString() As String CsvConnectString = "Provider=MSDASQL.1;Extended Properties=" + """" + "Driver={Microsoft Text Driver (*.txt; *.csv)};MaxBufferSize=2048;MaxScanRows=25;PageTimeout=5;SafeTransactions=0;Threads=3;" + """" End FunctionPublic Function SqlConnectString(ServerName As String, UserName As String, password As String) As String SqlConnectString = "Provider=SQLOLEDB.1;User ID=" + UserName + ";Pwd=" + password + ";Data Source=" + ServerName End Function这是类模块DataBaseConnection代码Private Sub cmdOK_Click() On Error Resume Next cmd.CommandText = "Select * from user_info where user1 = '" + Text1.Text + "' And password = '" + Text2.Text + "'" Set rs = cmd.Execute If Err.Number Then MsgBox Err.Description, 16 + vbOKOnly, Err.Number Exit Sub End If If rs.RecordCount <> 1 Then MsgBox "用户名不正确或密码不对,请重新输入!", 16 + vbOKOnly, "错误" Exit Sub End If y = Form1.Text1.Text MDIForm1.Show MDIForm1.StatusBar1.Panels.Item(1).Text = "当前管理员:" & Form1.Text1.Text Unload Me micount = micount + 1 If micount = 3 Then Me.Hide End If Exit Sub End Sub这个是登录代码!有的地方你得改改!不明白的地方告诉我!
If mrc.EOF = True Then你尝试去追踪到这一句时,
检查Mrc这个RecordSet是否有数据.
假设是没有数据的的话.
用RecordCount<1来判断,不要用Mrc.EOF
user_ID是否为数字型?如是的话,
应改为 txtSQL = "select * from user_Info where user_ID =" & val(txtUserName.Text) 教你一个调试方法,
……
Set mrc = ExecuteSQL(txtSQL, MsgText) '
If mrc.EOF = True Then '<---debug这一行.再看看上一句Msgtext的值
改为Dim mrc As new ADODB.Recordset
或者在使用mrc的时候
set mrc=new adodb.recordset
On Error Resume Next
Dim DBC As New DataBaseConnection
If db.State = 1 Then
MsgBox "成功连接数据库!"
db.Close
End If
db.ConnectionString = DBC.SqlConnectString(Text1.Text, Text2.Text, Text3.Text)
rs.CursorType = adOpenDynamic
rs.CursorLocation = adUseClient
rs.LockType = adLockOptimistic
db.CursorLocation = adUseClient
db.Open
Set cmd.ActiveConnection = db
If Err.Number Then
MsgBox Err.Description, 16 + vbOKOnly, Err.Number
Exit Sub
End If
db.DefaultDatabase = "你的数据库名称"
rs.CursorType = adOpenDynamic
rs.LockType = adLockOptimistic
If Err.Number Then
MsgBox Err.Description, 16 + vbOKOnly, Err.Number
Exit Sub
End If
Form1.Show
Unload Me
End SubPrivate Sub Command2_Click()
Unload Me
End Sub以上是连接服务器代码
Public Function MdbConnectString(MdbFile As String, Optional UserName As String, Optional password As String) As String
MdbConnectString = "Provider=MSDASQL.1;Extended Properties=" + """" + "DBQ=" + MdbFile + ";Driver={Microsoft Access Driver (*.mdb)};MaxBufferSize=2048;MaxScanRows=8;PageTimeout=5;SafeTransactions=0;Threads=3;UID=" + UserName + ";Pwd=" + password + """"
End FunctionPublic Function XlsConnectString(XlsFile As String) As String
XlsConnectString = "Provider=MSDASQL.1;Extended Properties=" + """" + "DBQ=" + XlsFile + ";Driver={Microsoft Excel Driver (*.xls)};MaxBufferSize=2048;MaxScanRows=8;PageTimeout=5;SafeTransactions=0;Threads=3" + """"
End FunctionPublic Function CsvConnectString() As String
CsvConnectString = "Provider=MSDASQL.1;Extended Properties=" + """" + "Driver={Microsoft Text Driver (*.txt; *.csv)};MaxBufferSize=2048;MaxScanRows=25;PageTimeout=5;SafeTransactions=0;Threads=3;" + """"
End FunctionPublic Function SqlConnectString(ServerName As String, UserName As String, password As String) As String
SqlConnectString = "Provider=SQLOLEDB.1;User ID=" + UserName + ";Pwd=" + password + ";Data Source=" + ServerName
End Function这是类模块DataBaseConnection代码Private Sub cmdOK_Click()
On Error Resume Next
cmd.CommandText = "Select * from user_info where user1 = '" + Text1.Text + "' And password = '" + Text2.Text + "'"
Set rs = cmd.Execute
If Err.Number Then
MsgBox Err.Description, 16 + vbOKOnly, Err.Number
Exit Sub
End If
If rs.RecordCount <> 1 Then
MsgBox "用户名不正确或密码不对,请重新输入!", 16 + vbOKOnly, "错误"
Exit Sub
End If
y = Form1.Text1.Text
MDIForm1.Show
MDIForm1.StatusBar1.Panels.Item(1).Text = "当前管理员:" & Form1.Text1.Text
Unload Me
micount = micount + 1
If micount = 3 Then
Me.Hide
End If
Exit Sub
End Sub这个是登录代码!有的地方你得改改!不明白的地方告诉我!