Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Open Form Instance as a Modal Form

Status
Not open for further replies.

Broccoli2

MIS
Mar 26, 2001
161
GB
I am creating form instances in code, using
Code:
Set frm = New Form_frmBase
which creates frm as a hidden form. However, I want to then display this form as a modal form and suspend code until it is hidden or closed. Just doing
Code:
frm.Visible = True
doesn't work, despite
Code:
frm.Modal = True
,
Code:
frm.PopUp = True
and
Code:
frm.BorderStyle = dialog

I can't use
Code:
DoCmd.OpenForm xxx,,,,,acDialog
since frm doesn't have a unique Name (it's an instance). What is the command to perform
Code:
DoCmd.OpenForm
without a form name? (The form instance frm does have a unique window handle - is there some function which utilises this to do the same thing as
Code:
DoCmd.OpenForm
?)

Please help!

Thanks
-Rob
 
Hi Rob,
I have this working quite well:
It's used off of a search form, and I clipped out all the possibles here for space, but in the event "FrmEstimate" is already loaded, call the multiple form drill:

Public FrmCopy As Form

Private Sub List_DblClick(Cancel As Integer)
If IsNull(Me.List.Column(1)) Then Exit Sub
strfind = Me.List.Column(1)
DoCmd.Minimize
If Not IsLoaded("frmestimate") Then
DoCmd.OpenForm "frmestimate", , , "[EstID]=" & strfind & ""
Else
Call MultipleForm(1)
End If

Private Sub MultipleForm(byTypeID As Byte)
On Error GoTo ErrMF
Dim strfind As String, ctl As Control
strfind = Me.List.Column(1)
Select Case byTypeID
Case 1
Set FrmCopy = New Form_frmestimate
DoCmd.ApplyFilter , "[EstID]=" & strfind & ""
Case Else
GoTo ExitMF
End Select
With FrmCopy
.Visible = True
.AllowAdditions = False
.AllowDeletions = False
.AllowEdits = False
.Modal = True
!LblCopy.Visible = True
End With
For Each ctl In FrmCopy
If ctl.ControlType = acCommandButton Or ctl.ControlType = acToggleButton Then ctl.Visible = False
If ctl.ControlType = acSubform Or ctl.ControlType = acComboBox Then ctl.Enabled = False
Next ctl
FrmCopy.ShortcutMenu = False

ExitMF:
Exit Sub

ErrMF:
MsgBox Err.Number & " " & Err.Description, vbInformation, "Multiple form error."
Resume ExitMF
End Sub

I just ran this with the .Modal inserted and all is fine? My forms are always visible though. Perhaps there's some disagreement with visibility and "modality"? Maybe if your form was by default visible, and when opening the original form, visible = false? Ideas?
Gord
ghubbell@total.net
 
Thanks for your reply. Prehaps I should clarify and say that I haven't got a problem getting the form to display - after creating the instance I can just make
Code:
frm.Visible = True
. My problems are:

1.
Making the form Modal (even preparing the form with its Modal property set to true, the form instance opens non-modally)

2.
Suspending code execution until the form instance is closed or hidden

As I said, doing these things with original forms is straightforward using
Code:
DoCmd.OpenForm
with the
Code:
acDialog
argument, but opening any other way and the form opens non-modally and the code just charges on ...

 
Right. well as above I'm getting a normally non modal form modal. Perhaps something in your code is causing the action to step over? Maybe comment the code and run a test just on the modal issue first. If it goes then we could look at what the code is doing...I honestly don't know if you actually can make a modal form invisible. Kind of a contradictory thing anyway, Modal being: give me all the focus, nothing else but, then hide me? More Ideas? Gord
ghubbell@total.net
 
The task I'm trying to achieve involves customising dialog boxes on the fly in code, based on a 'template' form, and I need more than one open at once.

Here's my code:
Code:
Dim frm As Form

Sub sbNewDialog()

    Set frm = New Form_frmDialog
    ' At this point frm is open but invisible
    frm.Modal = True
    frm.Visible = True
    ' Now frm is visible but not modal
    Debug.Print "Code continues!"
    ' ...
