Access 2007 VBA - Rename report upon closing it? - vba

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

Related

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.

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

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

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

Programming VBA in an Outlook form

I created my own Outlook form to use it as standard surface to enter certain orders instead of the normal message form. The creation, editing and sending works perfectly fine and in the next step I want to insert some code via VBA.
My problem is that I can´t access the objects of my form in the VBA editor. E.g. I want to show a message box when a certain checkbox is checked. According code would be:
Sub example()
If CheckBox1.Value = True Then
MsgBox("Checkbox 1 is checked.")
End If
End Sub
When I run the code I get the error that the object could not be found. The same goes for every other object, like textboxes or labels etc.
I guess the solution is pretty simple, like putting Item. or sth. like that in front of each object. But so far I wasn't able to find the solution.
I´m using Outlook 2010.
I know this is a year too late but you'll want to do something like this example below. It's kinda a work around but you can get whatever value was selected.
Sub ComboBox1_Click()
Set objPage = Item.GetInspector.ModifiedFormPages("Message")
Set Control = objPage.Controls("ComboBox1")
MsgBox "The value in the " & Control.Name & _
"control has changed to " & Control.Value & "."
End Sub
You should be able to get the value, just get a handle on the object you want using the Inspector
The following is an excerpt from here
When you use a custom form, Outlook only supports the Click event for
controls. This is a natural choice for buttons but not optimal for
controls like the combo box. You write the code by inserting it into a
form’s VBScript editor. You need to have the Outlook form open in the
Form Designer and click the View Code button found in the Form group
of the Developer tab.
Sub CheckBox1_Click()
msgbox "Hello World"
End Sub
The code page is fairly minimal with no syntax highlighting. I just tried this now and it does work. Dont forget to Publish your form to pick up the new changes.
I know this is almost 6 years late but, in VB and VBA, simply start with the form name. (And if that doesn't work, just keep going up a parent object and you'll get there.) So, your code becomes:
Sub example()
If MYFORMNAME.CheckBox1.Value = True Then
MsgBox("Checkbox 1 is checked.")
End If
End Sub
Of course, after typing "MYFORMNAME." you'll know if it will work because typomatic will kick in when the system recognizes "MYFORMNAME" after you hit the period.