I need a simplified way of writing and IF THEN loop
I have 10 checkboxes that are search criteria for my database. So 100 different possible combinations of search criteria. For each checkbox that is true, it will paste a piece of a SQL statement based on user chosen range, that is sent to Forms!Mailer.RecordSource after opening the window. (the variable chkDATE and DATERANGE are examples of this). The variables attached to each check box are also missing, but I assume you don't need them to get what I am trying to accomplish.
I hope that isn't too vauge. I just finished writing a long ass post about what I was doing, including my code sample and this website made me log in again, which deleted everything I had written.
Let me know if you need more information. Here is a piece of sample code.... Most of the SQL stuff isn't written yet, I wanted to tackle the IF the problem first. The statement has to start with WHEN (strSQL) then each variable, separated by AND (strSQL2).
I have 10 checkboxes that are search criteria for my database. So 100 different possible combinations of search criteria. For each checkbox that is true, it will paste a piece of a SQL statement based on user chosen range, that is sent to Forms!Mailer.RecordSource after opening the window. (the variable chkDATE and DATERANGE are examples of this). The variables attached to each check box are also missing, but I assume you don't need them to get what I am trying to accomplish.
I hope that isn't too vauge. I just finished writing a long ass post about what I was doing, including my code sample and this website made me log in again, which deleted everything I had written.
Let me know if you need more information. Here is a piece of sample code.... Most of the SQL stuff isn't written yet, I wanted to tackle the IF the problem first. The statement has to start with WHEN (strSQL) then each variable, separated by AND (strSQL2).
Code:
Private Sub Command1_Click()
Dim Value1 As String
Dim value2 As String
Value1 = Text4 'the starting value of the date range from a date picker
value2 = Text6 'the ending value of the date range from a date picker
strSQL = " Where "
strSql2 = " And "
If chkDate = True Then
DateRange = "(([Table 2].[Contacted Date]) Between #" & Value1 & "# And #" & value2 & "#)"
Else
chkDate = ""
End If
'clears the string
SqlSTRING = ""
If chkDate = True And ChkGood = True And ChkOk = True And ChkBad = True And ChkContacted = True Then
SqlSTRING = strSQL & DateRange & strSql2 & CGood & strSql2 & CBad & strSql2 & COk & strSql2 & CContacted
'1 false
ElseIf chkDate = True And ChkGood = True And ChkOk = True And ChkBad = True And ChkContacted = False Then
SqlSTRING = strSQL & DateRange & strSql2 & CGood & strSql2 & COk & strSql2 & CBad
........Lots of if then statements in between.......
'all false
ElseIf chkDate = False And ChkGood = False And ChkOk = False And ChkBad = False And ChkContacted = False Then
SqlSTRING = ""
End If
DoCmd.OpenForm "mailer"
Forms!Mailer.RecordSource = "SELECT Boise.OWNERNM, Boise.OWNRST, Boise.OWNERADR, [Table 2].Contacted, [Table 2].[Contacted Date], Boise.OWNERNM1, Boise.OWNERCTY, Boise.OWNERZIP, Boise.ADDRESS, Boise.CITY, Boise.STATE, Boise.ZIPCODE FROM Boise INNER JOIN [Table 2] ON Boise.RECNO=[Table 2].RECNO" & SqlSTRING & ";"
DoCmd.Close acForm, "criteria", acSaveNo
Form_Mailer.SetFocus
End Sub
Last edited: