Is it possible to have a user choose yes/no and add a parameter in a query based upon that answer?
I have a table that has a 'status' field. One of the statuses can be complete. There is a report that prints out that has this field. Sometimes I want the records that are marked as complete, and sometimes I do not.
Is it possible to prompt the user and ask if they want the completed records, using the criteria/expression builder in a query?
I think the best way to handle this would be to use a yes-no msgbox to learn whether the criteria should be shown, then run the report using the "where" parameter on the DoCmd.OpenReport command.
Dim intAnswer As Integer
Dim intAnswer As Integer
intAnswer = MsgBox("Show the details?", vbYesNo)
If intAnswer = vbYes Then
DoCmd.OpenReport "Form1", acViewPreview, , "[Status] <> ""Completed""", acWindowNormal, "Yadda Yadda"
Else
DoCmd.OpenReport "Form1", acViewPreview, , , acWindowNormal, "Zippity Doohdah"
End If
If you would like to inform the report of what the user selected one method is to pass an OpenArgs string and then have a label on the report that shows that string. (Above I pass the phrase "Yadda Yadda", or "Zippity Dodah")
On the report, add a label (here called Label1) and set its Caption equal to the OpenArgs:
Private Sub Report_Open(Cancel As Integer)
If Not IsNull(Me.OpenArgs) Then
Me.Label1.Caption = Me.OpenArgs
End If
End Sub
Related
I have a form in MS Access where on button click event I have the following code running. My objective is to make a user enter a value on button click on a form and send that value to report using openargs. I wanted to add the openargs parameter in that query but when I enter a value on the inputbox and hit enter I get a prompt called "Enter Parameter" which asks me to enter value for all the fields in the report. I am new to VBA and don't know what I am doing wrong
Form
strInput = InputBox("Which value did you want to print?")
DoCmd.OpenReport "01-REPORT", acPreview, , , , OpenArgs = strInput
Report
In my report on open report event I have
Private Sub Report_Open(Cancel As Integer)
If Me.OpenArgs > 0 Then
Me.RecordSource = "PRINT FINAL DATA WHERE " & "InvNum = '" & Me.OpenArgs & "'"
End If
End Sub
*PRINT FINAL DATA * is the name of a query which is the recordsource of the report.
To open report with filtered data you can do this:
In Design view for report's RecordSource set query with all data
(your *PRINT FINAL DATA *). And save it.
For form in click handler write this (i omit all error check & etc)
strInput = InputBox("Which value did you want to print?")
DoCmd.OpenReport "01-REPORT", acPreview, , "InvNum = '" & strInput & "'"
The forth parameter of OpenReport is WhereCondition which filters data before doing report.
P.S. And report asks for value of every field because your report don't have saved value in RecordSource when you open it. Or query have no such fields.
I am close on having syntax accurate, just one big problem!
The User ID that I am capturing from the input box, is not being passed into the report, so I have to input the user id twice
What needs to be altered in order to achieve this?
Public Function userProd() As Integer
Dim userID As String, saveloc As String, filename As String, reportname As String
'Report Name
reportname = "rpt_UserProduction"
'Setting Save location
saveloc = "C:\Test\"
'Setting filename
filename = "userProd "
'Getting User ID To Process
userID = InputBox("Enter User ID:", "VBA InputBox Function")
'Putting together full save location
saveloc = saveloc + filename + userID + ".pdf"
If userID = "" Then
'Do Nothing and Stop Processing
Exit Function
Else
If IsNumeric(userID) Then
'Preview Report
DoCmd.OpenReport reportname, acViewPreview, , "User_ID=" & userID
'Save As PDF
DoCmd.OutputTo acOutputReport, reportname, acFormatPDF, saveloc, True
'Close Report
DoCmd.Close acReport, reportname
End If
End If
End Function
Is the User_ID field a text field that holds a number? If so, you need to add single quotes to the filter. ... "User_ID='" & userId & "'". If it's not text, are you getting too many records or not enough? Is there another filter being applied when the report loads, or in the underlying report record source that is causing the filter to not work as expected?
If "twice" means once for the inputbox and once for the report, then "User_ID=" in your where condition is either misspelled or excluded from the query you use as source for the report.
I am trying to print a report called Carton Labels, and I want to print a specific record that the user just created. Every time I try using Me.cartonNo which is a actual field on the current form the user is on. He gets prompted for input. What am I doing wrong?
EDIT:
Dim strDocName As String
Dim strFilter As String
DoCmd.RunCommand acCmdSaveRecord
strDocName = "Carton Labels"
strFilter = "[cartonNo] = Forms![frm_addFinishedGoodsInventory]![cartonNo]"
DoCmd.OpenReport strDocName, acViewPreview, strFilter
my print button doesn't work, it acts as if nothing was stored in the strFilter, but if I create Dim intFilter As Integer and store it into a integer, I can clearly see the cartonNo if I set a break point, yet I dont in the strFilter.
You need to resolve the carton number before you pass it to OpenReport. By placing the Forms! reference outside the quotation marks like this:
strFilter = "[cartonNo] = " & Forms![frm_addFinishedGoodsInventory]![cartonNo]
you are creating the string [cartonNo] = 6 and then passing that to the report. If the field were a text field (or date), you'd need to include quotes (or #s) around the value.
I'm using VBA to dynamically load the content of a report, and depending on which report is selected from the control panel form I've built, the report's query might be filtered.
At the beginning of my Report_Open function, I have this:
Private Sub Report_Open(Cancel As Integer)
Me.Filter = "VIP=True"
Me.FilterOnLoad = True
Now, this was working when I started this project - I had commented out these lines, and when uncommenting them discovered that this doesn't work properly anymore. Instead of the filter being applied when the report is loaded, I have to reload the report for the filter to work - either by switching to print preview and switching back to report view, or by switching to design view and then back to report view (in design view, the selected filter does display in the properties pane).
I am loading the report using command buttons that allow the user to view, export to PDF, or print (opens in print preview). None of these commands cause the report to open with the filter applied - it has to be reloaded.
The commands I'm using to load the report are below for reference:
If (Action = "View") Then
DoCmd.OpenReport "Test", acViewReport
ElseIf (Action = "PDF") Then
DoCmd.OutputTo acOutputReport, "Test", acFormatPDF
ElseIf (Action = "Print") Then
DoCmd.OpenReport "Test", acViewPreview
End If
Ok, I have no idea why Me.Filter and Me.FilterOnLoad don't work, since from everything I have seen on MSDN and elsewhere, it should work. That being said, I figured out that I can use DoCmd.ApplyFilter instead:
'Check if VIP - add filter if necessary
If (getGlobal(1) = True) Then
DoCmd.ApplyFilter , "VIP = True"
End If
I'd still like to know why the other way was behaving so oddly, if anyone has any ideas...
As #David-W-Fenton suggested, use the WhereCondition with OpenReport instead of setting a Filter expression. Your WhereCondition can be the same string you were using for the Filter expression.
Also, if you give OpenReport an empty string as the WhereCondition, the effect is the same as no WhereCondition, so this (untested) code should work whether or not your getGlobal(1) returns True.
Dim strWhereCondition As String
Dim strReport As String
strReport = "Test"
If (getGlobal(1) = True) Then
strWhereCondition = "VIP = True"
End If
Select Case Action
Case "View"
DoCmd.OpenReport strReport, acViewReport, , strWhereCondition
Case "PDF"
DoCmd.OpenReport strReport, acViewReport, , strWhereCondition
DoCmd.OutputTo acOutputReport, , acFormatPDF
Case "Print"
DoCmd.OpenReport strReport, acViewPreview, , strWhereCondition
End Select
Notice also that DoCmd.OutputTo, without an ObjectName argument, uses the active object ... which will be the "Test" report in this case.
I have a form with a cmdbutton that at the moment opens another form and shows all records for several types of PartitionStyles and TrimFinishs (486 at present), I need to be able to filter the second form to show only the TrimFinish I need.
Private Sub lbl600SeriesS_Click()
Dim stDocName As String
Dim stLinkCriteria As String
stDocName = "frmModules"
stLinkCriteria = "Forms!frmModules![TrimFinish] = 1"
DoCmd.OpenForm stDocName, , , stLinkCriteria
End Sub
At the moment it shows only a new record, I know there should be 162 records using 1, what have I missed or done incorrect.
Base stLinkCriteria on a field in frmModules' RecordSource. So, if the RecordSource includes a numeric field named TrimFinish, try something like this:
stLinkCriteria = "[TrimFinish] = 1"
If the RecordSource is a query drawing from more than one table, you can qualify the field name with the table alias:
stLinkCriteria = "YourTableAlias.[TrimFinish] = 1"
If you still have trouble, edit your question to describe frmModules' RecordSource. If it's a query, paste in the SQL View of the query.