Open a Form as a Function?

G

Guest

Hello and thanks in advance.

I was wondering if it is possible to open a form as a function. It would be
ideal in a number of situations for me to be able to return a value
(delimited string of a couple of choices on small dialog forms, or true if
all the user information passes validations). OpenArgs going into the form
works good going in, but the only thing I have come up with for the way back
out is global variables, which I am trying to avoid. Any ideas?? Thanks...
 
S

Stuart McCall

Dymondjack said:
Hello and thanks in advance.

I was wondering if it is possible to open a form as a function. It would
be
ideal in a number of situations for me to be able to return a value
(delimited string of a couple of choices on small dialog forms, or true if
all the user information passes validations). OpenArgs going into the
form
works good going in, but the only thing I have come up with for the way
back
out is global variables, which I am trying to avoid. Any ideas??
Thanks...

You don't need to create delimited strings etc. Here's the best way to do
this:

DoCmd.OpenForm "MyForm",WindowMode:=acDialog

On MyForm, place Ok and Cancel buttons. In the OnClick event for the Ok
button, hide the form:

Me.Visible = False

In the OnClick for the Cancel button, close the form:

DoCmd.Close acForm, Me.Name

Now, on the line following the DoCmd.OpenForm, detect which button was
pressed by checking whether MyForm is still open (if Ok was pressed, it will
still be open, you just can't see it). I use this little function:

Public Function FormIsOpen(ByVal FormName$) As Boolean
'Returns True if form formname$ is open in form view

If SysCmd(acSysCmdGetObjectState, acForm, FormName$) Then
If Forms(FormName$).CurrentView Then
FormIsOpen = True
End If
End If

End Function

Paste the function into a standard module.

Because the form is still around, you can grab all the values you want from
it:

DoCmd.OpenForm "MyForm",WindowMode:=acDialog
If FormIsOpen("MyForm") Then
Debug.Print MyControl1.Value
Debug.Print MyControl2.Value
Debug.Print MyControl3.Value
...
DoCmd.Close acForm, "MyForm" 'Don't forget this!
End If

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top