Criteria in SQL String

TallMan

Registered User.
Local time
Today, 15:33
Joined
Dec 5, 2008
Messages
239
Good Morning All,

I have a tricky one here, at least it is tricking me. It is a rather simple concept but cannot get this to work 100%. I will try to make this as short as possible.

In my VBA code below. I first check what day it is. If it is any day other than Friday then the query pulls date() + 7 from the "paydate field". If the Day is Friday then I pull dat() +7, Date()+8 and Date()+9 from the paydate field.

After I establish which days to pull I find out what month we are in so that I can pull the appropriate records from the "Pay_Month" field.

The code is running fine. It finds the day, then pulls the information and exports it to excel.

The problem is the code is pullling ALL of the records for the day I am looking for. For example, today is the 18th and it is not Friday so the query should pull all the records that have a day of 25. The query is skipping the "Pay_month" field and pulling every record with a 25. Depending on what month we are in I should only be pulling certain month records.

****Here is the really tricky part. When I populate a msgbox with the sql code it pulls the exact criteria I need, however after the query executes and populates the spreadsheet all of the monthly records are there!

Does this make sense? PLease post back with questions.:o



<FONT size=3><FONT face=Calibri>
PHP:
Private Sub Command140_Click()
Dim db As DAO.Database
Set db = CurrentDb
 
Dim sqldaily As String
Dim PPY_Excel As Object
Set PPY_Excel = CreateObject("Excel.Application")
'----------------------
Dim criteria1 As String
Dim criteria2 As String
 
If Weekday(Date) = 6 Then
criteria1 = Day(Date + 7) & " OR " & Day(Date + 8) & " OR " & Day(Date + 9)
Else
criteria1 = Day(Date + 7)
End If
 
If Month(Date) = "1" Then
criteria2 = "'Monthly' Or 'Jan-April-July-Oct' OR 'Jan-Annual'"
    ElseIf Month(Date) = "2" Then
    criteria2 = "'Monthly' Or 'Feb-May-Aug-Nov' Or 'Feb-Annual'"
        ElseIf Month(Date) = "3" Then
        criteria2 = "'Monthly' Or 'March-June-Sept-Dec' Or 'March-Annual'"
            ElseIf Month(Date) = "4" Then
            criteria2 = "'Monthly' Or 'Jan-April-July-Oct' Or 'April-Annual'"
                ElseIf Month(Date) = "5" Then
                criteria2 = "'Monthly' Or 'Feb-May-Aug-Nov' Or 'May-Annual'"
                    ElseIf Month(Date) = "6" Then
                    criteria2 = "'Monthly' Or 'March-June-Sept-Dec' Or 'June-Annual'"
                        ElseIf Month(Date) = "7" Then
                        criteria2 = "'Monthly' Or 'Jan-April-July-Oct' Or 'July-Annual'"
                            ElseIf Month(Date) = "8" Then
                            criteria2 = "'Monthly' Or 'Feb-May-Aug-Nov' Or 'Aug-Annual'"
                                ElseIf Month(Date) = "9" Then
                                    criteria2 = "'Monthly' Or 'March-June-Sept-Dec' Or 'Sept-Annual'"
                                    ElseIf Month(Date) = "10" Then
                                        criteria2 = "'Monthly' Or 'Jan-April-July-Oct' Or 'Oct-Annual'"
                                        ElseIf Month(Date) = "11" Then
                                            criteria2 = "'Monthly' Or 'Feb-May-Aug-Nov' Or 'Nov-Annual'"
                                            Else: criteria2 = "'Monthly' Or 'March-June-Sept-Dec' Or 'Dec-Annual'"
 
End If
'----------------------
 
