Creating a Custom Form to Work Like a Msgbox

  • Thread starter Thread starter Jeff Mason
  • Start date Start date
J

Jeff Mason

Hi,

I'm trying to create a form that acts like a msgbox; that is, during
execution of VBA, the form should pop up, get user inputs, and then continue
program execution.

My problem is that I can't get the form to have the focus. The form has the
pop-up and modal properties set, and a dialog box border style. I load the
form using docmd.openform. This works correctly. I set a few form
parameters. This too works correctly. Then I try to give the form the focus
using the setfocus statement. The form flashes briefly, then the calling
program just continues to the next statement after the setfocus statement.
Code in the form's OnGotFocus event is not executed.

I'm running Access 2000 on XP.

I'm stumped. Any ideas? Help would be much appreciated.

Jeff
 
Of course, if code opens a form, it does NOT wait, the calling code
continues to run. Thus, what we need is the call code (the docmd.Openform)
to halt, and wait while the user selects some value from the prompt form.
When done, then your calling code can examine the form' values.

Ok, here is how you do it. You open the form in acDialog mode. This will
halt the calling code, and wait. The user then enters some data into fields,
and the hits the ok button. The trick here is that the ok button DOES NOT
close the form, but sets the forms visible property to false. The will cause
the form to drop out of acDialog mode, and then the calling code will
continue!. If the user does in fact hit cancel, or closes the form, you
consider that as a CANCEL!! (ie: the cancel, or even the "X" in the upper
right corner is thus considered a close/cancel).

Hence, the code looks as

' lets open a prompt form
strF = "frmGetComboName"
docmd.OpenForm strF,acNormal,,,,acDialog

' the above code opens our form "frmGetComboName", and the waits until the
user
enters some data, and then hits ok. The code behind the ok button in the
GetName form does NOT close the form, but does

me.Visiable = false

' at this point, either the user hit ok, or closed the form. If the form is
still open, then user did not cancel, and we assume he hit OK

if isloaded(strF) = true then
' code goes here to example values
strName = forms(strF)!ThenameField
strSex = fomrs(strF)!GenderField
' ok, got our data...lets close the form
docmd.Close acForm,strF
else
' if the form is not open, then user hit the "x", or the cancel
' button on the GetName form. Note that the cancel button on
' the GetName from simply does a docmd.close

msgbox "user canceled"
end if

You also need the code for isloaded. It can be found in tons of examples,
and also in the northwind traders. It is reproduced below just in case
you don't have it:

You can also thus wrap the whole acDialog form code in a function that
returns a value if you want. (I do this for date calendar prompts, and
you thus get re-usable form that can be used anywhere by a simple function).


Function fIsLoaded(ByVal strFormName As String) As Integer
'Returns a 0 if form is not open or a -1 if Open
If SysCmd(acSysCmdGetObjectState, acForm, strFormName) <> 0 Then
If Forms(strFormName).CurrentView <> 0 Then
fIsLoaded = True
End If
End If
End Function
 
Back
Top