PC Review


Reply
Thread Tools Rate Thread

Build SQL Statements w/Functions, Variables or Controls in VB

 
 
Robert P.
Guest
Posts: n/a
 
      2nd Oct 2003
I can not get the SQL Statement to run in VB. I am trying
to create a query that will use DateDiff function to
select policies that are older than 36 months. Any ideas?

strSQL = "SELECT * FROM [Policies] WHERE" & DateDiff
("m", "[Date Published]", Now()) & ">" & "36;"

Do you know of any good reference material on the subject
of Building SQL Statements w/Functions, Variables or
Controls in VB? I have taken a SQL class but writing SQL
statements in code seems to be a different type of beast.
Thank You.
 
Reply With Quote
 
 
 
 
Marshall Barton
Guest
Posts: n/a
 
      3rd Oct 2003
Robert P. wrote:

>I can not get the SQL Statement to run in VB. I am trying
>to create a query that will use DateDiff function to
>select policies that are older than 36 months. Any ideas?
>
>strSQL = "SELECT * FROM [Policies] WHERE" & DateDiff
>("m", "[Date Published]", Now()) & ">" & "36;"
>
>Do you know of any good reference material on the subject
>of Building SQL Statements w/Functions, Variables or
>Controls in VB? I have taken a SQL class but writing SQL
>statements in code seems to be a different type of beast.


If you know how to write SQL statements, then you can debug
the code that constructs SQL by using a Debug,Print strSQL
right after the above line of code. This will let you see
what was constructed and it's usually pretty obvious what
happened in the code.

In the above case you have the DateDiff outside the quotes
so [Date Published] is refering to something in your
procedure, noy to a field in the table. From what I can
tell, you don't need to be calculating a value in that where
clause:

strSQL = "SELECT * FROM [Policies] WHERE DateDiff("m", [Date
Published], Now()) > 36;"

--
Marsh
MVP [MS Access]
 
Reply With Quote
 
Tim Ferguson
Guest
Posts: n/a
 
      3rd Oct 2003
Marshall Barton <(E-Mail Removed)> wrote in
news:(E-Mail Removed):

>
> strSQL = "SELECT * FROM [Policies] WHERE DateDiff("m", [Date
> Published], Now()) > 36;"
>
>


Ahem...

"SELECT * FROM [Policies] " & _
"WHERE DateDiff(""m"", [Date Published], Now()) > 36;"


just a matter of the quotes around the ""m""...

All the best


Tim F


 
Reply With Quote
 
Marshall Barton
Guest
Posts: n/a
 
      4th Oct 2003
Tim Ferguson wrote:

>Marshall Barton <(E-Mail Removed)> wrote in
>news:(E-Mail Removed):
>
>>
>> strSQL = "SELECT * FROM [Policies] WHERE DateDiff("m", [Date
>> Published], Now()) > 36;"
>>
>>

>
>Ahem...
>
>"SELECT * FROM [Policies] " & _
>"WHERE DateDiff(""m"", [Date Published], Now()) > 36;"
>
>
>just a matter of the quotes around the ""m""...


Good catch Tim.

--
Marsh
MVP [MS Access]
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Variables in SQL statements Jim Jones Microsoft Access VBA Modules 4 30th Nov 2008 11:09 AM
Optional Variables in Functions (REPLACE and other 'Worksheet' functions) Evi Microsoft Access Queries 2 6th May 2008 09:59 PM
variables and for next statements =?Utf-8?B?QmlnZ2xlcw==?= Microsoft Access Form Coding 2 2nd Jun 2006 09:52 PM
if statements and variables =?Utf-8?B?REFWSURQRU9WRVI=?= Microsoft Access VBA Modules 4 19th Apr 2005 05:35 PM
Conditional Functions - logical functions based on volatile variables WorkerB5 Microsoft Excel Worksheet Functions 2 14th May 2004 11:20 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:26 PM.