简体   繁体   中英

SQL instruction in MS Access VBA

The Chr(34) idea gives

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'"

The '" & xx & "'" idea gives

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)

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

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. 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.

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. The first MsgBox generates SELECT * FROM Emp WHERE JobNumber = BFLA (a valid code)

Table Emp exists and has a field JobNumber which also contains 4char strings (including BFLA) which match the Me! 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.

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. 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.

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 !

Thanks

Chris

Try this - using quotes as xx is text:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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