Generating a printable report from a database using a date range - ms-access

I have a database which i have been able to design a form which enables me to search data in a date range and the results are display in a datasheet below it.
I wish to be able to make the filtered results printable in a report.
Below are the vba codes i used for the form:
Private Sub Command20_Click()
' Search button
Call Search
End Sub
Sub Search()
Dim strCriteria, task As String
Me.Refresh
If IsNull(Me.OrderDateFrom) Or IsNull(Me.OrderDateTo) Then
MsgBox "Please enter the date range", vbInformation, "Date Range Required"
Me.OrderDateFrom.SetFocus
Else
strCriteria = "([DATE] >= #" & Me.OrderDateFrom & "# And [DATE] <= #" & Me.OrderDateTo & "#)"
task = "select * from ALL_INCOME where (" & strCriteria & ") order by [DATE]"
DoCmd.ApplyFilter task
End If
Any help on how to get it into a printable report will be greatly appreciated.

Related

vba code to show the time range filter applied on form in report

In my Ms Access form (named frmGesamt) I have a filter where I filter the date ranges from .. to ... (named txtVomAll and txtBisAll) If I click on the 'search' button, the dates with this time filter are displayed. My code is like this:
Private Sub cmd_SAll_Click()
Dim strCriteria, task As String
Me.Refresh
If IsNull(Me.txtVomAll) Or
IsNull(Me.txtBisAll) Then
MsgBox "Please enter date range",
vbInformation, "date range"
Me.txtVomAll.SetFocus
Else
strCriteria = "([BestellDatum] >= #" & Format(Me.txtVomAll, "yyyy\/mm\/dd") & "# And [BestellDatum] <= #" & Format(Me.txtBisAll, "yyyy\/mm\/dd") & "#)"
task = "select * from
qryFürFormular where(" &
strCriteria & ") order by
[BestellDatum]"
DoCmd.ApplyFilter task
End If
End Sub
Now when I press the 'report' button, I want to generate a report with this filter.
I wrote the following code for the on open part of the report:
Private Sub Report_Open(Cancel As Integer)
Dim frm As Form
Dim strFilter, task As String
Set frm = Forms!frmGesamt
strFilter = ""
If IsNull("" & frm!txtVomAll) Or IsNull("" & frm!txtBisAll) Then
Me.Filter = ""
Me.FilterOn = False
Exit Sub
Else
strFilter = "select * from frmGesamt where [BestellDatum] Like "" & task & " * """"
Me.Filter = strFilter
Me.FilterOn = True
End If
End Sub
but it doesn't work. How can I solve this?

I need to generate a report from a data in a searched date range

I have a database which I have been able to design a split form where am able to search by date and the results are displayed in the datasheet below.
My issue is, is there any code to help me store the data displayed in the data sheet into a table so I can easily link it to a designed report for printing?
Or better still, is it possible for my designed report to easily pick the records searched in the date range without passing it through a table?
There are my codes for the split form
Private Sub Command20_Click()
' Search button
Call Search
End Sub
Sub Search()
Dim strCriteria, task As String
Me.Refresh
If IsNull(Me.OrderDateFrom) Or IsNull(Me.OrderDateTo) Then
MsgBox "Please enter the date range", vbInformation, "Date Range Required"
Me.OrderDateFrom.SetFocus
Else
strCriteria = "([DATE] >= #" & Me.OrderDateFrom & "# And [DATE] <= #" & Me.OrderDateTo & "#)"
task = "select * from ALL_INCOME where (" & strCriteria & ") order by [DATE]"
DoCmd.ApplyFilter task
End If
End Sub
I would like to create a button to print this report.
Any help with this will be greatly appreciated.
Thanks in advance.
You can directly filter report data by following command. Adjust report name, table field name to your case.
DoCmd.OpenReport "rptAllInvoice", acViewPreview, , "[MyDate] BETWEEN #" & Me.OrderDateFrom & "# AND #" & Me.OrderDateTo & "#", acWindowNormal
Here rptAllInvoice is report name and MyDate is date field to table.

VBA filter working for calculations not for report launch

I'm a beginner so please stick with me. I'm trying to make a user friendly form to calculate total cases for each worker during a date range, and to launch a report based off the same criteria. The calculation is working great but when I click the report it prompts me to enter the parameters for SWNameFilter. Thank you!
Option Compare Database
Option Explicit
Private Sub CalculateResultsSW_Click()
Dim SWReport As DAO.Recordset
Dim vChosenFilters As String
If (Not IsNull(TestSWFromDateFilter)) And (Not IsNull(TestSWToDateFilter)) Then
vChosenFilters = BuildFilterString
CasesCount = DCount("[SPN]", "TestSW", vChosenFilters)
End If
End Sub
Private Sub CloseForm_Click()
DoCmd.Close
End Sub
Private Sub Form_Load()
CalculateResultsSW_Click
End Sub
Private Function BuildFilterString()
BuildFilterString = "(ArrestDate Between #" & TestSWFromDateFilter & "# And #" & TestSWToDateFilter & "#)"
If Not IsNull(SWNameFilter) Then
BuildFilterString = BuildFilterString & " And (SWName = " & "SWNameFilter" & ")"
End If
End Function
Private Sub OpenSWReport_Click()
On Error Resume Next
DoCmd.OpenReport "SWReport", acViewPreview, , BuildFilterString
DoCmd.OutputTo acOutputReport, "SWReport", acFormatPDF, "C:\Users\" & Environ("Username") & "\Desktop\PJDSW Report.pdf", True
DoCmd.Close acReport, "SWReport"
On Error GoTo 0
End Sub
The reason that you see the popup for SWNameFilter is that the query believes that it's a name parameter, and it's asking you to enter a value for that.
The reason for it is simple, this line:
BuildFilterString = BuildFilterString & " And (SWName = " & "SWNameFilter" & ")"
The problem is that you've surrounded your variable name with double-quotes, which basically treats it like text, so your query is becoming:
And (SWName = "SWNameFilter")
This is clearly incorrect, as what you actually wanted was to include the value of the variable:
BuildFilterString = BuildFilterString & " And (SWName = " & SWNameFilter & ")"
Note the removal of the double-quotes, which will then embed the value of the variable in to the SQL instead of the word "SWNameFilter".
If SWNameFilter is a string (which I assume it is) then you'll want some double-quotes to be embedded into the SQL, which you can achieve like this in Access:
BuildFilterString = BuildFilterString & " And (SWName = """ & SWNameFilter & """)"
This will output:
And (SWName = "Value of SWNameFilter")

Runtime Error in VBA 3075

I am trying to create a search form in Access where in people can search records by date range. But each time I click search I get a run time error 3075... with DoCmd.ApplyFilter task highlighted. I am new to VBA.
Private Sub Command62_Click()
' Search Button
Call Search
End Sub
Sub Search()
Dim strCriteria, task As String
Me.Refresh
If IsNull(Me.txtDateIntakeAssignedFrom) Or IsNull(Me.txtDateIntakeAssignedTo) Then
MsgBox "Please enter the date range", vbInformation, "Date Range Required"
Me.txtDateIntakeAssignedFrom.SetFocus
Else
strCriteria = "([Date_Staff_Assigned]) >= #" & Me.txtDateIntakeAssignedFrom & "# And [Date_Staff_Assigned] <= #" & Me.txtDateIntakeAssignedTo & "#)"
task = "select * from frmCustomizeSearchDataonDatasheet where (" & strCriteria & ") order by [Date_Staff_Assigned]"
DoCmd.ApplyFilter task
End If
End Sub

MS Access: Custom report parameterization with tempvars

This report image shows a report which should open with tempVars set in place from a couple controls on a form which gets those values and therefore, assigns those to the named vars in the image. Currently, the WHERE clause is not working, some parameters do not match or are not getting the variables they should to read and therefore, open a report with the specified information in the WHERE condition clause. How might the where condition be modified to get the parameters to work?
(as you can see I erased some information to make is simpler.)
'------------------------------------------------------------
' Expirig_Click
'
'------------------------------------------------------------
Private Sub Expirig_Click()
On Error GoTo Expirig_Click_Err
DoCmd.OpenReport "Expiring", acViewPreview, "", "[DuesExpire]=[TempVars]![tempExpirationDate] And [Type]=[TempVars]![tempType]", acNormal
TempVars.Add "tempType", Combo164
TempVars.Add "tempExpirationDate", CutoffDate
DoCmd.OpenReport "Expiring", acViewReport, "", "[DuesExpire]=[TempVars]![tempExpirationDate] And [Type]=[TempVars]![tempType]", acNormal
Expirig_Click_Exit:
Exit Sub
ExpirigLabels_Click_Err:
MsgBox Error$
Resume Expirig_Click_Exit
End Sub
Use VBA variables instead of TempVars, and take them out of the constant WHERE string. Like this:
Dim myDate As Date
Dim myType As String
myDate = CutoffDate
myType = Combo164
DoCmd.OpenReport "Expiring", acViewPreview, , _
"[DuesExpire] = #" & Format(myDate, "mm\/dd\/yyyy") & "# AND [Type] = '" & myType & "'"
If Type is not string but number, it's simply [Type] = " & myType without the '
Filtering for dates in Access SQL needs a special format: = #mm/dd/yyyy#
Format(myDate, "mm\/dd\/yyyy") is only needed, if your regional settings have a date format different than mm/dd/yyyy (i.e. if you're not in the US)