程序一到mrc.eof就出现错误"对象变量未设置或With块变量未设置"不知道为什么

niuma5266 2003-08-24 08:05:43

最近买了一本书,书中的例子学生信息管理系统,书买来时附带一张光盘,是原码文件才用ADO连接,按书中说的把文件考到查询分析器中建立数据结构就可运行程序可是一到mrc.eof判断语句就出现错误原码如下:
公共模块代码为:
Public fMainForm As frmMain
Public UserName As String


Sub Main()
Dim fLogin As New frmLogin
fLogin.Show vbModal
If Not fLogin.OK Then
'Login Failed so exit app
End
End If
Unload fLogin


Set fMainForm = New frmMain
fMainForm.Show
End Sub
Public Function ConnectString() _
As String
'returns a DB ConnectString
ConnectString = "FileDSN=studentinfo.dsn;UID=sa;PWD="
End Function
Public Function ExecuteSQL(ByVal SQL _
As String, MsgString As String) _
As ADODB.Recordset
'executes SQL and returns 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

Public Function Testtxt(txt As String) As Boolean
If Trim(txt) = "" Then
Testtxt = False
Else
Testtxt = True
End If
End Function


frmlogin窗体原码
Option Explicit
Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpbuffer As String, nSize As

Long) As Long


Public OK As Boolean
'记录确定次数
Dim miCount As Integer
Private Sub Form_Load()
Dim sBuffer As String
Dim lSize As Long


sBuffer = Space$(255)
lSize = Len(sBuffer)
Call GetUserName(sBuffer, lSize)
If lSize > 0 Then
txtUserName.Text = ""

Else
txtUserName.Text = vbNullString
End If


OK = False
miCount = 0
End Sub



Private Sub cmdCancel_Click()
OK = False
Me.Hide
End Sub


Private Sub cmdOK_Click()
Dim txtSQL As String
Dim mrc As ADODB.Recordset
Dim MsgText As String
'ToDo: create test for correct password
'check for correct password

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 (就在这里出现错误的为黄色标记)









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
End If

miCount = miCount + 1
If miCount = 3 Then
Me.Hide
End If
Exit Sub
End Sub
...全文
114 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
aiur2000 2003-08-25
  • 打赏
  • 举报
回复
Dim txtSQL As String
Dim mrc As ADODB.Recordset
Dim MsgText As String

set mrc = new adodb.recordset
txtSQL = "select * from user_Info where user_ID = '" & txtUserName.Text & "'"
Set mrc = ExecuteSQL(txtSQL, MsgText)
这就是我们平时说的老3句了,哈哈哈,有点无聊的。
aiur2000 2003-08-25
  • 打赏
  • 举报
回复
"对象变量未设置或With块变量未设置"有以下几点可能出现的错误:
1。未定义记录集
2。未安装mdac
3。sql写法有错误
unkowme 2003-08-25
  • 打赏
  • 举报
回复
在定义记录集之后加上一句
set mrc = new adodb.recordset
应该就可以!!!
tiandiqing 2003-08-25
  • 打赏
  • 举报
回复
如果我没有说错的话,你买的是人民邮电出版社的一本,书名叫《什么数据库开发实例导航》
错误出在mrc没有定义,另外你的connectstring那个连接一定要写对,只要把这两点搞好,应没有问题了!!!
neo40 2003-08-24
  • 打赏
  • 举报
回复
函数定义的太罗嗦了,建议你看看别的书,和别人的原代码虽然很规范但好象已经很少人用了
neo40 2003-08-24
  • 打赏
  • 举报
回复
这句
Public Function ExecuteSQL(ByVal SQL _
As String, MsgString As String) _
As ADODB.Recordset
改成这句
Public Function ExecuteSQL(ByVal SQL As String, MsgString As String) As New ADODB.Recordset

因为没分配新的记录集,所以OPEN的方法是打不开的
LCAAA 2003-08-24
  • 打赏
  • 举报
回复
你试试这样写呢!应该就没问题了。
首先定义Public cn As New ADODB.Connection
然后把你打开表的语句改成这样的就可以了。
mrc.open txtSQL ,cn

1,216

社区成员

发帖
与我相关
我的任务
社区描述
VB 数据库(包含打印,安装,报表)
社区管理员
  • 数据库(包含打印,安装,报表)社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