Developer FAQs - Forms

This page contains several FAQs on using Forms effectively in Access.
The Code segments below can be selected in your browser and copied into an Access VBA module and run. You are free to use any code on this page for personal non-distributed use.


What’s the fastest way to Open an unbound form and populate its controls with information, such as in a property sheet?

How do I know if I am on a New Record?

What is the best way to the RecordCount for a bound form?

How can I tell if a form is loaded?

How do I prevent my form from moving to the next record when the user presses the Tab or Enter keys?

In a multipage form, how can I stop users from using the PgUp and PgDn keys to navigate?

How can I open a form using Visual Basic for Application Language?

 

What's the fastest way to Open an unbound form and populate its controls with information, such as in a property sheet?
Keywords: Open Unbound Form
Posted Apr 24, 1997

You can create custom properties for a form using the Let and Get statements and then open the form using the New keyword. Here are the steps to follow:

  1. First create a form and save it as "MyForm"
  2. Create a textbox on the form and call it "MyTextBox"
  3. In the "MyForm" module, add the following lines:
    Property Let MyName(szName As String)
        Me! MyTextBox = szName
    End Property
    
    Property Get MyName() As String
        MyName = Me! MyTextBox
    End Property
    
  4. Lastly, to open the "MyForm" form and fill in the "MyTextBox" textbox with information we use the "New" keyword and set the property:

    In the Declarations section write:
       Option Compare Database
       Option Explicit
    
       Dim mobjForm As New Form_ MyForm
    

    Our subroutine then looks like:

        Sub OpenMyForm()
          mobjForm.MyName = "The Acme Food Company"
          mobjForm.Visible = True
        End Sub
    

How do I know if I am on a New Record?
Keywords: New Record
Posted Apr 24, 1997

A bound form has a NewRecord property that returns either True or False:

Example:

        If Me.NewRecord Then
	   MsgBox "You are on a new record!"
	End If

What is the best way to the RecordCount for a bound form?
Keywords: RecordCount Bound Form
Posted Apr 24, 1997

There are many ways to get the recordcount buy most consume precious resources. One of the most efficient ways is to run a Count(*) query against your recordsource. Access has been optimized to run this type of query; the results are almost instantaneous.

Your alternative is opening a recordset or recordsetclone and performing a MoveLast and then retrieve the RecordCount property. This entails moving through the entire recordset!

How can I tell if a form is loaded?
Keywords: Load Form
Posted Apr 24, 1997

Use the SysCmd() function to create your own function which accepts the form’s name:

        Function IsOpen(szName as string)
	  IsOpen=(SysCmd(acSysCmdGetObjectState, acForm, szName) <> 0)
	End Function

This function will return True if the form is open and False if it is closed.

How do I prevent my form from moving to the next record when the user presses the Tab or Enter keys?
Keywords: Record Tab Enter Keys
Posted Apr 24, 1997

In previous versions of Access, as you moved past the last field on a bound form, Access automatically moved to the next row of data. Access forms now have a "Cycle" property, which can restrict users to the current record when navigating through the controls on a form.

In a multipage form, how can I stop users from using the PgUp and PgDn keys to navigate?
Keywords: KeyPreview Property PageUp PageDown
Posted Apr 24, 1997

In the form’s KeyPreview property use the following event procedure:

  Private Sub Form_KeyDown (KeyCode As Integer, Shift As Integer)
    If KeyCode=vbKeyPageDown Or KeyCode=vbKeyPageUp Then
	KeyCode=0
    End If
  End Sub

By setting the KeyCode to 0, you’re canceling the keystroke.

How can I open a form using Visual Basic for Application Language?
Keywords: VBA Language Form
Posted Apr 24, 1997

There are four ways to create the default instance of a form (open it). You can open an existing form by using the user interface, by executing the OpenForm method of the DoCmd object, by calling the CreateForm function and switching the new form into Form view, or by using Visual Basic to create a variable of type Form to refer to the default instance. The following example opens an Employees form and points a Form object variable to it:

Dim frm As Form
DoCmd.OpenForm "Employees"
Set frm = Forms!Employees

Microsoft Access also provides a shortcut that enables you to open a form and refer to a method or property of that form or one of its controls in one step. You refer to the form's class module as shown in the following example:

Form_Employees.Visible = True
Form_Employees.Caption = "New Employees"

When you run this code, Microsoft Access opens the Employees form in Form view if it's not already open and sets the form's caption to "New Employees." The form isn't visible until you explicitly set its Visible property to True (–1). When the procedure that calls this code has finished executing, this instance of the form is destroyed ¾ that is, the form is closed.

To create new, nondefault instances of a form's class from Visual Basic, declare a variable for which the type is the name of the form class module. You must include the New keyword in the variable declaration. For example, the following code creates a new instance of the Employees form and assigns it to a variable of type Form:

Dim frm As New Form_Employees

This nondefault instance of the form isn't visible until you explicitly set its Visible property.

Another way to open an instance of the form is as follows:

Dim frm as Form
Set frm= Form_Employees

 

Back to Top


Back Back to the FAQ Table of Contents


  Random Thoughts...
Hit any user to continue.


Copyright© 1996-1999, Baarns Consulting Group, Inc. - All rights reserved.