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.
Related
I create a report from this query:
select DocumentNumber,DocumentDate, from Documents where DocumentNumber ="Feb_345"
But I want to have a dynamic query. I want to create a form where user will enter a Document number and after that i will save this number in string variable in my OnClick function and after that I will sent this document number to my query as parameter in where statment. So when user open the report then he will see a report for document that he enter in the form earlier.
I' ve got a problem because when I save document number in my vba script I dont know how to send this variable to query as a parameter.
You can reduce your query to:
select DocumentNumber, DocumentDate from Documents
and then open the report filtered for the selected document number:
Dim DocumentNumber As String
Dim WhereCondition As String
WhereCondition = "DocumentNumber = '" & YourSelectedDocumentNumber & "'"
DoCmd.OpenReport NameOfYourReport, acViewNormal, , WhereCondition
For two or more parameters, expand the Where condition:
select DocumentNumber, Author, DocumentDate from Documents
and:
Dim WhereCondition As String
WhereCondition = "DocumentNumber = '" & YourSelectedDocumentNumber & "' And Author = '" & Author & "'"
DoCmd.OpenReport NameOfYourReport, acViewNormal, , WhereCondition
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.
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
when double clicking on a value i need to open a form. the value gets passed to the opened form. I want the form to directly filter on that value instead of pushing on a button first. i tried filtering on change and on load but it doesn't work. when loading it doesn't know the value because it gets added after it opened the form.
this is the code for passing the value:
DoCmd.OpenForm "SubmenuRubrieken", acNormal
Forms!SubmenuRubrieken.Tv_rubrieknaam.Value = Me.Tekst14.Value
this is the code for filtering on that value in Tv_rubrieknaam:
Dim filter As String
filter = ""
If Not IsNull(Tv_rubrieknaam) Then filter = filter & " AND rubrieknaam = '" & Tv_rubrieknaam.Value & "'"
Me.filter = Right(filter, Len(filter) - 5)
Me.FilterOn = True
for some reason it doesn't trigger the filter on changing the value of Tv_rubrieknaam. how do i need to solve this?
I guess, the Form_Load() event is finished before you set the value (OpenForm is performed before value is set), so you would have to do the filtering in the OnChange() event of the Textfield or similar.
Better: pass the Me.Text14.Value with the DoCmd.OpenForm command either as a prepared whereCondition OR as OpenArgs with filtering option in the On_Load event.
A basic example:
I have a Form1 with a TextBox called Text0 on it. Text0 has a value of 2.
I have a Form2 with a Table called Table1 as Recordsource. Table1 has a column called Field1 containing numbers between 1 and 3
All I need to do is add the following code into the module of the Form1 and the moment I click on Text0 Form2 will be openend filtered down to rows with Field1 = 2
Private Sub Text0_Click()
DoCmd.OpenForm "Form2", acFormDS, , "Field1 = " & Nz(Me!Text0, 0)
End Sub
I am developing a search form / report builder in Access 2007 called frmSearch. The existing search form works well, showing its results in a subform of a tabcontrol. Then I can click a button to show a report of all tests, which works fine. I want to modify the code to show a report for a single fldTestsID, but am stuck with the Form/Subform/Control path syntax.
There are two tabs: tabResultsTabular shows a subform frmResultsTabular as a query-like list of row. tabResultsRecord shows a subform frmResultsRecords, showing a single record. The report is rpt_TestDatasetExposureEffects. The report's underlying query is q_TestDatasetExposureEffect containing a field called fldTestsID.
So, the path is frmSearch to tabResultsRecord containing button cmdQAReportResults to frmResultsRecords to fldTestsID.
I see that that are other posts for the same error, but did not get them to work. The Access 2007 docs on DoCmd.OpenReport do not mention this specific instance.
Here is the code of the cmdQAReportResults click event including the options I have tried. It fails on the line strRptWhere =. The DoCmd.OpenReport syntax is working based on the Access 2007 docs.
Private Sub cmdQAReportResults_Click()
doQAReport
End Sub
Private Sub doQAReport()
'On Error GoTo Err_doQAReport 'comment during debugging
Dim stDocName As String ' report name
' Dim strRptSQL As String ' report SQL String
Dim strRptWhere As String ' report WHERE clause
stDocName = "rpt_TestDatasetExposureEffects"
'Override the recordsource to match the current record TestsID
' strRptSQL = "SELECT * FROM q_TestDatasetExposureEffects WHERE fldTestsID = " & fldTestsID
' DoCmd.OpenReport stDocName, acPreview
strRptWhere = "0 = 0"
strRptWhere = "fldTestsID = " & Me.Form![tabResultsRecord].fldTestsID.Value 'error 468
' other attempts follow
' strRptWhere = "fldTestsID = " & Forms("frmSearch").Controls("tabResultsRecord").Form.Controls("frmResultsRecords").Form.Controls("fldTestsID").Value
' strRptWhere = "fldTestsID = " & Me.Form.fldTestsID 'error 2465
DoCmd.OpenReport stDocName, acPreview, , strRptWhere
' Reports("rpt_TestDatasetExposureEffects").RecordSource = strRptSQL
Exit_doQAReport:
Exit Sub
Err_doQAReport:
MsgBox Err.Description
Resume Exit_doQAReport
End Sub
The correct syntax is:
Forms(<Parent Form>).<control container for subform>.form.<control>
In your case this might be:
Forms("frmSearch").frmResultsRecords.Form.fldTestsID
frmResultsRecords is the name of the subform, but is it also the name of the subform's container?
If not, replace it with the name of the control on the main form containing the subform.