Private Sub cmdOK_Click()
Dim txtSQL As String
Dim mrc As ADODB.Recordset
Dim MsgText As String
UserName = ""
If Trim(txtUserName.Text = "") Then
MsgBox "没有这个用户,请重新输入用户名!", vbOKOnly + vbExclamation, "警告"
txtUserName.SetFocus
Else
txtSQL = "select * from user_Info where user_ID =" & txtUserName.Text & ""
Set mrc = ExecuteSQL(txtSQL, MsgText)
If mrc.EOF = True Then ?????当程序运行到这里就说“对象变量或With块变量未设置”。Public Function ExecuteSQL(ByVal SQL As String, MsgString As String) As ADODB.Recordset
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim sTokens() As String
On Error GoTo ExecuteSQL_Error
sTokens = Split(SQL)
Set cnn = New ADODB.Connection
cnn.Open ConnectString
If InStr("INSERT,DELETE,UPDATE", UCase$(sTokens(0))) Then
cnn.Execute SQL
MsgString = sTokens(0) & "query successful"
Else
Set rst = New ADODB.Recordset
rst.Open Trim$(SQL), cnn, adOpenKeyset, adLockOptimistic
Set ExecuteSQL = rst
MsgString = "查询到" & rst.RecordCount & "条纪录"
End If
ExecuteSQL_Exit:
Set rst = Nothing
Set cnn = Nothing
Exit Function
ExecuteSQL_Error:
MsgString = "查询错误:" & Err.Description
Resume ExecuteSQL_Exit
End Function
请各位大哥解读。上面各位大哥所说的,我都试过了,可是还是不行,如果我在开始的时候这样写 dim mrc as new adodb.recordset的话就会出现“对象已经关闭,不允许操作”。如果我写成set mrc=new adodb.recordset的话,还是出现最初的错误。救命呀!!!连接是没有问题的。不缺少ConnectString语句。
工程中引用了,“Microsoft ActiveX Data objects 2.6librarv”
Dim txtSQL As String
Dim mrc As ADODB.Recordset
Dim MsgText As String
UserName = ""
If Trim(txtUserName.Text = "") Then
MsgBox "没有这个用户,请重新输入用户名!", vbOKOnly + vbExclamation, "警告"
txtUserName.SetFocus
Else
txtSQL = "select * from user_Info where user_ID =" & txtUserName.Text & ""
Set mrc = ExecuteSQL(txtSQL, MsgText)
If mrc.EOF = True Then ?????当程序运行到这里就说“对象变量或With块变量未设置”。Public Function ExecuteSQL(ByVal SQL As String, MsgString As String) As ADODB.Recordset
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim sTokens() As String
On Error GoTo ExecuteSQL_Error
sTokens = Split(SQL)
Set cnn = New ADODB.Connection
cnn.Open ConnectString
If InStr("INSERT,DELETE,UPDATE", UCase$(sTokens(0))) Then
cnn.Execute SQL
MsgString = sTokens(0) & "query successful"
Else
Set rst = New ADODB.Recordset
rst.Open Trim$(SQL), cnn, adOpenKeyset, adLockOptimistic
Set ExecuteSQL = rst
MsgString = "查询到" & rst.RecordCount & "条纪录"
End If
ExecuteSQL_Exit:
Set rst = Nothing
Set cnn = Nothing
Exit Function
ExecuteSQL_Error:
MsgString = "查询错误:" & Err.Description
Resume ExecuteSQL_Exit
End Function
请各位大哥解读。上面各位大哥所说的,我都试过了,可是还是不行,如果我在开始的时候这样写 dim mrc as new adodb.recordset的话就会出现“对象已经关闭,不允许操作”。如果我写成set mrc=new adodb.recordset的话,还是出现最初的错误。救命呀!!!连接是没有问题的。不缺少ConnectString语句。
工程中引用了,“Microsoft ActiveX Data objects 2.6librarv”
===>>
if trim(txUserName.text)="" 这里好象错了,呵呵
If Trim(txtUserName.Text) = "" Then
MsgBox "没有这个用户,请重新输入用户名!", vbOKOnly + vbExclamation, "警告"
txtUserName.SetFocus
exit sub
end if
txtSQL = "select * from user_Info where user_ID =" & txtUserName.Text & ""
Set mrc = ExecuteSQL(txtSQL, MsgText)
If mrc.EOF = True Then
msgbox "没有纪录"
end if
'声明rs是新的adodb记录集
Dim Rs As New ADODB.Recordset
'声明cn用与连接
Private cn As ConnectionPrivate Sub Form_Load()
'声明connstr是字符串,作连接字符串使用
Dim ConnStr As String
'声明cmd是新的adodb命令
Dim cmd As New ADODB.Command
'设置cn是新的adodb连接
Set cn = New ADODB.Connection
'设置connstr的连接字符串为"dsn=数据库" 数据库为odbc里的名称
ConnStr = "dsn=数据库"
'建立odbc连接
cn.ConnectionString = ConnStr
'打开数据库
cn.Open
'对于命令
With cmd
'使用cn激活连接
.ActiveConnection = cn
'命令类型为“表”类型
.CommandType = adCmdTable
'命令文本为“表1”,相当于数据源名
.CommandText = "表1"
'命令设置结束
End With
'对于记录集
With Rs
'游标类型为打开状态
.CursorType = adOpenStatic
'设置记录的锁定方式
.LockType = adLockPessimistic
'使用命令
.Open cmd
'记录集设置完毕
End With
End Sub
我刚开始学数据库,我这样用都是可以的,你试试看吧
这里应该是
txtSQL = "select * from user_Info where user_ID ='" & txtUserName.Text & "'"
txtSQL = "select * from user_Info where user_ID =" & txtUserName.Text & ""
这里应该是
txtSQL = "select * from user_Info where user_ID ='" & txtUserName.Text & "'"
txtSQL = "select * from user_Info where user_ID =" & txtUserName.Text & ""
应该为
txtSQL = "select * from user_Info where user_ID = '" & txtUserName.Text & "'"