我在这个事件有了一段代码
Private Sub Command1_Click()
Dim MsgText As String
Dim txtSQL As String
Dim mrc As ADODB.Recordset
txtSQL = "select * from aa where ad =aa"
Set mrc = ExecuteSQL(txtSQL, MsgText)
UserName = ""
If mrc.EOF = True Then
MsgBox "没有这个用户,请重新输入用户名!", vbOKOnly + vbExclamation, "警告"
txtUserName.SetFocus
Else
If Trim(mrc.Fields(1)) = Trim(txtPassword.Text) Then
OK = True
mrc.Close
Me.Hide
UserName = Trim(txtUserName.Text)
Else
MsgBox "输入密码不正确,请重新输入!", vbOKOnly + vbExclamation, "警告"
txtPassword.SetFocus
txtPassword.Text = ""
End If
End If
Exit Sub
然后我声明了一个通用函数是
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
'rst.MoveLast 'get RecordCount
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但是在运行当中说是错的,是实时错误"91"
对像变量或with块变量未设置,请问一下是什么原因,谢谢,我现在非常的急哟!
Private Sub Command1_Click()
Dim MsgText As String
Dim txtSQL As String
Dim mrc As ADODB.Recordset
txtSQL = "select * from aa where ad =aa"
Set mrc = ExecuteSQL(txtSQL, MsgText)
UserName = ""
If mrc.EOF = True Then
MsgBox "没有这个用户,请重新输入用户名!", vbOKOnly + vbExclamation, "警告"
txtUserName.SetFocus
Else
If Trim(mrc.Fields(1)) = Trim(txtPassword.Text) Then
OK = True
mrc.Close
Me.Hide
UserName = Trim(txtUserName.Text)
Else
MsgBox "输入密码不正确,请重新输入!", vbOKOnly + vbExclamation, "警告"
txtPassword.SetFocus
txtPassword.Text = ""
End If
End If
Exit Sub
然后我声明了一个通用函数是
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
'rst.MoveLast 'get RecordCount
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但是在运行当中说是错的,是实时错误"91"
对像变量或with块变量未设置,请问一下是什么原因,谢谢,我现在非常的急哟!
解决方案 »
- 新手学VB,请好心人帮个忙
- 〓Tiger_Zhao 〓请进,当然也欢迎其它高手,再帮我解决下打印问题
- 跪求 用vb做图像扫描,并且存贮到数据库,特别是twain接口,的例子。给一个,谢谢各位大虾了
- 高手,以下代码是对infile文件进行Base64解码,我想如infile是一个字符串,那么下面代码该如何改变,谢谢!!!
- 一个很简单的好像见到鬼问题!
- 如此问题?
- 大家做chart用什么控件呀??
- 还是SQL SERVER数据库的导出问题
- 如何屏蔽键盘?
- 网吧管理软件应用经验总结,想知道其中BUG快来说一说,来者有份!!!!
- vba最多支持多少个变量,超过50个是否用数组好一些
- excel中vb编辑器,使用控件问题
If mrc.EOF = True Then
这一句,不好意思好
你可以这样试试,直接将连接数据库的代码写在ExecuteSQL中。
就是改 cnn.Open ConnectString
为:cnn.Open "Provider = SQLOLEDB.1;Persist Security Info = False;" & _
"User ID = sa;Password = 123;Data Source = 192.168.0.1;" & _
"Initial Catalog = youDB"