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.
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
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
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've got a database that exports invoices to pdf and mails them, using the outputto command for generating the invoices. I've got the outputto in a function that takes the name of the invoice report (strRptName), the filter on invoice number (stropenargs) and the filename containing the invoice number (strFilename) and returns the filename of the PDF if it could be generated.
Public Function PDFRpt(strRptName As String, strFileName As String, strOpenArgs As String) As String
DoCmd.OpenReport strRptName, acViewPreview, , , acHidden, strOpenArgs
If Reports(strRptName).HasData Then
DoCmd.OutputTo acOutputReport, strRptName, acFormatPDF, strFileName & ".pdf", False
PDFRpt = strFileName & ".pdf"
Else
PDFRpt = "Nodata"
End If
DoCmd.Close acReport, strRptName
End function
In the print and mail subroutine, I loop through the invoice headers calling this function. Filename and filter variables are filled in the loop, obviously using the same invoice number from the recordset.
This usually works, but occasionally, the contents of the pdf don't match up with the filter. For example: the filename says it contains invoice number 149, but if you open the pdf, it shows invoice number 153. There is a file with 153 in the name, that also contains invoice number 153.
I suppose it's transient conditions on the network that cause this hiccough, but is there any way I can prevent it? Perhaps using other code to create the pdf's?
Hope that someone can throw some light on this problem. Thanks in advance.
Ingrid
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.