I have a form with a button that opens another form in dialog mode. Is there a dialog form property that contains information about the form which opened it? Something like Me.Parrent in sobform? The only way I know to achieve that is to use OpenArgs, but hoping for something more strict...
Thanks for any suggestions
Smok.
As long as you're not opening multiple variants of the same form, the Forms collection is the usual way to do it, like Gustav said.
You can access the form object like this:
Forms!ParentformName.Form
There are loads of other ways to refer to variables, June7 noted the most common ones. I prefer a global variable in the subform object
On the subfrom:
Public Parentform As Form
On the main form:
Private Sub OpenForm
DoCmd.OpenForm "myForm"
Forms!myForm.Parentform = Me.Form
End Sub
Or, to open a dialog form that's dependent on the main form:
Private dialogForm As Form
Private Sub OpenForm
Set dialogForm = New Form_myForm
dialogForm.Parentform = Me.Form
dialogForm.SetFocus
End Sub
Note that the last approach has several advantages, such as supporting multiple instances of the dialog form, and being able to do multiple things from the subform (especially if you change the Private dialogForm As Form to Private WithEvents dialogForm As Form which allows you to detect when the dialog box closes, get values entered in the dialog box, handle them on the main form, and more. It does require that the dialogForm has a form module.)
Related
Im pretty new to programming for MS Access and Visual basic. I'm trying to create a form that will change what the subform is based on a toggled option press.
I have attached pictures of the simple form I'm trying to do and gotten some code off tutorial sites that have shown an example of how to do it but I'm not sure if I should have the subforms pre-created or make the button load the form when clicked.
Option Compare Database
Private Sub Option0_Click()
End Sub
Private Sub Toggle11_GotFocus()
Form1!Testform1.SetFocus
End Sub
Private Sub Toggle12_GotFocus()
Form1!Testform2.SetFocus
End Sub
I know this code isn't all that's needed but I can't seem to find a good place to start to find out what needs to be done with this.
Newer versions of Access seem to hide subform tabs when you open a form, but there is a tab stop property for the childforms. I set the property for that to yes and then for one of the child forms to set the tab index to 0 and the other to 1. You can toggle between the forms using Ctrl Tab when you do this. If the Child Form tabs are not visible when you open the main form, Ctrl Tab will also make them visible allowing you to click on the tabs.
I am simply attempting to switch focus to a split form using a button located on another form. This can be broken down to code a simple as this.
Private Sub Command0_Click()
Forms.Form1.SetFocus
End Sub
This code will work so long as Form1 is in open "Single Form" view. The moment that you change Form1 to "Split Form" view, it no longer changes to Form1 and no error messages pop up.
How can I accomplish setting focus to a split form since obviously the normal way doesn't appear to work.
Edit: I would like to avoid the docmd.Runcommand OpenForm solution or the toggling visible false then true method of solving this problem.
just use docmd.openform frm_name,acNormal will open the form, if already opened will bring it to front (modal forms still stay on top)
I'm creating forms with VBA/Access to access my database.
In a form, I have a *lst_sinistres* listbox that displays the results of my SQL query and when I doubleclick on one of the results it opens me another form with thanks to this code
Private Sub lst_sinistres_DblClick(Cancel As Integer)
DoCmd.OpenForm "F_SINISTRE_MRH", acNormal, , , , , Me.lst_sinistres.Value
End Sub
I wanted to change my form, and add tabs to make it more ergonomic. So I placed my *lst_sinistres* listbox inside a tab.
The problem is that when I doubleclick on one of the results in this listbox (now placed in the tab), the form *F_SINISTRE_MRH* does not open.
Does someone have an idea of where the problem might come?
Thank you
A quirk of VBA control events is that event code can become detached from the control object. Things that cause this tend to be re-naming controls and copy/pasting similar code between controls. To move your listbox onto a tab control you needed to cut and paste it temporarily. That broke the link between the written code and the object name. When the code and object are properly linked, [Event Procedure] shows up in the property sheet (as suggested by #4dmonster).
If you are in the VBA editor, choosing Debug->Compile will search through all the code and re-link event code with like-named controls. This step is worth a try before re-writing because you may end up with orphan blocks of
Private Sub OldControlName_DblClick(Cancel As Integer)
MsgBox "Why don't I work anymore?"
End Sub
that are treated as Form-level subroutines that just happen to never be called.
pT
I know in the .Net framework you can map any controls events to a singular method, as if you wanted to map a column of buttons in a GridView to one method, you could.
In VBA i have since forgotten this information. Could someone help me out in how i can map a Forms' Control(s) to one method in the Forms Code-Behind?
Current OnClick Events, to be consolidated
Private Sub btnSubmit_Click()
CloseForm Me
End Sub
Private Sub btnCancel_Click()
CloseForm Me
End Sub
There are at least two of these methods per form. On some there are 3 or 4 for various Events but they all have the same body, one line pointing to a global method.
It depends on what you want to do. You can set up the form so that it receives the click events and check the screen.active control or you can set the event line on the property sheet for each control to a function. Frequently, people who are not familiar with the ease of MS Access go a long way around, which is why it is useful to say what you wish to do, there is often an easier way and often a way that does not involve code at all.
On the click line of the property sheet for the control:
=Menu_Or_Exit([Name],"cmdClose")
of course you could pass Me as form, but I have found [Name] to be very useful in a number of ways.
An excerpt from the function:
Case "cmdClose"
DoCmd.Close acForm, FormName
CheckMainMenu
You can keep a menu form, a menu subform, or just copy the button from form to form.
I have a form in MS Access which has an image. The image has an Click event which opens a modal form. The modal form has an OK and Cancel button. When you click the OK button, an event is supposed to fire which tells the main form which button was clicked. (This is to simulate the DialogResult functionality in C#). However, the code in the event handler never runs.
The modal form has the following in the general declarations:
Public Event OnDialogBoxClose(NewRecordID As Long, DialogResult As DialogResults)
and the following code where the OK button is clicked:
RaiseEvent OnDialogBoxClose(NewHardwareBaseItemID, dlgresBtnOKClicked)
The main form has the following in the general declarations:
Dim WithEvents RespondQuickAddClose As Form_qckfrmHardwareBaseItemCreate
and the following event handler:
Private Sub RespondQuickAddClose_OnDialogBoxClose(NewRecordID As Long, DialogResult As DialogResults)
MsgBox "Responding to closing of the dialog box" 'Never happens
Me.Requery
End Sub
Can someone explain why the event handler is never called?
Thanks!
Background:
The purpose of all this is to allow a modal dialog box to add an entry, then return the ID of the entry back to the main form to set the value of controls. For instance, imagine you are filling out an insurance form, and you need to select a brand of car this is not there. You click on an icon which pops up with the modal dialog box to allow you to add the car brand. Then when you click OK, it takes you back to the insurance form and selects the brand of car you just created.
This follows an example I found here:
http://database.itags.org/ms-access-database/80292/
You're making your life way too complicated by applying concepts from a different development environment to Access VBA. While VBA does support WithEvents/RaiseEvent, there's no reason to get that complicated here.
The usual way to work with dialogs in Access is that instead of closing them, you hide them. This allows the code after the form was open to run while leaving the values in the form available for use in that code.
Sample code in the OnOpen event of a report that opens a form for collecting values to filter the report:
Private Sub Report_Open(Cancel As Integer)
DoCmd.OpenForm "dlgDateRange", , , , , acDialog, "ThisYear"
If IsLoaded("dlgDateRange") Then
With Forms!dlgDateRange
If .Tag = "Cancel" Then
Cancel = True
Else
Me.Filter = "[InvoiceDate] Between #" & !txtStart & "# AND #" & !txtEnd & "#"
Me.FilterOn = True
Me!lblDateRange.Caption = StrConv(Trim(("from " + varZLStoNull(Format(!txtStart, "mm/dd/yyyy"))) _
& (" to " + varZLStoNull(Format(!txtEnd, "mm/dd/yyyy")))), vbProperCase)
End If
End With
DoCmd.Close acForm, "dlgDateRange"
End If
End Sub
The dialog form has two command buttons, CONTINUE>> and CANCEL. The CANCEL button sets the form's tag to "Cancel" and sets the form's .Visible property to False. The CONTINUE>> button does nothing but set the form's .Visible property to False. Clicking either of those buttons allows the code to continue on the line after the form is open with the acDialog switch.
My philosophy is to make the dialogs as stupid as possible. The calling code has to know what it's looking for in the forms (i.e., you need to know the names of the controls you're reading data out of), but that could be gotten around by adding customer properties to the form. But then you have to know the property names, so you've just moved the ball.
I have also implemented this kind of thing by wrapping the dailog form in a class module, and then the calling context simply initializes an instance of the class and then pulls the values out of it at the appropriate time. But that's actually more complicated that the approach above.
well I don't agree to
"While VBA does support WithEvents/RaiseEvent, there's no reason to
get that complicated here."
I have worked on various VB6 and VBA project. Recently I coded VBA in excel where I raised an event from winform. Few things to be considered when doing so.
If you are calling non-modal winform in VBA with
withevents/raiseevent. It should work normally as expected. No major
workaround is needed
If you are calling modal winform in VBA. Withevents/raiseevents may
not function as per requirement. A quick workaround is to transfer data using public variables declared in the module file.
You will need to use the workaround and I believe it will work absolutely fine.