Generating Report from User Inputs in Access Form - sql

Goal: To create an access form that takes user inputs combines those with a bound value in an access query calculates a final number and then generates a report.
The only issue I am having here in referencing the user inputs on the form to the actual report.
Any suggestions?

In the report it would be:
=Forms!YourUserInputFormName!txtUserInputBox
The form must be left open.

Design your query so that the field to filter by is just a data field, then use the WhereCondition parameter of OpenReport.
DoCmd.OpenReport "YourReportName",acViewNormal,,"My field=" & Me.txtUserInput
Variation for text field and Preview instead of Print:
DoCmd.OpenReport "YourReportName",acViewPreview,,"My field='" & Me.txtUserInput & "'"

Related

How do I set parameters for an embedded query on an Access report from an Access form?

I have an existing Access report that runs off a Query. In the existing report I have the parameters set with brackets; [CSRName], [StartDate], and [EndDate]. I have those parameters being filled form a form using the VBA below;
All of this works just fine. My problem is this; I have this query counting the total number of errors a CSR makes. I can easily display in the report the total number of errors. What I'd like to do is have another textbox on the report that shows the total number of completed items by the CSR. In order to do this I need to embed the Control Source for the textbox with another query. I have that query written and it works outside the form. What I need to do though is pass the same parameters that I am using to generate the report into this embedded query on the report. I can't figure out how to do that. I can pass the parameters to the report itself, and it works fine, but trying to do the same won't pass the parameters to the embedded query in the report. It keeps popping up an input box and asking me to give it the variables.
I assumed I could treat the embedded query the same that I could the query the report is based off, so I used the same process to try to pass the parameters to it as well, but it did not work.
Private Sub RunQuery_Click()
DoCmd.SetParameter "CSRName", Me.CSRNameCB
DoCmd.SetParameter "StartDate", "#" & Me.StartDate & "#"
DoCmd.SetParameter "EndDate", "#" & Me.EndDate & "#"
DoCmd.OpenReport "rpt_CSRErrorTracking", acViewPreview
End Sub
What I need is the method to pass the parameters from the form to both the query that the report is based off, and the query embedded in the report itself.
How about having the queries that the report and report field are based on store the "link" to the form's fields something like this (query by form concept)...
In the query's criteria for an appropriate field:
= [Forms]![MyForm]![CSRNameCB]
And:
Between [Forms]![MyForm]![StartDate] And [Forms]![MyForm]![EndDate]
And then on the form that opens the report you don't have to set any parameters at all.

Filter a Report based on user input

I created a report template which I want to open a single page report based on a specific IDENT NO. All of the fields in the report are then generated from the associated IDENT NO.
Currently, when I open the report, it will create a single page report for each ID number in the Report Data table when opened.
Instead, when the user is attempting to open the report, I want to prompt the user to enter the identification number of the specific report they are looking for so that it only opens that single individual 1 page report associated with the entered IDENT NO.
How could I achieve this filtering?
An alternative to using InputBox is to use a "parameter query" (documentation here)
Anywhere in a query criteria or report design view you may enter [IdentNoVariable] and a dialog box will appear (when the report or query is run) asking you to "Enter Parameter Value" for IdentNoVariable.
For example, if you wanted to restrict an ID # during a query (say the query that your report calls):
The dialog box seen appeared upon running the query and has the text of the variable seen in the query criteria. This same thing can be done in a report by entering [IdentNoVariable] into the part of the report where the entered value is desired.
You can display an InputBox for the user to insert the ID and then open and filter the report.
Dim id As String
id = InputBox("Enter the identification number:", "YourInputBoxTitle")
'if a value was entered, open and filter report
If Len(Trim(id)) > 0 Then
DoCmd.OpenReport "ReportName", acViewPreview, , "[IDENT NO] = " & id, acWindowNormal
End If

access create a report from a form input

