Converting query to string

  • Thread starter Thread starter Col
  • Start date Start date
C

Col

Guest
Hi,
can anyone tell me how to enter the following
WHERE clause into a string in VBA?

[Request Date] BETWEEN (DateAdd("ww", -1, Forms![Weekly Summary]![cboWeek])) AND Forms![Weekly Summary]![cboWeek]

I need to store this as a string in order to set the form's filter from code.

Thanks in advance
 
You would just set the string to it.
Code:
Dim YourSQL As String
YourSQL = "[Request Date] BETWEEN (DateAdd("ww", -1, Forms![Weekly Summary]![cboWeek])) AND Forms![Weekly Summary]![cboWeek]"

Then call it this way:

Code:
[Forms]![Weekly Summary].RecordSource = "SELECT * FROM [YourTableName] WHERE " & YourSQL

HTH

[This message has been edited by shacket (edited 09-26-2001).]
 
If you have more than a single year's worth of data in your table, you'll need to include year in your selection criteria.
 
Trouble is that you can't just enter the string as suggested - it interprets it as stopping at the second " mark (ie before the ww in the DateAdd function). I'm just wondering how to include the reserved operators (such as ") in the code.
 
Col,

Create a constant

Public Const QUOTES = """"

Then when you need quotation marks all you have to do is insert & QUOTES & instead of putting in quotation marks. It works for me. It's also cleaner looking.

Mike
 
Hi,

My previous reply was not complete. See code below:

dim strCompany as string
dim strSQL as string

strSQL = "SELECT * FROM table WHERE Company = " & QUOTES & strCompany & QUOTES

This is needed because strCompany is a string.

Thanks,

Mike
 

Users who are viewing this thread

Back
Top Bottom