简体   繁体   English

MS Access VBA 中的 SQL 指令

[英]SQL instruction in MS Access VBA

The Chr(34) idea gives Chr(34) 的想法给出

MsgBox1 SELECT * FROM Emp WHERE JobNumber = BFLA
MsgBox2 "SELECT * FROM Emp WHERE JobNumber = BFLA"

which looks as though it should work in OpenRecordset but gives the "can't find table" message for "'SELECT....BFLA'"看起来好像它应该在 OpenRecordset 中工作,但给出了“'SELECT ....BFLA'”的“找不到表”消息

The '" & xx & "'" idea gives '" & xx & "'" 的想法给出

MsgBox1 SELECT * FROM Emp WHERE JobNumber = 'BFLA'
MsgBox2 "SELECT * FROM Emp WHERE JobNumber = 'BFLA'"

which also gives the "can't find table" message for "'SELECT....'BFLA'" (extra single quote before BFLA)这也给出了“'SELECT ....'BFLA'”的“找不到表”消息(BFLA之前的额外单引号)

The original code which worked was:有效的原始代码是:

xx = Me![JobID]

Set r1 = db.OpenRecordset("Select * from Emp where EmployeeID = " & Nz(Me![JobID]))

In this code EmployeeID was a numerical value在这段代码中,EmployeeID 是一个数值

The latest code is:最新的代码是:

xx = Me![JobNumber] ' the JobNumber from the Combo box which is tested in MsgBox 1 and is correct

xxSQL = "SELECT * FROM Emp WHERE JobNumber = '" & xx & "'"

MsgBox xxSQL, vbOKOnly

xxSQL = Chr(34) & xxSQL & Chr(34)

MsgBox xxSQL, vbOKOnly ' which shows what will go into the OpenRecordset

Set r1 = CurrentDb.OpenRecordset(xxSQL)

and sorry about the poor formatting last time, hopefully I've got the hang of the message box...很抱歉上次格式不正确,希望我已经掌握了消息框...

Chris克里斯

Original message below以下为原帖

I am trying to fix some existing Access code because a source application has been changed and I'm really struggling to get a variable into a Select statement into SQL.我正在尝试修复一些现有的 Access 代码,因为源应用程序已更改,而且我真的很难将变量放入 SQL 的 Select 语句中。 I've read many solved problems that are similar to this and tried many permutations of """ before, after and in the middle of this Select, and tried splitting it up.我已经阅读了许多与此类似的已解决问题,并在此 Select 之前、之后和中间尝试了许多“””排列,并尝试将其拆分。

xx = Me![JobNumber]
xxSQL = "SELECT * FROM Emp WHERE JobNumber = " & xx
MsgBox xxSQL, vbOKOnly
xxSQL = """ & xxSQL & """
MsgBox xxSQL, vbOKOnly
Set r1 = CurrentDb.OpenRecordset(xxSQL)

The JobNumber is now a 4char string that cycles through various jobs from a Combo Box and produces a report page for each job. JobNumber 现在是一个 4 个字符的字符串,它从组合框中循环浏览各种作业,并为每个作业生成一个报告页面。 The first MsgBox generates SELECT * FROM Emp WHERE JobNumber = BFLA (a valid code)第一个 MsgBox 生成 SELECT * FROM Emp WHERE JobNumber = BFLA(有效代码)

Table Emp exists and has a field JobNumber which also contains 4char strings (including BFLA) which match the Me!表 Emp 存在并且有一个字段 JobNumber,其中还包含与 Me! 匹配的 4 个字符字符串(包括 BFLA)。 Combo box.组合框。

The second MsgBox produces "& xxSQL &" and then an Access error message - can't find the input table or query "'& xxSQL &"' because I can't get the number and placement of the double quotes right.第二个 MsgBox 生成“& xxSQL &”,然后是 Access 错误消息 - 找不到输入表或查询“'& xxSQL &”',因为我无法正确获取双引号的数量和位置。

The report then generates, repeating the 2 MsgBoxes as it cycles through the Combo box list, and the reports are fine except for missing the content from the Emp table.然后生成报告,重复 2 个 MsgBoxes,因为它在组合框列表中循环,报告很好,除了缺少 Emp 表中的内容。 There have been no changes to the Emp table other than the introduction of JobNumber, and no changes to the code following the Set r1.除了引入 JobNumber 之外,Emp 表没有任何变化,并且 Set r1 之后的代码没有变化。

I fear that this is a 2-sec fix for someone with more experience than I, and it will be embarrassing, but I just can't fix it !我担心这对于比我更有经验的人来说是一个 2 秒的修复,这会很尴尬,但我无法修复它!

Thanks谢谢

Chris克里斯

Try this - using quotes as xx is text:试试这个 - 使用引号作为xx是文本:

xx = Me![JobNumber]
xxSQL = "SELECT * FROM Emp WHERE JobNumber = '" & xx & "'"
MsgBox xxSQL, vbOKOnly
Set r1 = CurrentDb.OpenRecordset(xxSQL)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM