Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Syntax to change Marcro query to VBA query

Posted on 2013-11-12
7
Medium Priority
?
267 Views
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.
0
Comment
Question by:thandel
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 

Author Comment

by:thandel
ID: 39642006
I've attached a screen shot as it may help clafiry.
Query.jpg
0
 
LVL 61

Expert Comment

by:mbizup
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

0
 
LVL 61

Accepted Solution

by:
mbizup earned 2000 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

0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

Author Comment

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

Expert Comment

by:mbizup
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.
0
 
LVL 61

Expert Comment

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

Author Comment

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

Featured Post

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

704 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