How to refer to a report's control via a form? - vba

I have a report that can be opened from two different forms, and depending on from which form it is opened, it's title should be changed. For example, if I press the print button on the other form, then it refers to the report it's about to print and uses Label.Caption to change the title.
Does anyone have insight on this?

When you open the form, call it using the parameter OpenArgs (the last parameter):
DoCmd.OpenReport "YourReportName",,,,, Me.Name
Read that when the report is opened:
Private Sub Report_Load()
Me.Caption = Nz(Me.OpenArgs)
End Sub

Related

Filtering Access subform2 via VBA

I have a database with a main form called frmIndex.
frmIndex has a subform1 control named Sub.
Sub contains a form named frmMaintenance.
frmMaintenance has a subform2 control named Child9.
Child9 contains a form named frmHolidays.
In frmHolidays I have a combo named cboCountry and a code that filters the forms underlying table by countrycode, so that I can see the holidays for a specific country.
Private Sub cboCountry_AfterUpdate()
DoCmd.ApplyFilter , "[COUNTRYCODE]='" & Me.cboCountry & "'"
End Sub
The filter works fine when the frmHolidays is open as stand-alone form, but it doesn't work when frmHolidays is opened inside frmIndex/frmMaintenance.
Can anyone help in fixing this issue please?

Access button to open multiple reports depending on user input

I am new to access so this might be an easy task or I am just trying to tackle it wrongly. I have a report that has various columns, sample id, sample time, sample type, dry matter, moisture. I am trying to create a button that has an input box for the user to chose what column to sort the report by. So far I thought of creating various reports that have been sorted by each column, named the reports by the column that sorts them then I am trying to have the open report action have a parameter that opens the report linked to the column entered at the input box. Is this even possible or is there a workaround for this.
PS. I am avoiding creating various buttons since it will fill up the screen.
Okay, this is pretty generic and will require some tweaking but it shows the core of how to do this.
To start with you need a module (so not form/report code). This is where the globals will be assigned values:
Option Compare Database
Option Explicit
Global rptname As String
Global fldname As String
Sub setRptName(name As String)
rptname = "Report Sorted by: " & name
fldname = name
End Sub
You will call that code inside the Click() event of your command button on your form and then open the report after that. This will take a combo box value and pass that value to the module code, creating the two global variables.:
Private Sub cmd_report_Click()
mdl_Globals.setRptName Me.cmb_fields.Value
DoCmd.OpenReport "Report1", acViewPreview
End Sub
I'm unsure if this will work with non-preview views, but you probably want preview anyway.
Lastly in the report code behind, you need the Load() and open() events, you may be able to get it to work with both inside one or other but I know this one works.
So to set the caption:
Private Sub Report_Load()
Me.lbl_header.Caption = rptname
End Sub
And then to sort:
Private Sub report_open(Cancel As Integer)
Me.Report.OrderBy = "[" & fldname & "]"
Me.Report.OrderByOn = True
End Sub
If your entry box on the form does not have entries that exactly match the name(s) of the fields in the table you will get a parameter popup.

Access: trying to open a form and populate a textbox with the value from another form

Okay, I'm obviously doing something wrong, but can't figure out what it is, and this should be pretty simple.
I have a button on a form that opens another form and populates a textbox in the second form with a value from the first form.
Here is my code:
Private Sub test_Click()
DoCmd.OpenForm "subfrm_EA_COMMENT_ADD"
Forms!subfrm_EA_COMMENT_ADD.txtUWI = Me.txtUWI
End Sub
This works just fine, however I want the form to open in a dialog window, so I modified the script to this:
Private Sub test_Click()
DoCmd.OpenForm "subfrm_EA_COMMENT_ADD", acNormal, , , acFormAdd, acDialog
Forms!subfrm_EA_COMMENT_ADD.txtUWI = Me.txtUWI
End Sub
Now I am getting a run-time error 2450.
I've searched around regarding this error, but can't find any answers that work.
Can anyone point me in the right direction as to what I'm missing?
Thanks!
acDialog parameter causes code execution of first form to suspend until second form is closed. So either remove the parameter or place code in second form to populate its own record.
Pass value with OpenArgs argument:
Private Sub test_Click()
DoCmd.OpenForm "subfrm_EA_COMMENT_ADD", acNormal, , , acFormAdd, acDialog, Me.txtUWI
End Sub
To read OpenArgs property, code behind second form, possibly in the Current event:
If Me.NewRecord Then Me.txtUWI = Me.OpenArgs
I found a work-around for this issue using a temp variable. I created a temp variable at the beginning of my Add Comment button, then set the unbound text box in the Comment dialog window to the value of the temp variable. This works like a charm and I can now open my windows in dialog instead of normal.
I just wanted to let others know in case they run into this issue.

How to navigate to a form based on a comb box selection in Microsoft Access

I'm creating a form for my Microsoft Access database. I have a combo box, and I need it to navigate to the form based on which item the user clicks.
How would I do this? I know it requires some VBA code, but none of the methods I have tried have worked thus far.
The form I am trying to navigate to is called "Forms_Reports"
My current code:
Private Sub Combo0_AfterUpdate()
If Me.Combo0.Value = 1 Then
DoCmd.OpenForm "Forms_Reports", acNormal
End If
End Sub
Your code is basically okay, but you can't combine it in a single line. Also, I'm guessing your form is really just named "Reports". You don't include the Form_ prefix displayed in the project window. Try this:
Private Sub Combo0_AfterUpdate()
If Me.Combo0.Value = 1 Then
DoCmd.OpenForm "Reports", acNormal
End If
End Sub

Access 2007 VBA - Rename report upon closing it?

I have a report that is dynamically generated depending on the button pressed on my main form, in order to change the filter, the query used, etc. I have DoCmd.Rename working to rename the report to the current (dynamic) report title. However, it appears that I cannot rename the report back to a generic name upon closing the report.
Using the Report_Close() event doesn't work; Access tells me the report is still open and therefore can't be closed. Using DoCmd.Close doesn't work either; I get Runtime error 2501 (The Close action was cancelled).
How can I rename this report after it's closed?
Are you saying that each time someone changes the settings and opens a report, you want to save that as a new report in Access?
I wouldn't recommend this.
If the dynamically changed stuff are just things like filter and query, why not always use the same report and set the RecordSource dynamically?
EDIT:
Okay, now I understand what you actually want to do.
You can set the Caption property of the report at runtime in code:
Private Sub Report_Open(Cancel As Integer)
Me.Caption = "Incidents By Assignee"
End Sub
You can also pass the text for the caption from your main form to the report:
Pass the text from the form in the OpenArgs parameter when opening the report:
DoCmd.OpenReport "YourReport", acViewNormal, , , , "Incidents By Assignee"
...and in the report, just set the Caption to OpenArgs if it's not empty:
Private Sub Report_Open(Cancel As Integer)
If Nz(Me.OpenArgs) > "" Then
Me.Caption = Me.OpenArgs
End If
End Sub