Runtime Error in VBA 3075 - ms-access

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

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?

Generating a printable report from a database using a date range

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.

Using multiple Combo-boxes in Access as query criteria dont work together but using one combo-box works ?? How to make all Combo-boxes work?

I looks like when i use one Combo-box as a criteria works just fine however using more than that though following the same steps doesn't work . I don't use SQL i do use the Design view though.
How to make all combo boxes works together to provide the needed criteria.
If you are looking to filter a form or list box using data selected from several combo boxes, then you will need to build up the RowSource "on the fly" based on the selections made.
Below is some sample code that uses selections from 2 combo boxes (cboCountry and cboRMZone) to create the RowSource for a list box (lstCountry):
Private Sub cboCountryZone_AfterUpdate()
Call sSearchMultiple
End Sub
Private Sub cboRMZone_AfterUpdate()
Call sSearchMultiple
End Sub
Private Sub Form_Load()
Call sSearchMultiple
End Sub
Private Sub sSearchMultiple()
On Error GoTo E_Handle
Dim strSQL As String
If Not IsNull(Me!cboCountryZone) Then strSQL = strSQL & " AND CountryZone_PK=" & Me!cboCountryZone
If Not IsNull(Me!cboRMZone) Then strSQL = strSQL & " AND RMZone_PK=" & Me!cboRMZone
If Left(strSQL, 4) = " AND" Then
strSQL = " WHERE " & Mid(strSQL, 6)
End If
If Len(strSQL) > 0 Then
Me!lstCountry.RowSource = "SELECT CountryName FROM dbo_svr_Country " & strSQL & " ORDER BY CountryName ASC;"
Else
Me!lstCountry.RowSource = "SELECT CountryName FROM dbo_svr_Country ORDER BY CountryName ASC;"
End If
sExit:
On Error Resume Next
Exit Sub
E_Handle:
MsgBox Err.Description & vbCrLf & vbCrLf & "Form3!sSearchMultiple", vbOKOnly + vbCritical, "Error: " & Err.Number
Resume sExit
End Sub
Regards,

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")

How to open Access report in a subform/subreport of the main form that houses the controls

I have this MS Access VBA code, using MS Access 2016.
Private Sub cmdPreview_Click()
On Error GoTo Err_Handler
Dim strReport As String
Dim strDateField As String
Dim strWhere As String
Dim lngView As Long
Const strcJetDate = "\#mm\/dd\/yyyy\#"
strReport = "Sales Report V2"
strDateField = "[OrderDate]"
lngView = acViewReport
If IsDate(Me.txtStartDate) Then
strWhere = "(" & strDateField & " >= " & Format(Me.txtStartDate, strcJetDate) & ")"
End If
If IsDate(Me.txtEndDate) Then
If strWhere <> vbNullString Then
strWhere = strWhere & " AND "
End If
strWhere = strWhere & "(" & strDateField & " < " & Format(Me.txtEndDate + 1, strcJetDate) & ")"
End If
If CurrentProject.AllReports(strReport).IsLoaded Then
DoCmd.Close acReport, strReport
End If
DoCmd.OpenReport strReport, lngView, , strWhere
Exit_Handler:
Exit Sub
Err_Handler:
If Err.Number <> 2501 Then
MsgBox "Error " & Err.Number & ": " & Err.Description, vbExclamation, "Cannot open Report"
End If
Resume Exit_Handler
End Sub
The is the the code is use to make date range form work. The form lets you pick a start date and an end date. You then click a button that calls the above code and the report is generated in an new tab. What I want to achieve but have not been able to figure out so far. Is how to make the report show up in a subform/subreport of the form that contains the date range controls and then from there have a button that is clicked to open the generated report in a new tab or printing or whatever if the user is happy with the selection.
This code is from a tutorial located at www.allenbrowne.com/casu-08.html
You can set the Filter property of the enclosed report:
Me!NameOfYourSubreportControl.Report.Filter = strWhere
Me!NameOfYourSubreportControl.Report.FilterOn = True
To open the report the normal way, use your existing code.