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
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'm currently running Access 2007. I have a reporting manager that allows for the user to set parameters. Customize dates and allows for selecting what date field needs to be modified (timecreated, timemodified, txndate). The reporting aspect works perfectly.
Within the report (totalshipped), the user can double click the body of the report and it'll go from "big picture" summary to a detailed report (totalshippeddetailed).
Basically in order for it to work properly, the dates from totalshipped and the customer name from the body (on double click) need to match in the totalshippeddetailed report when it opens. Here is an example of what I've tried:
Private Sub ftrReport_DblClick(Cancel As Integer)
DoCmd.OpenReport "TotalShippedDetailed", acViewReport, "", me.filter
End Sub
The report successfully opens up and passes the filter property (dates) onto the totalshipped detailed report, the problem is that I need to reference the [customerref_fullname] (customer) as well which I was doing by using this line:
[reports]![TotalShipped]![customerref_fullname] = [customerref_fullname]
This works as a reference to the customer but I need to combine the me.filter property with the reference to the customerref_fullname, an error occurs when I try:
Private Sub ftrReport_DblClick(Cancel As Integer)
DoCmd.OpenReport "TotalShippedDetailed", acViewReport, "", me.filter & _
[reports]![TotalShipped]![customerref_fullname] = [customerref_fullname]
End Sub
Another way I tried it which works but doesn't allow the detailed version the report to be filtered by anything but the [txndate] :
DoCmd.OpenReport "TotalShippedDetailed", acViewReport, "", _
"[reports]![TotalShipped]![customerref_fullname] = [customerref_fullname] and [txndate] >= [forms]![frmrptdatemanager]![txtstartdate] And [txndate] <= [forms]![frmrptdatemanager]![txtenddate] "
And lastly I took the above code and tried to reference the date field itself in the form which will give an error:
DoCmd.OpenReport "TotalShippedDetailed", acViewReport, "", _
"[reports]![TotalShipped]![customerref_fullname] = [customerref_fullname] and [forms]![frmrptdatemanager]![cboDateField] >= [forms]![frmrptdatemanager]![txtstartdate] And [forms]![frmrptdatemanager]![cboDateField] <= [forms]![frmrptdatemanager]![txtenddate] "
There are many more examples I've tried but none of them can accomplish my goal: allow the users to double click the body of the report to see the detailed report by customer with the same date range as the main report.
Please helP!!!!
Remember that the filter parameter is like a WHERE clause without the WHERE keyword at the front of it.
What you're doing here:
DoCmd.OpenReport "TotalShippedDetailed", acViewReport, "", me.filter & _
[reports]![TotalShipped]![customerref_fullname] = [customerref_fullname]
You're taking the existing filter condition and adding to it, but you didn't include an AND keyword to link the current filter condition and the next condition.
You would need to do:
DoCmd.OpenReport "TotalShippedDetailed", acViewReport, "", me.filter & _
" AND [reports]![TotalShipped]![customerref_fullname] = '" & [customerref_fullname] & "'"
Notice there, also, that you have to concatenate the variable or control name into the filter string to pick up the value of the variable or control rather than the name of the variable or control. The value also has to be offset with correct punctuation when it's a string or date: single-tick quotes for string values and hash tag/pound symbols for dates.
To help develop and debug things like this, I recommend using a string variable to build up the entire filter condition. Inspecting the contents of the string variable or dumping the value to the immediate window (Debug.Print) before you use it as the filter parameter in the OpenReport method will help you catch missing ANDs, missing spaces, and missing punctuation in the filter condition.
Your DblClick method will look something like this:
Private Sub ftrReport_DblClick(Cancel As Integer)
Dim FilterCondition as String
FilterCondition = me.filter
FilterCondition = FilterCondition & " AND [txndate] >= #" & [forms]![frmrptdatemanager]![txtstartdate] & "# AND [txndate] <= #" & [forms]![frmrptdatemanager]![txtenddate] & "#"
Debug.Print FilterCondition
DoCmd.OpenReport "TotalShippedDetailed", acViewReport, "", FilterCondition
End Sub
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 new to Access. I just created my first DB w/ a report in Access 2010. The report requires a parameter to run. If I run the report, then try to print, it prompts for the parameter for every page of the report. This is a deal breaker for me. Is there something I can do to "fix" this. I might expect it once, but not for every page!
If you don't mind using VBA you can use an InputBox to acquire the value from the user on the Report's Open event. Then use that value to create an SQL Statement and set it to the Report's RecordSource. Your code should look something like this. Note that I didn't write the code in MS Access or test it.
Private Sub Report_Open(Cancel as Integer)
Dim sSQL As String, sInput as String
sSQL = "SELECT * FROM Contacts "
sInput = InputBox("Enter a Name", "Enter a Name")
If sInput <> "" Then
sSQL = sSQL & " WHERE FirstName = '" & Replace(sInput, "'", "''") & "'"
End If
Me.RecordSource = sSQL
End Sub
I tried to display a list of all requests from a specific dept of my table
So I created a form with all the tbl fields that I want to display on the form view of "details" section like this .
dept name Totalnum req# ticket
Then I have created a combo box with pre-defined values as 'depttest' field.
Then I used the following code on change value of field but form is displaying only the first record of the category and not showing all the records .... can some one please help me with this logic..
Option Compare Database
Option Explicit
'Set default record source of form
Const strsql = "SELECT tbl.dept,tbl.name,tbl.[Totalnum],tbl.[req#],tbl.[Ticket] FROM tbl"
Private Sub depttest_Change()
Dim strFilterSQL As String
strFilterSQL = strsql & " Where [dept] = 'me.depttest.value';"
Me.RecordSource = strFilterSQL
'DoCmd.RunSQL strFilterSQL
Me.Requery
End Sub
You are passing me.depttest.value as a string not a value. Try:
strFilterSQL = strsql & " Where [dept] = '" & me.depttest.value & "';"