By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,660 Members | 1,219 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,660 IT Pros & Developers. It's quick & easy.

Pass a form reference to a form...

P: 20
This is a complicated one...

I have a modal form that lets the user preview some text, then either click Submit or Close. This form is called from several other forms.

What I would like, since Access can't return a value from a form, is to set (from the modal form) a hidden checkbox on the calling form, which would show which button the user pressed on the modal form.

My problem is this -- how can I pass the name of the calling form (which may be a subform) to the modal form, so I can set the checkbox on the calling form.

I've thought to just pass Me.Name, and then in the modal form, using Forms(Me.OpenArgs).chkRetval... but when calling from a subform, that fails. I've also considered setting a flag on the modal form, hiding it, reading the flag from the calling form and then closing the modal form, but that seems messy, so it's a last resort.

Any thoughts?

Thanks
Aug 29 '07 #1
Share this Question
Share on Google+
11 Replies


JKing
Expert 100+
P: 1,206
I'm a little confused as why you would need to capture which button has been clicked? Why not just add the relevant code to the onclick of the button rather than perform the actions from another forms module?
Aug 29 '07 #2

P: 20
I'm a little confused as why you would need to capture which button has been clicked? Why not just add the relevant code to the onclick of the button rather than perform the actions from another forms module?
This form allows the user to preview some text before writing it out to another database. I need to know if the user published the text or closed the form without publishing. Based on that, the calling form will either update a "published date" or not. It's a lot cleaner if the calling form takes care of writing that date, instead of the modal form because the published date will not always be in the same table.
Aug 29 '07 #3

JKing
Expert 100+
P: 1,206
What about a message box with a yes or no button. You can capture which button is pressed and act accordingly.
Aug 29 '07 #4

P: 20
What about a message box with a yes or no button. You can capture which button is pressed and act accordingly.
The text will typically be too much for a msgbox. Plus, the user wants to be able to edit the text in the preview window.. which means it isn't actually a preview anymore, but the changes made won't be reflected in the local database -- just pushed to the sql server.

It's a complicated thing.
Aug 29 '07 #5

JKing
Expert 100+
P: 1,206
How about creating a global string variable. On submit click the "preview"text is stored in the string on close click the string is set to "".

Then the other form can check the string if it's null do nothing, if its not null send the text to the appropriate location.
Aug 29 '07 #6

P: 20
I try to avoid global vars at all costs in Access.. I hate them almost as much as Goto's.. hehe

I think I'll just go with my Plan B until a better idea comes along.. on clicking either button, set a hidden flag on the modal form, hide the form, read the flag from the calling form and then close the modal form.

Thanks for the replies.
Aug 29 '07 #7

Denburt
Expert 100+
P: 1,356
If I understand correctly I have utilized something similar in the past but i use the tag. When you Open the Modal dialog box insert the calling forms name into the tag. Now you know what form originally called the modal form. When you close the modal form call the modal forms tag like so.

Open the modal form:
Docmd.openform "ModalBtch"
Forms!ModalBtch.Tag = me.name

Then on closing the modal form you specify the button clicked and assign it to the calling forms tag like so.

Forms(me.tag).tag = "Print"
Aug 29 '07 #8

P: 20
If I understand correctly I have utilized something similar in the past but i use the tag. When you Open the Modal dialog box insert the calling forms name into the tag. Now you know what form originally called the modal form. When you close the modal form call the modal forms tag like so.

Open the modal form:
Docmd.openform "ModalBtch"
Forms!ModalBtch.Tag = me.name

Then on closing the modal form you specify the button clicked and assign it to the calling forms tag like so.

Forms(me.tag).tag = "Print"
Thanks for the thought, but (as far as I can figure), this wouldn't work with subforms.. Unless I set the parent form's Tag property, but then I'd need to pass Me.Parent.Name.. this is getting messy.
Aug 29 '07 #9

FishVal
Expert 2.5K+
P: 2,653
Thanks for the thought, but (as far as I can figure), this wouldn't work with subforms.. Unless I set the parent form's Tag property, but then I'd need to pass Me.Parent.Name.. this is getting messy.
Hi, there.

This simple code will help you to get reference to Form object by name no matter it was openes as main form or as subform with any possible level of nesting.

Expand|Select|Wrap|Line Numbers
  1. Public Function GetForm(ByVal strFormName As String) As Access.Form
  2.  
  3.     For Each frm In Forms
  4.         If frm.Name = strFormName Then
  5.             Set GetForm = frm
  6.             Exit Function
  7.         Else
  8.             Set GetForm = ScanFormControls(strFormName, frm.Controls)
  9.             If Not (GetForm Is Nothing) Then Exit Function
  10.         End If
  11.     Next
  12.  
  13. End Function
  14.  
  15. Private Function ScanFormControls(ByVal strFormToFind As String, _
  16.                                   ByRef frmControlsToScan As Controls) As Access.Form
  17.  
  18.     On Error GoTo NextIteration
  19.     For Each ctrl In frmControlsToScan
  20.         If TypeName(ctrl) = "SubForm" Then
  21.             If ctrl.Form.Name = strFormToFind Then
  22.                 Set ScanFormControls = ctrl.Form
  23.                 Exit Function
  24.             Else
  25.                 Set ScanFormControls = _
  26.                     ScanFormControls(strFormToFind, ctrl.Form.Controls)
  27.                 If Not (ScanFormControls Is Nothing) Then Exit Function
  28.             End If
  29.         End If
  30. NextIteration:
  31.     Next
  32.  
  33. End Function
  34.  
Aug 29 '07 #10

FishVal
Expert 2.5K+
P: 2,653
BTW another interesting solution.

Let us say you have form "Main" from which you open form "Popup" with two buttons.

Form_Main module:
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Dim WithEvents frmForm As Form_Popup
  3.  
  4. Private Sub Command0_Click()
  5.     DoCmd.OpenForm "Popup"
  6.     Set frmForm = Forms!PopUp
  7. End Sub
  8.  
  9. Private Sub Form_Close()
  10.     Set frmForm = Nothing
  11. End Sub
  12.  
  13. Private Sub frmForm_Button1Clicked()
  14.     MsgBox "Button1 clicked"
  15. End Sub
  16.  
  17. Private Sub frmForm_Button2Clicked()
  18.     MsgBox "Button2 clicked"
  19. End Sub
  20.  
Form_Popup module:
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Event Button1Clicked()
  3. Event Button2Clicked()
  4.  
  5. Private Sub btnButton1_Click()
  6.     RaiseEvent Button1Clicked
  7. End Sub
  8.  
  9. Private Sub btnButton2_Click()
  10.     RaiseEvent Button2Clicked
  11. End Sub
  12.  
I hope this nice solution makes a sense.
Aug 29 '07 #11

P: 20
Wow -- Go Go FishVal !

First off, the functions to return a form ref are awesome.. thank you.

And I didn't even know about declaring Events.. so thanks and kudos all-round.
Aug 31 '07 #12

Post your reply

Sign in to post your reply or Sign up for a free account.