isampurna:
Query by form is a pretty straight forward process.
1. Create an unbound form, name it something like frmMonthSet.
2. Add two text boxes to the form, name then txtStart and txtEnd (this is where the user will enter the start and end dates for the report period).
3. Add a command button to open the report or query.
4. Open the query where you want to use the two date entries.
5. In the criteria cell of the date field type this:
>=[Forms]![frmMonthSet]![txtStart] And <=[Forms]![frmMonthSet]![txtEnd]
or you can use the Expression Builder by right clicking on the criteria cell and selecting Build to create the above string. Check Help for Expression Builder for specifics.
I usually take this one step further since I don't trust users to enter dates correctly and display two list boxes for the user to select the month and year for the report range. The following (from one of my class handouts) explains how to set that up on the frmMonthSet.
Access Code Sample
Code to use a list boxes to set a month date range for report generation
Problem: Allowing users to enter the beginning and ending dates opens the possibility of invalid date values being entered which will produce a vague error message. Trapping for such date errors is difficult.
Solution: Allow users to only select for a month/year combination and set the beginning and ending dates via code.
This code sample uses the following controls on an unbound form to set a monthly date range for the production of reports:
lstMonth List Box Control to select report month
lstYear List Box Control to select report year
txtStartDate Beginning date of the selected report month/year
txtEndDate Ending date of the selected report month/year
txtReportMonth Formatted header for inclusion in the report output
lstMonth.RowSource = tblListMonth
SELECT DISTINCTROW [tblListMonth].[MonthNum], [tblListMonth].[MonthName] FROM [tblListMonth];
lstYear.RowSourceType = ListYears() [Function – See Below]
Form Code:
'frmSetDates
'Example of setting report date values via code
'On Open -- Sets the date range of the report to the previous month's first and last date.
'After Update(lstMonth and lstYear) -- Sets the date range to the selected Month/Year combination; provides
‘warnings if the user selects the current or later month.
'Date values are passed to the underlying query via reference or can be used internally in the creation
‘of queries via sql code.
'Properties (Optional):
'Control Width Height ColumnWidths RowSourceType RowSource
'lstYear 0.40 0.60 0”,.4” ListYears()
'lstMonth 0.75 1.80 0”,.75” tblListMonth
'txtStartDate 0.75 0.1667
'txtEndDate 0.75 0.1667
'txtReportMonth 2.60 0.1667
'
Option Compare Database
Option Explicit
Private Sub Form_Open(Cancel As Integer)
Call SetMonth ‘Sets the initial values for lstYear and lstMonth
Call SetDates ‘Sets the initial values for the text controls
End Sub
Private Sub lstMonth_AfterUpdate()
Call SetDates
End Sub
Private Sub lstYear_AfterUpdate()
Call SetDates
End Sub
Private Sub SetMonth()
If DatePart("M", Date) = 1 Then
'If the current month is January then we want to set the report
'month to December and the report year to the previous year.
lstMonth = 12
lstYear = DatePart("YYYY", Date) - 1
Else
'If the current month is after January then just set the
'report month to the previous month.
lstMonth = DatePart("M", Date) - 1
lstYear = DatePart("YYYY", Date)
End If
End Sub
Private Sub SetDates()
Dim datStart As Date
Dim datEnd As Date
Dim intMonth As Integer
Dim intYear As Integer
intMonth = CInt(lstMonth) 'Makes the value of list month an Integer for comparison purposes
intYear = CInt(lstYear) 'Makes the value of list year an Integer for comparison purposes
If intMonth = DatePart("M", Date) And intYear = DatePart("YYYY", Date) Then
'The selected month is the current month and the user is shown a prompt to cancel/continue.
'If the user selects cancel, the list boxes are reset and the sub is exited.
If MsgBox("This is the current month!" & vbCrLf & vbCrLf & _
"Do you wish to continue?", vbOKCancel + vbQuestion, "TCC Reports"

= vbCancel Then
Call SetMonth
Exit Sub
End If
Else
'The selected month/year is later than the current date; no records will be available.
'The user's only option is to exit the sub and select another month/year.
If intMonth > DatePart("M", Date) And intYear >= DatePart("YYYY", Date) Then
MsgBox "The month selected is after the current month!" & vbCrLf & vbCrLf & _
"Please Cancel by clicking on OK.", vbOKOnly + vbCritical, "TCC Reports"
Call SetMonth
Exit Sub
End If
End If
'If the user selects OK on the first condition test or neither condition is true, then the date range
'and report header text controls are set to the selected month/year combination.
datStart = lstMonth & "/01/" & lstYear 'Example: 1 + /01/ + 2003 = 01/01/2003
datEnd = DateAdd("M", 1, datStart) 'Example: 01/01/2003 + 1 Month = 02/01/2003
datEnd = DateAdd("D", -1, datEnd) 'Example: 02/01/2003 - 1 day = 01/31/2003
txtStartDate = datStart
txtEndDate = datEnd.
txtReportMonth = Format(datStart, "MMMM D" & ", " & "YYYY"

& " To " & Format(datEnd, "MMMM D" & ", " & "YYYY"

'Example: January 1, 2003 To January 31, 2003
End Sub
This function should be placed in a separate module or added to an existing module. It can be called by lstYears.RowSourceType from any form which uses this technique to establish report date ranges.
Public Function ListYears(fld As Control, id As Variant, row As Variant, col As Variant, code As Variant) As Variant
Dim intOffset As Integer
Dim intYear As Integer
intYear = DatePart("YYYY", Date) – 3 ‘Sets the beginning year to 3 years in the past; list box will display the current
‘year and the last three years
Select Case code
Case acLBInitialize
ListYears = True
Case acLBOpen
ListYears = Timer
Case acLBGetRowCount
ListYears = 4
Case acLBGetColumnWidth
ListYears = 0.75
Case acLBGetValue
intOffset = 1
ListYears = intYear + intOffset * row
End Select
End Function
See List Boxes/Source Data in Access Help for an explanation of this code.
HTH
Larry De Laruelle
ldelaruelle@familychildrenscenter.org