Here's the code:
The code is in several places. It also needs some cleaning up.
First the month calendar is set up as per instructions. Then the on
click code the query command button is:
Private Sub cmdSearch_Click()
'Define the variables used to build the SQL
Dim stroutfallnumber As String 'to hold the value for the outfall
number
Dim strcompliancesample As String 'to hold the value for compliance
sample
Dim dcollectiondate As Date 'to hold the value for collection date
Dim denddate As Date
Dim strSQLSelectStatement As String
Dim strSQLClause1 As String 'for combo box 1 outfall number
Dim strSQLClause5 As String 'for combo box 5 compliance sample
Dim strSQLClause8 As String 'for text box EndDate
Dim strSQLClause6 As String 'for the text box collection date
Dim strOrderByClause As String 'the sort order for the query
Dim strFinalSQLStatement As String 'the final SQL string to return
results with
fFirstSQLClause = True 'tells the VB function to use WHERE or AND
before each SQL search criteria
'Combo box 1 criteria for outfall number
stroutfallnumber = Nz(Me.cmbOutfallNameNumber.Column(1,
Me.cmbOutfallNameNumber.ListIndex), "")
If stroutfallnumber <> "" Then 'then we have a valid customer
criteria
'create the search criteria for the customer id entry
strSQLClause1 = "[qry Analyzed Results].[Outfall Number] = """
+ CStr(stroutfallnumber) + """"
'append WHERE or AND as necessary to the SQL clause
strSQLClause1 = strFixedSQLClause(strSQLClause1)
Else
'no restriction here-just make it equal to ""
strSQLClause1 = ""
End If
'strSQLClause5 = "[qry Analyzed Results].[Compliance Sample])=
Yes"
strSQLClause5 = " WHERE [qry Analyzed Results].[compliance
sample] = yes"
'Text box 6 criteria for collection date
' test sql Results.[Collection Date]) Between dcollectiondate And
[forms]![Export Form]![end]
dcollectiondate = Nz(Me.CollectionDate, #1/1/1901#)
Rem If dcollectiondate <> #1/1/1901# Then 'then we have a valid
product criteria
'create the search criteria for the product id entry
strSQLClause6 = " AND [qry Analyzed Results].[Collection Date]
=" + "#" + CStr(dcollectiondate) + "#"
'strSQLClause6 = "test"
'append WHERE or AND as necessary to the SQL clause
'strSQLClause6 = strFixedSQLClause(strSQLClause6)
Rem Else
'no restriction here-just make it equal to ""
Rem strSQLClause6 = ""
Rem End If
'Text box 8 criteria for collection date
' test sql Results.[Collection Date]) Between dcollectiondate And
[forms]![Export Form]![end]
denddate = Nz(Me.txtEndDate, #1/1/1901#)
Rem If dcollectiondate <> #1/1/1901# Then 'then we have a valid
product criteria
'create the search criteria for the product id entry
strSQLClause8 = " AND [qry Analyzed Results].[Collection Date]
<=" + "#" + CStr(denddate) + "#"
'strSQLClause6 = "test"
'append WHERE or AND as necessary to the SQL clause
'strSQLClause6 = strFixedSQLClause(strSQLClause6)
Rem Else
'no restriction here-just make it equal to ""
Rem strSQLClause6 = ""
Rem End If
'set up the sort order clause
strOrderByClause = "ORDER BY [qry Analyzed Results].[Collection
Date]"
'strOrderByClause = ""
If fFirstSQLClause Then
strOrderByClause = ""
End If
'add it all together
strFinalSQLStatement = strDefaultSQLString + " " + strSQLClause1 +
" " + strSQLClause5 + " " + strSQLClause6 + " " + strSQLClause8 + " " +
strOrderByClause
'strFinalSQLStatement = strDefaultSQLString + " " + strSQLClause5 +
" " + strSQLClause6
strqrySQL = strFinalSQLStatement
'set the new recordsource
Rem old Me.Child0.Form.RecordSource = strFinalSQLStatement
'new test
Me.Child0.Form.RecordSource = strFinalSQLStatement
End Sub
Private Function strFixedSQLClause(strSQLClause As String) As String
If fFirstSQLClause = True Then
fFirstSQLClause = False
strFixedSQLClause = "WHERE " + strSQLClause
Else
strFixedSQLClause = "AND " + strSQLClause
End If
End Function
Then there is the checkbox to indicate that the dataset is the dataset
that you desire to send. If checked it makes another command button to
send email visible. The after update code for this is:
Private Sub CheckBox_AfterUpdate()
If CheckBox.Value = -1 Then
Command40.Visible = True
Label44.Visible = True
Else
Command40.Visible = False
Label44.Visible = False
End
End If
End Sub
Finally, the command button for the send email button is (note, right
now it opens the dataset as an Excel file, this will be changed -
recycling some code):
Private Sub Command40_Click()
Dim Msg, Style, Title, Help, Ctxt, Response, MyString
Msg = "By Selecting yes I certify that..." ' Define message.
Style = vbYesNo ' Define buttons.
Title = "Certification" ' Define title.
' Display message.
Response = MsgBox(Msg, Style, Title)
If Response = vbNo Then ' User chose No.
End ' Perform some action.
Else ' User chose Yes.
'MyString = "Yes" ' Perform some action.
End If
Call cmdSearch_Click
If strqrySQL = "" Then
strqrySQL = "SELECT [qry Analyzed Results].[Outfall Number], [qry
Analyzed Results].[Outfall Name], [qry Analyzed Results].[Collection
Date], [qry Analyzed Results].Analyte, [qry Analyzed Results].Result,
[qry Analyzed Results].[Result Number], [qry analyzed results].[units],
[qry Analyzed Results].[Compliance Sample], [qry Analyzed
Results].[Sample Type], [qry Analyzed Results].Sampler FROM [qry
Analyzed Results]"
ExportToExcel (strqrySQL)
Else
DoCmd.Requery "Child0"
ExportToExcel (strqrySQL)
End If
End Sub
If the query is run, and the checkbox is checked, you can use the
calendar control again to reset the dates. On the other hand if you
uncheck the checkbox and then try to use the calendar you get an error.
Also, the is a yes/no msgbox in the "send email" command button.
Yes means the dataset is correct, while No indicates, that no this is
not the dataset that I want to send. It stops the process of send the
mail and takes you back to the form. If no is selected you can't use
the calendar control to set other dates.
Thanks