Open Report if field contains certain word - ms-access

We have a form with 2 buttons pointing to 2 reports, ideally we would like one button on the form to prevent user printing incorrect certificates
Is there a way to have only one button so when a user clicks on the button to run a report, that
if [type] contains "semi" open report "semi cert" & if [type] contains "spring" open report "spring cert"
these are our currents buttons
Private Sub semi_Click()
DoCmd.OpenReport "Quick Hitch (SEMI) Certificate", acViewPreview, , "[no] = " & Me.[no]
DoCmd.PrintOut
DoCmd.Close
End Sub
Private Sub spring_Click()
DoCmd.OpenReport "Quick Hitch (SPRING) Certificate", acViewPreview, , "[no] = " & Me.[no]
DoCmd.PrintOut
DoCmd.Close
End Sub

Assuming that type is the name of your text box
Private Sub OpenReport_Click()
Dim reportName As String
Select Case Me.[type].Value
Case "semi"
reportName = "Quick Hitch (SEMI) Certificate"
Case "spring"
reportName = "Quick Hitch (SPRING) Certificate"
Case Else
MsgBox "Report type '" & Me.[type].Value & "'is unknown"
Exit Sub
End Select
DoCmd.OpenReport reportName , acViewPreview, , "[no] = " & Me.[no]
DoCmd.PrintOut
DoCmd.Close
End Sub

Related

Error On click Button Docmd Report Error 3464

