Hi, I hope someone can straighten me out (and my code)! I've looked at several books and each book shows me a different method to write SQL in VBA. I am very confused. There must be a simple, understandable method to develop a "where" clause for filtering a form using a dialogue box. I think I have the basics down for the following code. But I get an error (and probably more). Thanks for any help that you can give on both the code and providing insight or a referal to a good reference source for writing sql in vba. Thanks!
Private Sub Ok_Click()
'The purpose of this sub is to open FrmTrackRptDatesODR Form with QryRptsSelectODR Query as datasource'
'using filter input from this dialogue box. The input includes PE_ID (integer), BU_ID (integer),'
'RptType ( 3 Options), and OvrDue (3 Options).'
Me.Visible = False
Dim strWhere As String
strWhere = "1=1 "
'Set Where Clause variable to True'
'Find all records with the BU_ID and PE_ID input from Dialogue Box'
strWhere = strWhere & " AND [PEID]=" & Me![PE_ID] & _
" AND [BUID]=" & Me![BU_ID]
'For Case RptType in dialogue box, find all RA reports, CSP reports and RAA reports'
Select Case Me.RptType
Case 1
'For Case Ovrdue Rpts in dialogue Box, find all dates for RADue <31 days for today, < 30 days and'
'all null due dates'
Select Case Me.OvrDue
Case 1
'StrWhere = strWhere + "SQL Statement #Date# & _ (line continuation)'
strWhere = strWhere & " AND [RADue]<#(Date()+31)#" & _
" AND [RAComp]Is Null "";"
Case 2
strWhere = strWhere & " AND [RADue] >#(Date()+30)#" & _
" AND [RAComp]Is Null "";"
Case 3
strWhere = strWhere & " AND [RADue] Is Null" & _
" AND [RAComp]Is Null "";"
End Select
Case 2
'For Case Ovrdue Rpts find all dates for CSPDue <31 days for today, < 30 days and'
'all null due dates'
Select Case Me.OvrDue
Case 1
'StrWhere = strWhere + "SQL Statement #Date# & _ (line continuation)'
strWhere = strWhere & " AND [CSPDue] <#(Date()+31)#" & _
" AND [CSPComp]Is Null "";"
Case 2
strWhere = strWhere & " AND [CSPDue] >#(Date()+30)#" & _
" AND [CSPComp]Is Null "";"
Case 3
strWhere = strWhere & " AND [CSPDue] Is Null" & _
" AND [CSPComp]Is Null "";"
End Select
Case 3
'For Case Ovrdue Rpts find all dates for RAADue <31 days for today, < 30 days and'
'all null due dates'
Select Case Me.OvrDue
Case 1
'StrWhere = strWhere + "SQL Statement #Date# & _ (line continuation)'
strWhere = strWhere & " AND [RAADue] <#(Date()+31)#" & _
" AND [RAAComp]Is Null "";"
Case 2
strWhere = strWhere & " AND [RAADue] >#(Date()+30)#" & _
" AND [RAAComp]Is Null "";"
Case 3
strWhere = strWhere & " AND [RAADue] Is Null" & _
" AND [RAAComp]Is Null "";"
End Select
End Select
Private Sub Ok_Click()
'The purpose of this sub is to open FrmTrackRptDatesODR Form with QryRptsSelectODR Query as datasource'
'using filter input from this dialogue box. The input includes PE_ID (integer), BU_ID (integer),'
'RptType ( 3 Options), and OvrDue (3 Options).'
Me.Visible = False
Dim strWhere As String
strWhere = "1=1 "
'Set Where Clause variable to True'
'Find all records with the BU_ID and PE_ID input from Dialogue Box'
strWhere = strWhere & " AND [PEID]=" & Me![PE_ID] & _
" AND [BUID]=" & Me![BU_ID]
'For Case RptType in dialogue box, find all RA reports, CSP reports and RAA reports'
Select Case Me.RptType
Case 1
'For Case Ovrdue Rpts in dialogue Box, find all dates for RADue <31 days for today, < 30 days and'
'all null due dates'
Select Case Me.OvrDue
Case 1
'StrWhere = strWhere + "SQL Statement #Date# & _ (line continuation)'
strWhere = strWhere & " AND [RADue]<#(Date()+31)#" & _
" AND [RAComp]Is Null "";"
Case 2
strWhere = strWhere & " AND [RADue] >#(Date()+30)#" & _
" AND [RAComp]Is Null "";"
Case 3
strWhere = strWhere & " AND [RADue] Is Null" & _
" AND [RAComp]Is Null "";"
End Select
Case 2
'For Case Ovrdue Rpts find all dates for CSPDue <31 days for today, < 30 days and'
'all null due dates'
Select Case Me.OvrDue
Case 1
'StrWhere = strWhere + "SQL Statement #Date# & _ (line continuation)'
strWhere = strWhere & " AND [CSPDue] <#(Date()+31)#" & _
" AND [CSPComp]Is Null "";"
Case 2
strWhere = strWhere & " AND [CSPDue] >#(Date()+30)#" & _
" AND [CSPComp]Is Null "";"
Case 3
strWhere = strWhere & " AND [CSPDue] Is Null" & _
" AND [CSPComp]Is Null "";"
End Select
Case 3
'For Case Ovrdue Rpts find all dates for RAADue <31 days for today, < 30 days and'
'all null due dates'
Select Case Me.OvrDue
Case 1
'StrWhere = strWhere + "SQL Statement #Date# & _ (line continuation)'
strWhere = strWhere & " AND [RAADue] <#(Date()+31)#" & _
" AND [RAAComp]Is Null "";"
Case 2
strWhere = strWhere & " AND [RAADue] >#(Date()+30)#" & _
" AND [RAAComp]Is Null "";"
Case 3
strWhere = strWhere & " AND [RAADue] Is Null" & _
" AND [RAAComp]Is Null "";"
End Select
End Select