0

Could any expert tell me why I always get this error "Expected:End of Statement" when I run the following code in Access VBA :

FNSQL = DoCmd.RunSQL _
          "SELECT First_Name FROM tblPatient_Info WHERE Patient_ID = '" & 1967 & "';"

Thank you so much in advance!

2
  • 1
    What is FNSQL and why are you assigning it to DoCmd.RunSQL method? Commented Jul 29, 2014 at 19:08
  • 3
    DoCmd.RunSQL executes SQL statements only for action queries ( INSERT, DELETE, UPDATE, etc..). If you want to execute Select query, use Recordset Commented Jul 29, 2014 at 19:25

4 Answers 4

3

Your code is wrong in a couple different ways.

You don't tell us what FNSQL is, so one can only imagine.

Mistake #1

Mentioned by @simoco, DoCmd.RunSQL is used in the following scenarios:

Here's the API: DoCmd.RunSQL SQLStatement UseTransaction

SQLStatement : A string expression that's a valid SQL statement for an action query or a data-definition query. It uses an INSERT INTO, DELETE, SELECT...INTO, UPDATE, CREATE TABLE, ALTER TABLE, DROP TABLE, CREATE INDEX, or DROP INDEX statement.

Include an IN clause if you want to access another database.

Mistake #2

FNSQL I'm not sure what you are aiming to do with the results of the query, but this is for your general knowledge because if you didn't know the RunSQL syntax you may be unfamiliar with Recordsets.

You can assign your stored query's results to a Recordset and do with it as you please, much like a table.

Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim SQL As String
Dim firstName As String

SQL = "SELECT First_Name FROM tblPatient_Info WHERE Patient_ID = 1967;"
' OR
SQL = "SELECT First_Name FROM tblPatient_Info WHERE Patient_ID = '" & 1967 & "';"
' You can write Debug.Print SQL to make sure you're getting the right SQL.

Set db = CurrentDb
Set rs = db.OpenRecordset(SQL)
' The disadvantage of this approach is that the query string must be compiled each time it runs
' OR
Set rs = db.OpenRecordset(YourSaveQueryName)
'This would be the faster method of the 2.

You can then manipulate your data however you want, and loop through the entire recordset.

firstName = "Larry"

If rs.BOF And rs.EOF Then
' Do nothing, the recordset is empty
Else
   Do Until rs.EOF
      rs.Edit
      rs!FirstName = firstName 'This is how you access properties in the recordset
      rs.Update
      rs.MoveNext
Loop

etc.

Sign up to request clarification or add additional context in comments.

2 Comments

Only one note to this great answer: on VBScript you can use & to add string to numbers, so in VBapplication is recommended that identify the type and this case is better patient_id = ' " & cstr(1967) & " ' " No need to put the last ; on SQL sequence.
@Mastercafe Thanks for that. I enjoy when people clean up code snippets. Feel free to ever edit anything you see incorrect! Cheers
0

I think you need parentheses around your SELECT statement when you pass it to RunSQL, because it's called as a function that returns "something" that gets assigned to FNSQL. So something like this:

FNSQL = DoCmd.RunSQL( _
          "SELECT First_Name FROM tblPatient_Info WHERE Patient_ID = '" & 1967 & "';")

In fact, looking at the RunSQL documentation, RunSQL doesn't return anything so, even if you fix your original issue, you'll still find there's an error there.

Furthermore, again according to the documentation, you can only execute SELECT ... INTO statements with RunSQL. For simple SELECT ... FROM statements, simoco is right -- you need to use a Recordset.

Comments

0

2 errors.

First, DoCmd.RunSQL won't do anything with just a select. Maybe you are missing your action (delete or something) or maybe you want to read it, so you should use CurrentDb.OpenRecordset . You'll need to post more code so we can better understand where you are going with this.

Second, if Patient_ID is an integer (and I'm guessing it is), you don't need the '. You don't need the ; either.

So the query should look like this:

varInt = 1967
"SELECT First_Name FROM tblPatient_Info WHERE Patient_ID = " & varInt

If you don't want to use a var for the int, just insert it directly into the string like so:

"SELECT First_Name FROM tblPatient_Info WHERE Patient_ID = 1967"

In case Patient_ID is a string, you will indeed need ', but you are also missing " around the string, so it should look like this:

"SELECT First_Name FROM tblPatient_Info WHERE Patient_ID = '" & "1967" & "'"

3 Comments

You are aware that the question is almost 2 years old and was already answered comprehensively back then?
Loooool good call. For some reason it showed up in my search in the first page. I was under the impression the search was ordered by new entries, so I probably had a mistype on the search query (that is also mistyped here somewhere)
Also, no one had mentioned the integer and string difference in the query explicitly. IDs are usually integers, so it made sense to bring it up.
0

Not a correct answer to this question but it's worth noting I just wanted to see if a row existed in the database. I assumed that would require me to write some sort of select or result query. However access has a separate function just to run count operations.

So this code I ended up with:

Dim count As Long
count = DCount("release_id", "list_releases", "list_id = 1 AND release_id = " & Me!release_id)
If count Then
  Me!owned.Enabled = False
  MsgBox "You own this release."
End If

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.