sql1 = "INSERT INTO Main_PPY_TBL_TMP ( Account, Amount, PPM_Code, Pay_Date, Pay_Month, Starts_On ) "
sql1 = sql1 & "SELECT Main_PPY_TBL.Account, Main_PPY_TBL.Amount, Main_PPY_TBL.PPM_Code, Main_PPY_TBL.Pay_Date, Main_PPY_TBL.Pay_Month, Main_PPY_TBL.Starts_On "
sql1 = sql1 & "FROM Main_PPY_TBL "
sql1 = sql1 & "WHERE (Main_PPY_TBL.Pay_Date = " & criteria1 & ") AND (Main_PPY_TBL.Pay_Month = " & criteria2 & ") "
sql1 = sql1 & "ORDER BY Main_PPY_TBL.Account, Main_PPY_TBL.Pay_Date;"
MsgBox sql1
db.Execute (sql1)
 
'Send the information to excel on the associates C:drive
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "Main_PPY_TBL_TMP", "C:\Process_PPY.xls", True
'Open Workbook
PPY_Excel.workbooks.Open "C:\Process_PPY.xls"
PPY_Excel.Visible = True
'Delete all records from temp tbl
SQL = "DELETE * FROM Main_PPY_TBL_TMP"
db.Execute (SQL)
End Sub
 
Tip to improve performance.

Create an array as follows, and place it either into the forms declarations section or in a standard module.

Code:
Dim MnthArray(11)

MnthArray(0) = "'Monthly' Or 'Jan-April-July-Oct' OR 'Jan-Annual'"
MnthArray(1) = "'Monthly' Or 'Feb-May-Aug-Nov' Or 'Feb-Annual'"
MnthArray(2) = "'Monthly' Or 'March-June-Sept-Dec' Or 'March-Annual'"
MnthArray(3) ="'Monthly' Or 'Jan-April-July-Oct' Or 'April-Annual'"
MnthArray(4) ="'Monthly' Or 'Feb-May-Aug-Nov' Or 'May-Annual'"
MnthArray(5) ="'Monthly' Or 'March-June-Sept-Dec' Or 'June Annual'"
MnthArray(6) = "'Monthly' Or 'Jan-April-July-Oct' Or 'July-Annual'"
MnthArray(7) ="'Monthly' Or 'Feb-May-Aug-Nov' Or 'Aug-Annual'"
MnthArray(8) ="'Monthly' Or 'March-June-Sept-Dec' Or 'Sept-Annual'"
MnthArray(9) ="'Monthly' Or 'Jan-April-July-Oct' Or 'Oct-Annual'"
MnthArray(10) ="'Monthly' Or 'Feb-May-Aug-Nov' Or 'Nov-Annual'"
MnthArray(11) ="'Monthly' Or 'March-June-Sept-Dec' Or 'Dec-Annual'"

Then in your code instead of using the long winded if, else if statement

Code:
criteria2 = MnthArray(Month(Date) -1)

Also is Main_PPY_TBL.Pay_Date a Date field or a number field?
 
DCrake,

Thanks a ton for your response. I have read about Arrays but never had a full understanding or "need" for them. Could you please expand more on the sentence below?

"place it either into the forms declarations section or in a standard module"

I am not familiar with the declaration area of the form. I looked under form properties but could not find anything. I am not sure with the standard module piece either. Sorry I am slowly learning this stuff.

To answer your question, the Main_PPY_TBL.Pay_Date is a number field that goes from 1-31 for the number of days that could be in the month.

Thanks again!
 
Declarations area:

When you design a form and view the VBA code area, if you click on the combo to the left and select (General) you will see on the right hand one one that says (Declarations) this is the area where you can end such things as variables and the like. Outside of and subs and functions.

Standard Module:
Create a module that can be addressed at any time from anywher in the application.

Also instead of using

Day(Date + 7) & " OR " & Day(Date + 8) & " OR " & Day(Date + 9)

try

Criteria1 = Day(Date + 7) & ", " & Day(Date + 8) & ", " & Day(Date + 9)


WHERE (Main_PPY_TBL.Pay_Date In(" & criteria1 & ")).....

