这段程序
Dim mrc As ADODB.Recordset
dim txtSQL as string............ txtSQL = "select * from Test_Infor group by Test_Chap "
Set mrc = ExecuteSQL(txtSQL, MsgText)
If Not mrc.BOF Then
mrc.MoveFirst
End If
.....
每次运行运行,都显示对象变量或with块变量未设置
选择调试后,"If Not mrc.BOF Then"显示黄色
去掉"group by Test_Chap "后程序正常
注:Test_Chap 为String,对应数据库中的一个字段
我怀疑该错误是由于没有找到任何记录引起的,但就是不知道怎么改!
Dim mrc As ADODB.Recordset
dim txtSQL as string............ txtSQL = "select * from Test_Infor group by Test_Chap "
Set mrc = ExecuteSQL(txtSQL, MsgText)
If Not mrc.BOF Then
mrc.MoveFirst
End If
.....
每次运行运行,都显示对象变量或with块变量未设置
选择调试后,"If Not mrc.BOF Then"显示黄色
去掉"group by Test_Chap "后程序正常
注:Test_Chap 为String,对应数据库中的一个字段
我怀疑该错误是由于没有找到任何记录引起的,但就是不知道怎么改!
If Not mrc.BOF Then
mrc.MoveFirst
End If
end if
If Not mrc.BOF And Not mrc.EOF Then
Microsoft Access versions 1.x and 2.0 and Microsoft Visual Basic version 3.0 used earlier versions of DAO. Several objects, methods, properties, and statements in those earlier versions are considered "obsolete" but are still supported for backwards compatibility with existing user code.The following is a list of DAO methods, properties, objects, and statements that have been replaced by more powerful, flexible, and easy-to-use features. Each obsolete feature in the list has a corresponding replacement feature.
|------------------------------|-----------------------|
| Obsolete feature | Replacement feature |
|------------------------------|-----------------------|
|Database.ExecuteSQL method | Execute method |
|______________________________|_______________________|由此可见,ExecuteSQL 只是为了向前兼容(supported for backwards compatibility )而保留的,但从 Dim mrc As ADODB.Recordset 分析可知,目前的版本是高级ADO版本,有可能是早期的 ExecuteSQL 不支持 Group By 关键字所引起的吧!
Set mrc =dbSample.ExecuteSQL(txtSQL, MsgText) (我加了个 dbSample),返回Nothing ,使用 mrc.BOF 将引起错误(因为 mrc “对象变量未设置”)
(使用了数学函数的字段除外)
dim txtSQL as string............ txtSQL = "select * from Test_Infor group by Test_Chap "
Set mrc = ExecuteSQL(txtSQL, MsgText)
If mrc.BOF and mrc.eof Then
msgbox "没有找到相关记录!!!"
else
mrc.MoveFirst
End If
错误~
Set mrc = ExecuteSQL(txtSQL, MsgText)
If mrc.BOF and mrc.eof Then
msgbox "没有找到相关记录!!!"
else
mrc.MoveFirst
End If1。你的查询语句有问题,没有聚合函数为什么用GROUP BY?
2。你监视一下EXECUTESQL是否返回了一个RECORDSET对象?即MRC是否为NOTHING ?如果为NOTHING ,当然后面会出错。
.------
设置参数
.------end with
Set mrc = ExecuteSQL(txtSQL, MsgText)
If Not isNULL(Test_Chap) Then
mrc.MoveFirst
End If
那么多人都误人子弟!
不懂就不要乱回贴,只有一两个人说到问题的重点。
重点是:SQL语句出错,
用group by必须在select中写明该字段,前后都得有。
看一下SQL2000 的联机丛书
唉!可悲可叹!