473,385 Members | 2,162 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Filter Query through Form

SET-UP: I currently have a form with unbound text fields to type in the criteria. Then, I have a query with the criteria referencing those text fields. On the form, I have a 'Report' command button to print preview the report.

GOAL: I have a form that filters a query to then print a report. There are two sections in the form: one for Start Date and End Date, the other for a material number. I would like the option to do the following scenarios:
  • Enter a date range to report all materials received within that range.
  • Enter a material number to report all material receipts.
  • Enter a date range and material number to report a specific material received within a certain period.
  • Leave all three fields blank and report all receipts of every material.
Oct 15 '14 #1
3 1244
This is what I've done... I now have 4 quearies for my 4 different scenarios. I need to change the record source of the report based on what fields are null in my form. Theoretically, I think this should work, but it doesn't and I'm not skilled enough to know why. Any help would be appreciated.
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdReport_Click()
  2. On Error GoTo Err_cmdReport_Click
  3.  
  4.     Dim stDocName As String
  5.     Dim stWhereCondition As String
  6.  
  7.     stDocName = "rptRMStatus"
  8.  
  9. If IsNull(Me.IDH.Value) Then
  10.     stWhereCondition = Report.rptRMStatus.RecordSource = "qryRMStatus1"
  11. ElseIf IsNull(Me.StartDate.Value) And (Me.EndDate.Value) Then
  12.     stWhereCondition = Report.rptRMStatus.RecordSource = "qryRMStatus2"
  13. ElseIf IsNull(Me.IDH.Value) And (Me.StartDate.Value) And (Me.EndDate.Value) Then
  14.     stWhereCondition = Report.rptRMStatus.RecordSource = "qryRMStatus4"
  15. Else: stWhereCondition = Report.rptRMStatus.RecordSource = "qryRMStatus3"
  16. End If
  17.  
  18. DoCmd.OpenReport stDocName, acViewPreview, , stWhereCondition, acWindowNormal
  19.  
  20. Exit_cmdReport_Click:
  21.     Exit Sub
  22.  
  23. Err_cmdReport_Click:
  24.     MsgBox Err.Description
  25.     Resume Exit_cmdReport_Click
  26.  
  27. End Sub
  28.  
Oct 16 '14 #2
dsatino
393 256MB
This makes no sense to the machine:

stWhereCondition = Report.rptRMStatus.RecordSource = "qryRMStatus1"

I'm surprised it doesn't throw an error.

Anyway, it should look something like this:

stWhereCondition="([YourDateField] Between #1/1/2014# AND #1/31/2014#) AND [YourMatNoField]=12345"

The bracketed items refer to fields in the report recordsource.
Oct 16 '14 #3
jforbes
1,107 Expert 1GB
Hey Pookaroo85,

You are on the right track. Typically, when opening a Report in this manner, the Reports RecordSource is not changed. The RecordSource is setup to return a all records then when the Report is opened with the DoCmd.OpenReport method, a WhereCondition is supplied to filter the Report down to only the Records that are desired. It's basically splitting your Query into two parts, the part that selects the fields is what the Report is saved with, while the part that selects which rows is supplied by the Form when the Report is opened.

So what you will want to do is set the RecordSource of your report to a Query that you have made that returns all records(the last bullet point from Post #1), then build up the stWhereCondition string to filter Report.

This is an Example that should be close to what you need, but you will need to tweak it:
Expand|Select|Wrap|Line Numbers
  1. If Len(Nz(Me.IDH.Value, "")) > 0 Then
  2.     stWhereCondition = stWhereCondition & "IDH='" & Me.IDH.Value & "'"
  3. End If
  4. If IsDate(Me.StartDate.Value) AND IsDate(Me.EndDate.Value) THEN
  5.    If Len(stWhereCondition) > 0 THEN stWhereCondition = stWhereCondition & " AND "
  6.    stWhereCondition = stWhereCondition & "[NameOfDateField] Between #" & Me.StartDate.Value & "# AND #" & Me.EndDate.Value & "#)"
  7. End If
Oct 16 '14 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Andante.in.Blue | last post by:
Hi everyone! I was wondering if there is a away to use Access 97's build in filter-by-form function but restrict its effect to just the subform. I have a parent form that shows the major...
2
by: Seth Delaney | last post by:
I have a form with multiple unbound text boxes which serves as a "search form". I can enter my search parameters in the various boxes as needed and click okay. My records are then filtered to...
2
by: SirPoonga | last post by:
Is it possible that when having a user using Filter By Form in a form that no matter what the user enters in for a value that "LIKE *Value*" gets automatically replaced? Would I have to do...
2
by: Lenin Torres | last post by:
Hi everybody I have an Union Query that works fine. I used this query as the RecordSource for a Form. That Form is used as a subform in another form. Everything works fine, except for the "Filter...
3
by: dhowell | last post by:
In reading some of the posts on this group, it appears as though it is not strait forward at all to filter a form, which has subforms, by criteria which are either on subforms or span more than one...
6
by: fonzie | last post by:
Is there any way to include two unbound text boxes (for a start date and end date) to a filter-by-form? The users may want to filter by several different fields and they may want to include a date...
8
by: fonzie | last post by:
I have an edit button on my form that changes the recordsettype of the form, causing the form to requery. If I have a filter applied on the form before I hit the edit button, it is not applied to...
2
by: jcf378 | last post by:
hi all. I have a form which contains a calculated control ("days") that outputs the # of days between two dates (DateDiff command between the fields and ). However, when I click "Filter by...
8
by: Gari | last post by:
Hello, I am trying to build a filter query with some AND and OR. I have three text boxes and 5 check boxes. The checkboxes are linked via code to other textboxes for the purpose of the query. ...
5
by: Jim Mandala | last post by:
Using Access 2003 front end; SQL Server 2005 Back end: I have a complex form that has lots of data fields including about thirty or so checkboxes storing Yes/No data that I would like my users...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.