End Sub

Thanks for your help

-Hopeful Rob
 
Gettin' to the guessing stage... maybe move Modal to after Visible? Also Hopeful!, Gord
ghubbell@total.net
 
OK, I've found the underlying problem (and a solution). When you're in break mode, Modal forms aren't modal. Please excuse me if this is a blinding flash of the obvious. I've also found a way to suspend the code until the form is closed or hidden - but is this the best way?

Here's my code:
Code:
Sub sbNewDialog()

    Set frm = New Form_frmDialog
    frm.Visible = True
    frm.Modal = True

End Sub

Sub sbWaitForDialog()

    On Error GoTo stopwait
    sbNewDialog
    While frm.Visible
        DoEvents
    Wend
stopwait:
End Sub

Thanks for your help.

-Rob
 
Hey Rob, Looks fine from heren nice and simple, and if it works for you, its perfect! Gord
ghubbell@total.net
 
Oops I spoke too soon. I modified my code to change the source of a subform, but it won't open the subform - do I need to be in design view to make that kind of a change? How do I do that, since I'm dealing with a form instance without a name? aargh

-Rob

Code:
Sub sbNewDialog(ByVal SubFormName As String)

    Set frm = New Form_frmDialog
    frm.Controls("frmSubForm").SourceObject = SubFormName
    frm.Visible = True
    frm.Modal = True

End Sub

Sub sbWaitForDialog(ByVal SubFormName As String)

    On Error GoTo stopwait
    sbNewDialog SubFormName
    While frm.Visible
        DoEvents
    Wend
stopwait:
End Sub
 
You don't working with a "real" form, an instance: no idea(never tried). You could cheat, run 2 subforms on the 1 main, flip their visibility on/off in the new instance? Not real "sano" but it might fly for your needs..... :) Gord
ghubbell@total.net
 
Very wierd. I started a brand new empty database and everything works fine. It seemes to be something to do with the datasources you use for your forms/ODBC linked tables maybe. In any case, for those following the trail, here's the code I'm finally using:

Code:
Sub sbDialog(ByVal SubFormName As String)

    On Error GoTo exitdialog
    Dim frm As Form
    Set frm = New Form_frmDialog
    frm.Controls("frmSubform").SourceObject = SubFormName
    frm.Modal = True
    frm.Visible = True
    While frm.Visible
        DoEvents
    Wend

exitdialog:
End Sub
 
Way to go...I'm seeing this sort of issue more and more with Access: was just over in general writing about "gremlins" that sometimes live in these things. MS may have the exterior quite polished but I guess there's still some work to be done under the hood! Glad to hear and I'll note your code. Take care, Gord
ghubbell@total.net
 
I am trying to something similar, but quite a bit simpler. I want to be able to open multiple instances of the same form. I am using the following code:

Dim frm as Form
Set frm = New Form_frmMyForm
frm.Visible = True

The new form flashes on the screen for a second and then goes away. I have no idea why it's doing this and how to make it stay visible. Any ideas? Thanks!
 
Your code is fine, but you need to store your
Code:
frm
object somewhere, otherwise it gets destroyed at the end of your procedure. Try declaring
Code:
frm
as a public, module level variable, rather than in the procedure.

-Rob
 
The above has answered a question that's been bugging me for weeks!

One minor point - the modal form I've opened has a couple of combo boxes on it. When you click on them - the list appears but nothing happens when you click on an entry. Not a huge problem - you can still enter text but any ideas why this should happen and how to avoid??

Cheers
 
Hmm, not sure about your combo box problem.
- Any event scripts running from them?
- Any input masks/default values?

Otherwise I'm not so sure

-Rob
 
yeah - on update on all of them now you mention it, but no default or input masks. I'll try removing them.

Just out of interest - this while wend loop seems to use up a lot of processor time - the task manager shows access as using 99% of processor time! is that normal (as soon as the form is closed, it drops right down to the normal 0% - 2% )

Is that normal and is that why the combo box isn't working?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top