Exchange data between forms

sambo

Registered User.
Local time
Today, 05:51
Joined
Aug 29, 2002
Messages
289
I would like to popup a form after a button is clicked. I would like the button click event code to wait until the modal form is closed before continuing. The code will be reliant on the values returned from the modal form. This is a simple task in c++, but I can't seem to find a way to do it in VBA.

psuedo..
Code:
Sub OnClick
  Dim frm as MyForm
  Set frm = New MyForm
  integer nRet = frm.DoModal  ' -->code should stop executing here and wait
  if nRet = IDOK 'The user clicked OK on the modal form
    'Do the data exchange
    m_strMainFormSerial = frm.m_strModalFormSerial
  elseif nRet = IDCANCEL 'The user clicked CANCEL on the modal form
    Set frm = Nothing
    Exit Sub  
  endif
  'Do some other stuff
  Set frm = Nothing
End Sub
1. Is this type of thing even possible in VBA?
2. How do I stop exectution and wait for a value back from DoModal?
3. Is there anything equivalent to DoModal in VBA?
4. Any other way to do all of this?
 
It's simple in VBA too. The easiest way is to use the prebuilt message boxes. Here's a sample:
Code:
  Dim msg, button, title, Response
  msg = "Are you sure you want to cancel call?"
  button = vbYesNo + vbDefaultButton2
  title = "Confirm Cancel!"

  Response = MsgBox(msg, button, title)
  If Response = vbYes Then
    'what to do if user replied yes
   Else  
    'what to do if they didn't
  End If
 
Hmmm…

Link to possible solution, here.

In VBA, as far as I know, a Form object does not have a return type.
In order to return a variable from a Form, one needs to do one of three things…

1. Write the return value/s to (a) public variable/s. (Not recommended.)
2. Write back to the calling Form and close. (Preferred by me but who knows, or indeed cares?)
3. Stay open and let the calling Form read them and then close the called Form.

Code execution diversion is preserved with opening the called Form with the argument of acDialog.

Passing Me.Name passes the name of the Form that opened the called Form. Using Me.OpenArgs in the called Form allows writing back the variable/s to the calling Form (Calling Form must be open and variables must be public.)

Would be nice if Micro$oft allowed passing pointers in the OpenArgs argument.

PS.
I forgot to say…
4. You can throw a little more code at the problem… here.


Regards,
Chris.
 
Last edited:
ChrisO said:
Passing Me.Name passes the name of the Form that opened the called Form. Using Me.OpenArgs in the called Form allows writing back the variable/s to the calling Form (Calling Form must be open and variables must be public.)

Would be nice if Micro$oft allowed passing pointers in the OpenArgs argument.
So you're telling me that I can't pass a pointer as an OpenArg to a form??? So basically I am forced to use a global variable to share between the two? Please clarify if I am misreading your post. If I read it correctly, then that certainly is shocking that there is no sure fire way to share pointers between 2 forms (except by global variable).
 
If you are going to open a form and stop the code you need to specify the acDialog constant.

i.e.

Code:
DoCmd.OpenForm "MyForm", , , , , acDialog

There's no pointers. The solutions?

  • global variables :(
  • sending the value you need to the Tag property of a control on the other forum
    Code:
    Forms("MyForm").MyLabel.Tag = myValue
 
Mile-O-Phile said:
  • global variables :(
  • sending the value you need to the Tag property of a control on the other forum
    Code:
    Forms("MyForm").MyLabel.Tag = myValue
Tricky tricky tricky...
I shall try.
 
Right on Mile! Here's a reference for future viewers. This basically amounts to a souped-up inputbox.
Code:
    'clear tags
    Me.FRNT_Serial.Tag = ""
    Me.FRNT_Part.Tag = ""
    'open and wait
    DoCmd.OpenForm "NEW_CASE", , , , , acDialog
    
    If Me.FRNT_Serial.Tag = "" Or Me.FRNT_Part.Tag = "" Then Exit Sub 'user canceled
    DoCmd.GoToRecord , , acNewRec
    Me!FRNT_Date = Now()
    Me!FRNT_Serial = Me.FRNT_Serial.Tag 'assigned from modal dialog
    Me!FRNT_Part = Me.FRNT_Part.Tag 'assigned from modal dialog
 

Users who are viewing this thread

Back
Top Bottom