I have a need to create a report from a user input on an Unbound form.
I have two tables where the data will come from:
Table Name: RMAMaster
Fields:
RMANbr (5 digit number)
DateIssued (date in MM/DD/YYYY format)
CompanyName (text)
CustNbr (6 digit number)
Table Name: RMAItems
Fields (that are relevant for the search):
RMANbr (relationship established to RMAMaster.RMANbr)
PartNbr (text)
Warranty (Yes/No)
Disposition (lookup from another table)
The form (Name: RMA Report Search) has the following Controls on it, the idea being that the user can fill in any number of fields. Blank fields would mean "Search all for that field" and filled in fields would be "search for the data in those fields".
CustNbr (being in the RMAMaster table)
PartNbr (being in the RMAItems table)
Date Opened <start> to <end> (being in the RMAMaster table)
Disposition (being in the RMAItems table)
Warranty (drop down with choice "Yes", "No" and "Any") (being in the RMAItems table)
(Possibility to add more fields as the user sees fit)
For example, I enter in Part number "G-5645" and leave "Warranty" as "any" and it will search for all the RMA's that have the part number "G-5645" and display them in the report. the report would be paid out sort of like this:
RMAMaster.RMANbr RMAMaster.DateIssued RMAMaster.CustNbr, RMAMaster.CompanyName
|
|-- RMAItems.PartNbr RMAItems.Warranty RMAItems.Disposition
|-- RMAItems.PartNbr RMAItems.Warranty RMAItems.Disposition
|-- (possibly repeated depending on how many items the customer sent back
I can build the report but I'm not wrapping my head around getting the data from the Form RMA Report Search and build a Report off of it. Do I use a Query? VBA Code that calls the report? Not sure how I should go about doing this. Any help would be appreciated.
Thanks!
If I understand you, you want to 'build' a report based on a search form. If that's correct, you don't need to build a report at run-time, but instead build a report based on a query that combines the fields you'd ultimately want to display from RMAItems, RMAMaster tables. The recordsource for this report should be this query.
Next, from the search form a user would select all applicable fields and open the report (maybe a button-click)? This button-click would generate a WHERE statement based on the selected fields. Rpt_Results would be the report you want to generate, stLinkCriteria is the criteria you want to pass. Iteratively build the linking criteria by adding to stLinkCriteria.
Dim stLinkCriteria As String
dim stDocName as String
stDocName = "RPT_Results"
stLinkCriteria = "[" & [PARTNumber] & "]=" & forms!FRM_Search.PartNumber
...
DoCmd.OpenForm stDocName, , , stLinkCriteria

DoCmd.OpenReport Asks for values Access07

I have a form that opens a report based on a combo box selection.
Which looks like.
The invoice shipment button opens the report via
DoCmd.OpenReport "ItemList4", acViewReport, , "ShipRef = " & Me.SRCB
SRCB is the combo box next to the shipment label.
When clicking the invoice shipment button I always get asked what the parameter value for S100018 is, so obviously it knows what the value is but isn't applying it to the filter when opening the report like so
How do I prevent this from happening?
I always create a query with a where clause that refers back to the textbox in the form. Then I build the report on the query, which selects 1 record, and I get my report.
I'l show a quick demo, here is a table named Persons:
Then create a form with a textbox, I named the form PersonForm:
Now creat a query that selects everything from the Persons table. In the where clause, open the builder, browse to the created form and select the textbox.
Then we create a very simple report with the report wizard, based on the query.
Now we go back to our created form and add a button. In the button, select for the option to open a report.
Now if you open the form in Form view, enter the name Ivo in my instance. And then click the button.
Of course you have to adjust the example to your context.
Create a query with the data that you need to generate the report. Then add a where clause in the query to the list box where you display your shipment id. Then let the report get the data from the query.
The problem seems to be the report. It looks like it's accesing a variable called S100018. Is this intended behaviour?
The problem was that it was not sending the filter argument as a string
it should be as such
DoCmd.OpenReport "ItemList4", acViewReport, , "ShipRef = '" & Me.SRCB & "'"

Select SQL Query using input from form fields Access 2007

I have a form with two combo boxes and a button that runs a select query with the code below. Currently the issue is it doesn't take the input of the combo boxes as the field input. Is this something that can be solved in SQL or would I have to build a form using VBA?
SELECT Field=[Forms]![TestInteractForm]![Combo18], Field=[Forms]![TestInteractForm]![Combo20]
FROM TestInteract;
Although it may not be apparent, your goal is basically the same as using a query parameter in the SELECT clause to identify a field.
Unfortunately, Access does not support that feature. Your combo box value, which is a field name, will be recognized as a valid text string, but there is no way to inform Access that string should be interpreted as a field name.
The closest you can get would be to use a Switch statement which maps each field name string to the corresponding field value. So, if TestInteract and Combo18 both include 4 fields (fld1, fld2, fld3, and fld4) something like this could actually work ...
SELECT
Switch(
[Forms]![TestInteractForm]![Combo18]='fld1', [fld1],
[Forms]![TestInteractForm]![Combo18]='fld2', [fld2],
[Forms]![TestInteractForm]![Combo18]='fld3', [fld3],
[Forms]![TestInteractForm]![Combo18]='fld4', [fld4]
) AS first_column
FROM TestInteract;
However, I'm not recommending you adopt that method. I think it could be simpler to generate the SELECT statement in your command button's click event.
Dim strSelect As String
strSelect = "SELECT [" & Me.Combo18.Value & "]" & vbCrLf & _
"FROM TestInteract;"
And then use strSelect where you need it ... as a form's RecordSource, as the data source for a recordset, or as the .SQL property of a saved query.