Open form as modal

  • Thread starter Thread starter Craig Buchanan
  • Start date Start date
C

Craig Buchanan

I would like to create a logon form that act like the common dialog form.
The logon form should open as modal, wait for input, then pass data back to
the calling code. moreover, the calling code should wait for the logon form
to close before continuing. i would like to use the New sytax (more object
oriented) if possible. I've defined the logon form as modal and popup. for
some reason, the calling code (Command1_Click) doesn't wait for the logon
form.

What am i missing?

Thanks.

<code>
Sub Command1_Click

Dim UID,PWD
....
Dim frm As New Form_frmAuthenticate
With frm
.DialogTitle = "Login"

'code should wait here for user to enter infomation in
frmAuthenticate, then click OK
.ShowOpen

'exit if form is cancelled
If .Cancelled Then Goto MemberExit

'gather relevant properties
UID=.UserId
PWD=.Password

End With

MemberExit:
Set frm = Nothing
....

End Sub
</code>
 
Craig,

Did you set the modal property of the form to yes?

Try using the docmd.openform method and set the windowmode to acdialog.
Then the code will wait for the form to close.
Like this:

DoCmd.OpenForm stDocName,,,,,acDialog

HTH,
Josh
 
Have you looked at DoCmd.OpenForm? Try that, and you can tell it to
open as modal (just look at the parameters.) I do this for my login
form, and it works fine. If you want to pass it parameters you might
want to put them in the OpenArgs (last param?) and parse them inside
the Form_Open event.

- Andrew
 
Have you looked at DoCmd.OpenForm? Try that, and you can tell it to
open as modal (just look at the parameters.) I do this for my login
form, and it works fine. If you want to pass it parameters you might
want to put them in the OpenArgs (last param?) and parse them inside
the Form_Open event.

- Andrew
 
i did set the modal property to true. it didn't have an effect.

i'd prefer to use the New syntax, as it allows me to set the form's
properties more easily.
 
i am aware of this option, but i'd prefer to use the New syntax, as it
allows me to set the form's properties more easily.
 
Craig said:
i am aware of this option, but i'd prefer to use the New syntax, as it
allows me to set the form's properties more easily.

While you might prefer it you cannot if you want your calling code to pause.
Making the form modal does NOT pause the calling code. Only using the acDialog
argument of the OpenForm method does.
 
I found a solution.

<frmAuthenticate>
<setttings>
Border Style = Dialog
Modal = True
Popup = True
</setttings>
<code>
Public Event Cancelled()
Public Event Finished()

Private Sub cmdCancel_Click()
RaiseEvent Cancelled
DoCmd.Close
End Sub

Private Sub cmdFinish_Click()
RaiseEvent Finished
DoCmd.Close
End Sub
</code>
</frmAuthenticate>

<calling form>
<code>
Option Explicit

Private WithEvents Authentication As Form_frmAuthenticate

Private Sub Authentication_Cancelled()
MsgBox "cancelled"
End Sub

Private Sub Authentication_Finished()
MsgBox "finished"
End Sub

Private Sub cmdPopup_Click()
Set Authentication = New Form_frmAuthenticate
Authentication.Visible = True
End Sub

Private Sub Form_Unload(Cancel As Integer)
Set Authentication = Nothing
End Sub
</code>
</calling form>
 
Thanks for posting that solution! I think I may take a look into using
something like it now.

- Andrew Backer
 
I've been reading this thread with great interest as I have exactly the same
problem with returning values from a dialog form. Using properties of the
form and the New syntax seems the elegant way to do it.

Craig, I've used your approach it works very nicely. Cheers.

Andrew, when using Docmd.Openform stDocName,,,,acDialog

How do you return back the selections made by the user. Do you need to use
Global variables to do so with that approach?

I assume there is no way to use the New syntax to create a form and use
Docmd.Openform to operate on that form. Is that true?

- Jeremy
 
I passed back values through the Finished event.

I like to see how you've implemented the code. Why not post the relevant
parts?

Craig
 
In this case I needed a MsgBox with non standard buttons. So I created a form
with 2 configurable buttons. That form returns the caption of the clicked
button. Because Im not calling this from another form I need to manually
halt execution in a loop until a button is clicked. Here's the relevant code.

<frmMsgBox>
<setttings>
Border Style = Dialog
Modal = True
Popup = True
</setttings>
<code>
Public Event Finished()

Private strMsgReturn As String

'Readonly property returns caption of the clicked button
Public Property Get MsgReturn() As String
MsgReturn = strMsgReturn
End Property

Private Sub b1_Click()
ReturnButton (b1.Caption)
End Sub

Private Sub b2_Click()
ReturnButton (b2.Caption)
End Sub

Private Sub ReturnButton(PbtnText As String)
strMsgReturn = PbtnText
RaiseEvent Finished
DoCmd.Close
End Sub
<\code>
<\frmMsgBox>

<Calling class module MyMsgBox>
<code>
Private WithEvents frmMyMsgBox As Form_MyMsgBox
Private blnDone As Boolean

Private Sub frmMyMsgBox_Finished()
blnDone = True
End Sub

Public Function ShowMsg(Pmsg As String, Ptitle As String, _
Pbutton1Text As String, Optional Pbutton2Text As
String ) _
As String


On Error GoTo Err_ShowMsg

ShowMsg = ""

Set frmMyMsgBox = New Form_MyMsgBox
frmMyMsgBox.Msg = Pmsg
frmMyMsgBox.Caption = Ptitle
frmMyMsgBox.Button1 = Pbutton1Text
If Not IsMissing(Pbutton2Text) Then
frmMyMsgBox.Button2 = Pbutton2Text
End If
frmMyMsgBox.Visible = True

'Showing a modal pop-up doesn't halt the code so wait for Finish event
of the form
blnDone = False
Do While Not blnDone
DoEvents ' Yield to other processes.
Loop
'Return the text of the clicked button
ShowMsg = frmMyMsgBox.MsgReturn
Set frmMyMsgBox = Nothing

Exit_ShowMsg:
Exit Function

Err_ShowMsg:
MsgBox Err.Description
Resume Exit_ShowMsg

End Function
<\code>
<\calling class module MyMsgBox>

- Jeremy
 

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

Back
Top