关于VB与数据库的 运行VB是弹出这样的错误:对象变量或with块变量未设置
这是定义的函数PublicFunctionEXECUTESQL(ByValsqlAsString)AsADODB.Recordset'传递参数:SQL传递语句,返回执行结...
这是定义的函数
Public Function EXECUTESQL(ByVal sql As String) As ADODB.Recordset
'传递参数:SQL 传递语句,返回执行结果记录集 Recordset
Dim Conn As New ADODB.Connection
Dim RsStu As New ADODB.Recordset
Dim sTokens() As String
On Error GoTo ExecuteSQL_Error
sTokens = Split(sql)
Conn.CursorLocation = adUseClient
Conn.ConnectionString = "Provider=SQLOLEDB.1; " & _
"Data Source=127.0.0.1; Initial Catalog=TuiMag; " & _
"User ID=sa; Password="
Conn.Open
If InStr("INSERT、UPDATE、DELETE", UCase(sTokens(0))) Then
Conn.BeginTrans
Conn.Execute sql
Conn.CommitTrans
Else
RsStu.Open Trim$(sql), Conn, adOpenStatic, adLockOptimistic
Set EXECUTESQL = RsStu
End If
ExecuteSQL_Exit:
Set Conn = Nothing
Set RsStu = Nothing
Exit Function
ExecuteSQL_Error:
Resume ExecuteSQL_Exit
End Function
然后是登陆界面的代码
Option Explicit
Dim RsStu As ADODB.Recordset
Private Sub cmd登陆_Click()
If OpStudent.Value = True Then
Dim mysql As String
mysql = "select * from StuInfo where StuID='" & Trim(txtid.Text) & "'"
Set RsStu = New ADODB.Recordset
Set RsStu = EXECUTESQL(mysql)
If RsStu.RecordCount > 0 Then
If RsStu.Fields(2) = Trim(txtpsw.Text) Then
frm菜单.Show
frm登陆.Hide
Else
MsgBox "密码错误"
txtpsw.Text = ""
txtpsw.SetFocus
End If
Else
MsgBox "没有该用户"
txtid.Text = ""
txtpsw.Text = ""
txtid.SetFocus
End If
End If
然后就在If RsStu.RecordCount > 0 Then 这里停住了 变成黄色了
在线等你们的回答
万分感谢
试过了把登陆界面的Dim RsStu As ADODB.Recordset
改为Dim RsStu As New ADODB.Recordset 之后出现另一个问题:对象关闭时不允许操作 展开
Public Function EXECUTESQL(ByVal sql As String) As ADODB.Recordset
'传递参数:SQL 传递语句,返回执行结果记录集 Recordset
Dim Conn As New ADODB.Connection
Dim RsStu As New ADODB.Recordset
Dim sTokens() As String
On Error GoTo ExecuteSQL_Error
sTokens = Split(sql)
Conn.CursorLocation = adUseClient
Conn.ConnectionString = "Provider=SQLOLEDB.1; " & _
"Data Source=127.0.0.1; Initial Catalog=TuiMag; " & _
"User ID=sa; Password="
Conn.Open
If InStr("INSERT、UPDATE、DELETE", UCase(sTokens(0))) Then
Conn.BeginTrans
Conn.Execute sql
Conn.CommitTrans
Else
RsStu.Open Trim$(sql), Conn, adOpenStatic, adLockOptimistic
Set EXECUTESQL = RsStu
End If
ExecuteSQL_Exit:
Set Conn = Nothing
Set RsStu = Nothing
Exit Function
ExecuteSQL_Error:
Resume ExecuteSQL_Exit
End Function
然后是登陆界面的代码
Option Explicit
Dim RsStu As ADODB.Recordset
Private Sub cmd登陆_Click()
If OpStudent.Value = True Then
Dim mysql As String
mysql = "select * from StuInfo where StuID='" & Trim(txtid.Text) & "'"
Set RsStu = New ADODB.Recordset
Set RsStu = EXECUTESQL(mysql)
If RsStu.RecordCount > 0 Then
If RsStu.Fields(2) = Trim(txtpsw.Text) Then
frm菜单.Show
frm登陆.Hide
Else
MsgBox "密码错误"
txtpsw.Text = ""
txtpsw.SetFocus
End If
Else
MsgBox "没有该用户"
txtid.Text = ""
txtpsw.Text = ""
txtid.SetFocus
End If
End If
然后就在If RsStu.RecordCount > 0 Then 这里停住了 变成黄色了
在线等你们的回答
万分感谢
试过了把登陆界面的Dim RsStu As ADODB.Recordset
改为Dim RsStu As New ADODB.Recordset 之后出现另一个问题:对象关闭时不允许操作 展开
1个回答
展开全部
RsStu是空对象(nothing),回 EXECUTESQL里检查sql语句是否正确,msgbox "Provider=SQLOLEDB.1; " & _
"Data Source=127.0.0.1; Initial Catalog=TuiMag; " & _
"User ID=sa; Password="
new什么啊,南辕北辙。 问题在前面的函数里,前面的函数里有错误判断,不成功返回nothing对象。 在登录界面代码里也应判断返回的是否nothing。如果是nothing,就分析是否正常,一般是SQL语句容易出错,用msgbox输出一下那个SQL字符串的结果,看是否正确
"Data Source=127.0.0.1; Initial Catalog=TuiMag; " & _
"User ID=sa; Password="
new什么啊,南辕北辙。 问题在前面的函数里,前面的函数里有错误判断,不成功返回nothing对象。 在登录界面代码里也应判断返回的是否nothing。如果是nothing,就分析是否正常,一般是SQL语句容易出错,用msgbox输出一下那个SQL字符串的结果,看是否正确
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询