vba 运行时错误“3065”无法执行选择查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25906567/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 04:34:58  来源:igfitidea点击:

Run-time error '3065' Cannot execute a select query

vbams-accessaccess-vba

提问by user3029925

I have written lots of the queries but I'm struggling with this one.

我已经写了很多查询,但我正在努力解决这个问题。

I get the run-time error 3065 when I run the following sql.

当我运行以下 sql 时,出现运行时错误 3065。

Dim db As DAO.Database
Dim sqlstring As String

Set db = DBEngine(0).Databases(0)


sqlstring = "SELECT ebk.hr_leav_amnt AS hr_clia_hour, ebk.hr_leav_type, ebk.hr_leav_code, ebk.hr_empl_code, ebk.hr_loadg_amt AS hr_loadg_amt, 'Leave Pay' AS hr_provision, mst.hr_paym_code, mst.hr_base_hour, '' AS hr_splt_accr, mst.hr_leav_abbr, ype.hr_norm_pcnt, ype.hr_allw_amnt"
sqlstring = sqlstring + " FROM hrtlvebk AS ebk, hrtlvmst AS mst, hrtptype AS ype"
sqlstring = sqlstring + " WHERE ebk.hr_leav_code Like 'a%' And ebk.hr_leav_code = [mst].[hr_leav_code] And ebk.hr_leav_type Like '1%' And bk.hr_leav_type = [mst].[hr_leav_type] And ebk.hr_recd_type = 'a' And ebk.hr_lbkg_refn = 'ACCRUAL' And ebk.hr_from_dati >= 20140701 And ebk.hr_from_dati <= 20140730 And mst.hr_load_rule <> 'y' And mst.hr_paym_code = [ype].[hr_paym_code]"
sqlstring = sqlstring + " GROUP BY ebk.hr_leav_amnt, ebk.hr_leav_type, ebk.hr_leav_code, ebk.hr_empl_code, ebk.hr_loadg_amt, mst.hr_paym_code, mst.hr_base_hour, mst.hr_leav_abbr, ype.hr_norm_pcnt, ype.hr_allw_amnt"
db.Execute sqlstring, dbFailOnError

When I run statement with Query (SQL) it works fine. The only thing I change is the text in the where clause.. ('a%' - Query it is "a%")

当我使用查询(SQL)运行语句时,它工作正常。我唯一改变的是 where 子句中的文本.. ('a%' - Query it is "a%")

Thank you in advance.

先感谢您。

John

约翰

回答by 4dmonster

The message is true for SELECTqueries you should use Openrecordsetto be able to retrieve results of selection. Executeis for 'command' queries that don't return values.

该消息适用于SELECT您应该Openrecordset用来检索选择结果的查询。Execute用于不返回值的“命令”查询。