Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding From and TO date field to form to generate a Report

Status
Not open for further replies.

DouglasLB

Technical User
Jun 18, 2001
10
0
0
US
I need to add to a form fields that will determine the dates a report will show data.

How is this achieved?

 
Just add 2 textboxes or comboboxes to the form linked to a date field in your data, and add the following code to the click event of the button which runs the report.

Just modify the code to fit your database fields. B-)

Private Sub ReportCmd_Click()
Dim RptName As String
Dim LinkFilter As String
Dim NumEntries As Integer

If (Me!BegDate > Me!EndDate) And (Not (IsNull(Me!EndDate))) Then
MsgBox "Error: Beginning date cannot be later than Ending date!", vbCritical
Me!BegDate.SetFocus
Exit Sub
End If

LinkFilter = ""

If Not (IsNull(Me!BegDate)) Then
If LinkFilter = "" Then
LinkFilter = "[Date] >= #" & Me!BegDate & "#"
Else
LinkFilter = LinkFilter & "And [Date] >= #" & Me!BegDate & "#"
End If
End If

If Not (IsNull(Me!EndDate)) Then
If LinkFilter = "" Then
LinkFilter = &quot;[Date] <= #&quot; & Me!EndDate & &quot;#&quot;
Else
LinkFilter = LinkFilter & &quot;And [Date] <= #&quot; & Me!EndDate & &quot;#&quot;
End If
End If

NumEntries = DCount(&quot;*&quot;, &quot;table1&quot;, LinkFilter)
If NumEntries < 1 Then
MsgBox &quot;There are no records which match the specified criteria.&quot;, vbInformation
Else
RptName = Me!ReportName
DoCmd.OpenReport RptName, acViewPreview, , LinkFilter
DoCmd.Close acForm, &quot;ReportForm&quot;
End If

End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top