Hi Everyone How Are You I hope You Are Well.
I Am Working On A MS Access Application And Want Fetch Result From List Box With On Click Button
But I'm Facing A Error Data Type Mismatch In Criteria expression
Here is My Code On Click Button
Private Sub ViewBalance_Click()
'Dim listrpt As String
If IsNull(listrpt) Then
MsgBox "Please Select The Account Name", vbOKOnly, "Warning"
Exit Sub
End If
DoCmd.OpenReport "List All Parties Balance", acViewPreview, , "[Account ID] = """ & listrpt &
""""
Me.FilterOn = True
End Sub
And Here is My On Report Load Filter But This Filter Working Fine Above Code Give me Error
Report Load Code
Private Sub Report_Load()
DoCmd.Maximize
If MsgBox("Do You Want To Filter Available Balance Only?", vbYesNo + vbQuestion) = vbYes Then
Dim strFilter As String
strFilter = "[RQ]>0"
'verify that it looks good
Me.Filter = strFilter
Me.FilterOn = True
End If
End Sub
Report Name: List All Parties Balance
Query Name Where is Field Name: Account ID is: ListBalance
And listrpt Response Account ID = 60023
Please Anyone Give me A Support & Help Thanks in Advance

Ms Access filter reports in runtime mode

Does anyone knows how to filter reports the right way within Access runtime mode?
The usual code with DoCmd doesn't work.
This is what I tried for the report:
Private Sub Befehl217_Click()
DoCmd.OpenReport "Tagesbericht", acViewPreview
End Sub
Private Sub Bezeichnungsfeld26_Click()
DoCmd.GoToControl "DateFilter"
DoCmd.RunCommand acCmdFilterMenu
End Sub
This didn't work. Access complained that "FilterMenu isn't available".
I tried to create a context menu but this only displayed me cut, copy and paste.
You confirmed your report includes a control named Bezeichnungsfeld26 and when the user clicks in that control, you want to bring up the filter menu for that control.
When the user clicks in that control, it has the focus, so there is no need for GoToControl. And you don't want to go to a different control if you want the user to filter on Bezeichnungsfeld26.
Disable the GoToControl line ...
Private Sub Bezeichnungsfeld26_Click()
'DoCmd.GoToControl "DateFilter"
DoCmd.RunCommand acCmdFilterMenu
End Sub
You can use the Filter property:
Me.Filter = "[YourField] = " & somevalue & ""
Me.FilterOn = True
or, to expand on your current method:
DoCmd.OpenReport "Tagesbericht", acViewPreview, , "[YourField] = " & somevalue & ""
If you filter for a date you must pass a properly formatted string expression for the date:
Dim FilterDate As Date
FilterDate = Date
DoCmd.OpenReport "Tagesbericht", acViewPreview, , "[DateFilter] = #" & Format(FilterDate, "yyyy\/mm\/dd") & "#"

Access Report - show current recordsource of another form

I have a report that displays another form's recordsource. When I add some record on this form and click for report, this records is not being displayed. How can I achieve displaying what is on screen - allways ? Here is my simple code for report:
Private Sub cmdOpenReport_Click()
DoCmd.OpenReport "MyReport", acViewReport
End Sub
Private Sub Report_Open(Cancel As Integer)
Me.RecordSource = Forms![MyForm].Form.RecordSource
End Sub
You need to close the Report before re-opening it and save the new record.
In the click event of your Open Report button insert the following:
Private Sub OpenReport_Click()
DoCmd.RunCommand acCmdSaveRecord
DoCmd.Close acReport, "Test1", acSaveYes
DoCmd.OpenReport "Test1", acViewPreview
End Sub
Try requerying your Forms Recordset before opening the report:
Private Sub cmdOpenReport_Click()
Me.FilterOn = False
Me.Requery
DoCmd.OpenReport "MyReport", acViewReport
End Sub
I have an answer, I knew It's not so easy (in click event of OpenReport button):
Dim strWhere As String
Me.Dirty = False
With Me.Recordset.Clone
Do Until .EOF
strWhere = strWhere & "," & !ID
.MoveNext
Loop
End With
strWhere = Mid(strWhere, 2)
DoCmd.OpenReport "MyReport", acViewReport, WhereCondition:="ID In (" & strWhere & ")
Answer provided by Leigh Purvis (Access MVP), a BIG thanks once more !!

VBA Access strange behavior with global variables

I have a form that calls a series of reports, one after the other. I also have a global variable that I use as a counter. Every time a page's PageFooterSection_Format is called, I want to increment the count. The problem is, the counter is counting higher that it should. Here is my code:
I have a module called genericFunctions:
Option Compare Database
Public pageCount As Integer
In my form, I have a loop that calls this:
'before loop
pageCount = 1
'start loop, which I left out for brevity
'run in previewView first so page footer format function is called
DoCmd.OpenReport reportName, acViewPreview, , , acHidden, !ID
'then run this to open in report view, so onload event runs
DoCmd.OpenReport reportName, acViewReport, , , acHidden, !ID
'save report as a pdf
DoCmd.OutputTo acOutputReport, reportName, "PDF", rptPath
'close report
DoCmd.Close acReport, reportName
'I then have a method that "stitches" these individual reports into one PDF
now, in my report, I have this code:
Private Sub PageFooterSection_Format(Cancel As Integer, FormatCount As Integer)
If (Report.CurrentView = 5) Then
'a textbox in the report
Me.pgNumber.Value = pageCount
pageCount = pageCount + 1
End If
End Sub
I then place a breakpoint in the event. This event is hit 3 times, as it should. So, I would expect pgNumber text box to have 1,2,3 as their values but it has 2,4,6 instead. No where else in my code do I increment the pageCount variable. What's going on? Is this a scope issue?
PageFooterSection_Format runs both for DoCmd.OpenReport reportName, acViewPreview and for DoCmd.OutputTo acOutputReport, reportName, "PDF", because the latter is like a print command.
In both cases, Report.CurrentView = acViewReport (5). I'm not sure what to make of that - it feels like a bug. (Note: this is Access 2010)
Anyway, since DoCmd.OutputTo is like a printing (and thus like a print preview), you can simply omit this line:
DoCmd.OpenReport reportName, acViewPreview, , , acHidden, !ID
At least that worked for me.
Note: Breakpoints in event procedures can be misleading as to what happens when. It is safer to use Debug.Print calls.
My test code was:
Sub ReportTesting()
Const reportName = "Bericht4"
'before loop
pageCount = 1
'run in previewView first so page footer format function is called
Debug.Print pageCount, "acViewPreview"
DoCmd.OpenReport reportName, acViewPreview, , , acHidden
'then run this to open in report view, so onload event runs
Debug.Print pageCount, "acViewReport"
DoCmd.OpenReport reportName, acViewReport, , , acHidden
'save report as a pdf
Debug.Print pageCount, "acOutputReport"
DoCmd.OutputTo acOutputReport, reportName, "PDF", "C:\test.pdf"
'close report
DoCmd.Close acReport, reportName
Debug.Print pageCount, "done"
End Sub
and
Private Sub Report_Open(Cancel As Integer)
Debug.Print "Report View: " & Me.CurrentView
End Sub
Private Sub PageFooterSection_Format(Cancel As Integer, FormatCount As Integer)
If Me.CurrentView = acViewReport Then
Debug.Print "Footer - acViewReport"
'a textbox in the report
Me.pgNumber.Value = pageCount
pageCount = pageCount + 1
Else
Debug.Print "Footer - other view" ' <-- this never happens
End If
End Sub
Output in Immediate window:
1 acViewPreview
Report View: 5
Footer - acViewReport
2 acViewReport
Report View: 6
2 acOutputReport
Footer - acViewReport
3 done

Access DoCmd print method

I'm selecting some record and I have option to view them as a report (by pushing Command33 button)
Private Sub Command33_Click()
DoCmd.OpenReport "repAwariaOtwarta", acViewPreview, , "[dbAwarieOtwarte].[ID] =" & Me![ID]
End Sub
After clicking the button I have report pop up.
What I should do to print that report without overwiewing it after clicking Command33?
I was trying to use DoCmd.PrintOut, but I seem to failed.
Just combine your ideas:
Open the report, use the print command.
Private Sub Command33_Click()
DoCmd.OpenReport "repAwariaOtwarta", acViewPreview, , _
"[dbAwarieOtwarte].[ID] =" & Me![ID]
DoCmd.PrintOut , , , , 1
End Sub
Here are the paramteres for Printout command
https://msdn.microsoft.com/en-us/library/office/ff192667.aspx
If you want, you can do a close method for the report after the print commmand like
DoCmd.Close acReport, "repAwariaOtwarta"