becomes
WHERE (Main_PPY_TBL.Pay_Date In(25,26,27)
 
Thank you DCrake.
I will try this and post back with the outcome!
 
OKay,

I have posted the code below in the "general", "declarations" section of the vba module that the rest of my code is on for this form.

PHP:
Dim MnthArray(11)
MnthArray(0) = "'Monthly' Or 'Jan-April-July-Oct' OR 'Jan-Annual'"
MnthArray(1) = "'Monthly' Or 'Feb-May-Aug-Nov' Or 'Feb-Annual'"
MnthArray(2) = "'Monthly' Or 'March-June-Sept-Dec' Or 'March-Annual'"
MnthArray(3) = "'Monthly' Or 'Jan-April-July-Oct' Or 'April-Annual'"
MnthArray(4) = "'Monthly' Or 'Feb-May-Aug-Nov' Or 'May-Annual'"
MnthArray(5) = "'Monthly' Or 'March-June-Sept-Dec' Or 'June Annual'"
MnthArray(6) = "'Monthly' Or 'Jan-April-July-Oct' Or 'July-Annual'"
MnthArray(7) = "'Monthly' Or 'Feb-May-Aug-Nov' Or 'Aug-Annual'"
MnthArray(8) = "'Monthly' Or 'March-June-Sept-Dec' Or 'Sept-Annual'"
MnthArray(9) = "'Monthly' Or 'Jan-April-July-Oct' Or 'Oct-Annual'"
MnthArray(10) = "'Monthly' Or 'Feb-May-Aug-Nov' Or 'Nov-Annual'"
MnthArray(11) = "'Monthly' Or 'March-June-Sept-Dec' Or 'Dec-Annual'"

I tried running the code but I am getting a "compile error: Invalid outside procedure"


I also tried seting the array to my criteria2 as demonstrated below

PHP:
Set criteria2 = MnthArray(Month(Date) - 1)

Here is the code now with the crazy If statement gone.

PHP:
Private Sub Command140_Click()
Dim db As DAO.Database
Set db = CurrentDb
Dim sqldaily As String
Dim PPY_Excel As Object
Set PPY_Excel = CreateObject("Excel.Application")
'----------------------
Dim criteria1 As String
Dim criteria2 As String
Set criteria2 = MnthArray(Month(Date) - 1)
If Weekday(Date) = 6 Then
criteria1 = Day(Date + 7) & " OR " & Day(Date + 8) & " OR " & Day(Date + 9)
Else
criteria1 = Day(Date + 7)
End If
'----------------------
sql1 = "INSERT INTO Main_PPY_TBL_TMP ( Account, Amount, PPM_Code, Pay_Date, Pay_Month, Starts_On ) "
sql1 = sql1 & "SELECT Main_PPY_TBL.Account, Main_PPY_TBL.Amount, Main_PPY_TBL.PPM_Code, Main_PPY_TBL.Pay_Date, Main_PPY_TBL.Pay_Month, Main_PPY_TBL.Starts_On "
sql1 = sql1 & "FROM Main_PPY_TBL "
sql1 = sql1 & "WHERE (Main_PPY_TBL.Pay_Date = " & criteria1 & ") AND (Main_PPY_TBL.Pay_Month = " & criteria2 & ") "
sql1 = sql1 & "ORDER BY Main_PPY_TBL.Account, Main_PPY_TBL.Pay_Date;"
MsgBox sql1
db.Execute (sql1)
'Send the information to excel on the associates C:drive
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "Main_PPY_TBL_TMP", "C:\Process_PPY.xls", True
'Open Workbook
PPY_Excel.workbooks.Open "C:\Process_PPY.xls"
PPY_Excel.Visible = True
'Delete all records from temp tbl
SQL = "DELETE * FROM Main_PPY_TBL_TMP"
db.Execute (SQL)
End Sub

Any ideas how to get around that "invalid outside procedure" error?
 
Set criteria2 = MnthArray(Month(Date) - 1)

1.
You don't need the Set

2.
Move the following code to the OnLoad event of the form

Code:
MnthArray(0) = "'Monthly' Or 'Jan-April-July-Oct' OR 'Jan-Annual'"
MnthArray(1) = "'Monthly' Or 'Feb-May-Aug-Nov' Or 'Feb-Annual'"
MnthArray(2) = "'Monthly' Or 'March-June-Sept-Dec' Or 'March-Annual'"
MnthArray(3) = "'Monthly' Or 'Jan-April-July-Oct' Or 'April-Annual'"
MnthArray(4) = "'Monthly' Or 'Feb-May-Aug-Nov' Or 'May-Annual'"
MnthArray(5) = "'Monthly' Or 'March-June-Sept-Dec' Or 'June Annual'"
MnthArray(6) = "'Monthly' Or 'Jan-April-July-Oct' Or 'July-Annual'"
MnthArray(7) = "'Monthly' Or 'Feb-May-Aug-Nov' Or 'Aug-Annual'"
MnthArray(8) = "'Monthly' Or 'March-June-Sept-Dec' Or 'Sept-Annual'"
MnthArray(9) = "'Monthly' Or 'Jan-April-July-Oct' Or 'Oct-Annual'"
MnthArray(10) = "'Monthly' Or 'Feb-May-Aug-Nov' Or 'Nov-Annual'"
MnthArray(11) = "'Monthly' Or 'March-June-Sept-Dec' Or 'Dec-Annual'"

Retry
 
Can't seem to get this one to work.

I deleted the array from the general declaration. Then added the array to the load event of my form that I am working off of.

Here is the code in my onload event:

PHP:
Private Sub Form_Load() 
Dim MnthArray(11)
MnthArray(0) = "'Monthly' Or 'Jan-April-July-Oct' OR 'Jan-Annual'"
MnthArray(1) = "'Monthly' Or 'Feb-May-Aug-Nov' Or 'Feb-Annual'"
MnthArray(2) = "'Monthly' Or 'March-June-Sept-Dec' Or 'March-Annual'"
MnthArray(3) = "'Monthly' Or 'Jan-April-July-Oct' Or 'April-Annual'"
MnthArray(4) = "'Monthly' Or 'Feb-May-Aug-Nov' Or 'May-Annual'"
MnthArray(5) = "'Monthly' Or 'March-June-Sept-Dec' Or 'June Annual'"
MnthArray(6) = "'Monthly' Or 'Jan-April-July-Oct' Or 'July-Annual'"
MnthArray(7) = "'Monthly' Or 'Feb-May-Aug-Nov' Or 'Aug-Annual'"
MnthArray(8) = "'Monthly' Or 'March-June-Sept-Dec' Or 'Sept-Annual'"
MnthArray(9) = "'Monthly' Or 'Jan-April-July-Oct' Or 'Oct-Annual'"
MnthArray(10) = "'Monthly' Or 'Feb-May-Aug-Nov' Or 'Nov-Annual'"
MnthArray(11) = "'Monthly' Or 'March-June-Sept-Dec' Or 'Dec-Annual'"
End Sub


Then I took the set off of the criteria2 line so that it just reads:

criteria2 = MnthArray(Month(Date) - 1)

the new error is highlighting the MnthArray section of the line above and stating "Sub or Function not defined"

I tried changing my form load procedure to public but that did not help either.

Thanks again for the help !!!
 
Did I tell you to move Dim MnthArray(11) to the on load event? I don't think so. Moe this back to the main declarations section and retry.
 
Hey Dcrake,

I got one step farther but the criteria2 in the sql code is coming up as blank. for some reason the mntharray(9) (for october) is inserting into the sql code.
Don't give up on me!!:cool:
 

Users who are viewing this thread

Back
Top Bottom