Class: OpenForm in Category Microsoft Visual Basic 6 : Forms from Total Visual SourceBook

Class that open Forms via a string reference in VB6.

Visual Basic does not allow you to open and show a form based on a string or a variable containing the name of the form. This class lets you create a collection of the forms in your application, and then refer to them via strings or variables.

Procedure Name Type Description
(Declarations) Declarations Declarations and private variables for the COpenForm class.
Class_Initialize Initialize Instantiates the module-level collection.
Class_Terminate Terminate Cleans up the form reference collection.
AddForm Method Add a form reference to the collection (this form does not need to be loaded).
GetForm Method Get a form reference to the passed form (this form does not need to be loaded).
ShowForm Method Show the form specified in the passed string argument. This form must have previously been added to the collection with the AddForm method. This function is designed to work like the VB6 .Show method, except that you can show a form by using a string or a variable rather than hard coding the name of the form. You can specify whether the form is to be shown modally, and optionally specify an owner form.
' Example of the COpenForm class
'
' To try this example, do the following:
' 1. Create four forms, named 'form1', 'form2', 'form3' and 'form4'
' 2. Set form1 to be the startup form for the application
' 3. Add a listbox named 'lstForms' to form1
' 4. Add a command button named 'cmdOpen' to form1
' 5. Add a command button named 'cmdGetReference' to form1
' 6. Paste all the code from this example to form1's module
' 7. Run the application and load form1.
' 8. Press the cmdOpen and cmdGetReference buttons with the listbox set at different form choices
'    to see how this class lets you interact with forms via a string reference to the form's name

' Declarations section code
Private mOpenForm As COpenForm

Private Sub Form_Load()
  Set mOpenForm = New COpenForm

  ' Add the available form names to a list box for this example only.
  ' You could supply the form names in any way you like, including variables, prompts from the user, entries in a table and so forth.
  With lstForms
    .AddItem "form2"
    .AddItem "form3"
    .AddItem "form4"
    .ListIndex = 0
  End With

  ' This is the only "hard-coding" you need to do to use the COpenForm class. You need to add every form in your application to this section of code.
  ' If you add a new form to your application, you will need to add a reference to it here. Notice that there are no quotes around the form names.
  ' You are not storing the NAME of the form, but a reference to the CLASS of the form.

  With mOpenForm
      .AddForm Form2
      .AddForm Form3
      .AddForm Form4
  End With

  cmdOpen.Caption = "Open Form"
  cmdGetReference.Caption = "Get Reference"

End Sub

Private Sub cmdOpen_Click()
  ' Open the selected form indicated in the listbox.
  ' The form can optionally be shown modally, or with an open form specified as the 'owner' form
  mOpenForm.ShowForm lstForms
End Sub

Private Sub cmdGetReference_Click()
  ' Get a reference to one of the forms in the application and assign it to a local variable.
  ' Modify a property of the form via this variable:

  Dim frm As Form

  Set frm = mOpenForm.GetForm(lstForms)
  If frm.Visible = False Then
    frm.Visible = True
  End If

  frm.Caption = "Caption updated to: " & Now

End Sub

Total Visual SourceBook The source code in Total Visual Sourcebook includes modules and classes for Microsoft Access, Visual Basic 6 (VB6), and Visual Basic for Applications (VBA) developers. Easily add this professionally written, tested, and documented royalty-free code into your applications to simplify your application development efforts.

Total Visual SourceBook is written for the needs of a developer using a source code library covering the many challenges you face. Countless developers over the years have told us they learned some or much of their development skills and tricks from our code. You can too!

Additional Resources

Total Visual SourceBook CD and Printed Manual

Microsoft Access/ Office 2016, 2013, 2010, and 2007 Version
is Shipping!

New features in Total Visual SourceBook for Access, Office and VB6

Supports Access/Office 2016, 2013, 2010 and 2007, and Visual Basic 6.0!


View all FMS products for Microsoft Access All Our Microsoft Access Products

Reviews

Reader Choice Award for MS Access Source Code Library
Reader Choice

"The code is exactly how I would like to write code and the algorithms used are very efficient and well-documented."

Van T. Dinh, Microsoft MVP

SourceBook Info

Additional Info

Question

 

 

Free Product Catalog from FMS