Syntax to change Marcro query to VBA query

Posted on 2013-11-12
Last Modified: 2013-11-12
I have a macro that calls an existing query to run.  I am updating my code to move the macro and use VBA.

I can't determine the syntax for the following SQL command:

 DoCmd.RunSQL "UPDATE Tpatient SET Tpatient.RecallNotice = Date(), Tpatient.RecallNumber = [RecallNumber]+1
WHERE (((Tpatient.RecallNumber)=0) AND ((Tpatient.RecallActive)=-1) AND ((Date()-[TPatient]![LastApmnt])>([TPatient]![RecallFreq]*30))) OR (((Tpatient.RecallNumber)=1) AND ((Tpatient.RecallActive)=-1) AND ((Date()-[TPatient]![RecallNotice])>30) AND ((Tpatient.RecallFreq)>=12)) OR (((Tpatient.RecallNumber)<4) AND ((Tpatient.RecallActive)=-1) AND ((Date()-[TPatient]![LastApmnt])>360) AND ((Date()-[TPatient]![RecallNotice])>30) AND ((Tpatient.RecallFreq)<12))"

Any assistance is create appciated.
Question by:thandel
  • 4
  • 3

Author Comment

ID: 39642006
I've attached a screen shot as it may help clafiry.
LVL 61

Expert Comment

ID: 39642095
You're using .'s and !'s interchangeably.   Unless there is a form involved named Tpatient as well as a table, use all .'s:
Dim strSQL as string
strSQL =  "UPDATE Tpatient SET Tpatient.RecallNotice = Date(), Tpatient.RecallNumber = [RecallNumber]+1
WHERE (((Tpatient.RecallNumber)=0) AND ((Tpatient.RecallActive)=-1) AND ((Date()-[TPatient].[LastApmnt])>([TPatient]![RecallFreq]*30))) OR (((Tpatient.RecallNumber)=1) AND ((Tpatient.RecallActive)=-1) AND ((Date()-[TPatient].[RecallNotice])>30) AND ((Tpatient.RecallFreq)>=12)) OR (((Tpatient.RecallNumber)<4) AND ((Tpatient.RecallActive)=-1) AND ((Date()-[TPatient].[LastApmnt])>360) AND ((Date()-[TPatient].[RecallNotice])>30) AND ((Tpatient.RecallFreq)<12))"
 DoCmd.RunSQL strSQL

Open in new window

LVL 61

Accepted Solution

mbizup earned 500 total points
ID: 39642145
Simplifying and formatting the code for readability.  You actually don't need the prefixes since there is only one table involved:

Dim strSQL as string
strSQL =  "UPDATE Tpatient " _ 
                & "SET RecallNotice = Date(), " _
                &  "RecallNumber = [RecallNumber]+1 " _
                &  "WHERE (((RecallNumber)=0)  " _
                &   "AND ((RecallActive)=-1)  " _
                &   "AND ((Date()- [LastApmnt])>([RecallFreq]*30))) " _
                &   "OR (((RecallNumber)=1) AND ((RecallActive)=-1)  " _
                &    "AND ((Date()-[RecallNotice])>30) " _
                &    "AND ((RecallFreq)>=12)) OR (((RecallNumber)<4) " _
                &    "AND ((RecallActive)=-1) " _
                &    "AND ((Date()-[LastApmnt])>360) " _
                &     "AND ((Date()-[RecallNotice])>30) "
                &    "AND ((RecallFreq)<12))"
 DoCmd.RunSQL strSQL

Open in new window

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.


Author Comment

ID: 39642174
Thanks was all that was needed was to place the original SQL into a string and then use that string?
LVL 61

Expert Comment

ID: 39642196
Pretty much - When there are only tables involved (no form criteria, etc) it is generally just that simple.

Access automatically throws in a lot of garbage - extra parentheses, prefixes, etc which I usually try to clean up for readability.

Form references/criteria however can complicate things  -- you need to separate the references out from the SQL strings so that they get evaluated separately (You have to treat them like variables), so those are never quite this straight forward.
LVL 61

Expert Comment

ID: 39642199
All that said, I believe the main problem with your original post was the !'s where there should have been .'s

Author Comment

ID: 39642237
Thanks again, SQL is my weak spot but your eplanations certainly helped.

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Access check if a table is open 4 43
Should I keep recordsets open? 3 24
Visible ppty on rpt 7 7
average of calculation (TSQL) 4 9
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question