Module: Forms in Category Microsoft Access : Forms from Total Visual SourceBook

General routines for Microsoft Access forms (Jet and ADP). Includes VBA support for 32 and 64-bit API calls.

Procedure Name Type Description
(Declarations) Declarations Declarations and private variables for the modForms module.
ComboBoxOpen Procedure Open the passed combo box control.
ComboSetFirst Procedure Set the first value in a combo box as the selected value.
DeSelectControl Procedure De-highlights the text in the specified control by placing the cursor at the beginning of the control. This is useful to handle situations where a user tabs into a control and Access highlights the entire control. Call this function after arriving at the control and it will de-highlight the control.
Note: This function works only with textbox and combo box controls.
EnableDisableControls Procedure Enables or disables all controls in the specified section of the specified form. If disabling, make sure the control doesn't have focus. Controls that have focus cannot be disabled. If you call this procedure for a section that contains the control with focus, all controls except that control is disabled.
CurrentFormView Procedure Retrieve the current view of the specified form, if it's open.
GetActiveForm Procedure Get the name of the currently active form.
GetControlType Procedure Get the control type (English name) of a form or report control.
IsFormOpen Procedure Determine if a form is open. Typically, before opening a form, you check to see if that form is open.
IsFormOpenInDatasheetView Procedure Determine if a form is open in datasheet view.
IsFormOpenInDesignView Procedure Determine if a form is open in design view. Access supports certain operations on forms only when they are open in design view. Use this procedure to verify how a form is opened before attempting such operations.
IsFormOpenInFormView Procedure Determine if a form is open in form (normal) view.
IsFormOpenInLayoutView Procedure Determine if a form is open in layout view.
IsFormOpenInMode Procedure Determine if a form is open in a particular view.
FormAddRecord Procedure Adds a new record in the specified form. Many editing forms in Access applications have the need for the user to easily add records. You can accomplish this by using the navigation buttons of the form, or by using the keyboard to move to the last record and opening a new record. However, your application may need to add additional logic to the operation of adding a new record. Use this function to add a new record, handling your special needs before or after calling it.
FormClose Procedure Close the named form without errors. This function is useful when closing a form and that form may not be open. It first checks to see if the object is open, and if it is, closes it.
FormCloseAll Procedure Close all open forms that can be closed.
FormControlPropertiesChange Procedure Open a form in design view, sets the specified property to the specified value on all controls, and optionally saves the form. You can use this function to make mass changes to a form's properties. The fSave parameter indicates whether or not the changes should be changed.
FormControlsToArray Procedure Populate the passed array with control names of a form. The passed array must be 0-based. The procedure will expand the array as needed, so you do not need to pre-allocate array storage before calling the procedure. If the form specified is not open, the procedure opens it in design view, fills the array and then closes the form. If the form specified is already open, this procedure does not re-open or close the form.
FormControlsToString Procedure Populate the passed string with control names of a form. Use the chrDelimit parameter to specify the character or characters to use as the delimiter between control names. The procedure places the resulting string in the strIn parameter.
FormDeleteRecord Procedure Delete the current record from the specified form.
FormMoveFirst Procedure Move to the first record of a form. This is useful when you are implementing your own navigation buttons, and want to duplicate the behavior of the built-in Access navigation buttons.
FormMoveLast Procedure Move to the last record of a form This is useful when you are implementing your own navigation buttons, and want to duplicate the behavior of the built-in Access navigation buttons.
FormMoveNext Procedure Move to the next record of a form This is useful when you are implementing your own navigation buttons, and want to duplicate the behavior of the built-in Access navigation buttons.
FormMovePrevious Procedure Move to the previous record of a form This is useful when you are implementing your own navigation buttons, and want to duplicate the behavior of the built-in Access navigation buttons.
FormMoveSizeInches Procedure Move and/or resizes the named form in units of inches. The form named by the strForm parameter must be open for this procedure to work. To leave a particular sizing/position value unchanged, do not specify a value when calling the procedure. For example, to resize a form, leaving its position intact, specify values for the varWidth and varHeight parameters, and zero-length strings for the varTop and varLeft parameters.
FormMoveSizeTwips Procedure Move and/or resizes the named form in units of Twips (1 twip = 1/1400 inch). This procedure gives a greater amount of accuracy in moving and sizing forms than the related FormMoveSizeInches procedure. The form named by the strForm parameter must be open for this procedure to work. To leave a particular sizing/position value unchanged, do not specify a value when calling the procedure. For example, to resize a form, leaving its position intact, specify values for the varWidth and varHeight parameters, and zero-length strings for the varTop and varLeft parameters.
FormNamesToArray Procedure Loads an array with the names of all the forms in the current database.
FormNamesToString Procedure Populate a string with names of all forms in the current database.
FormOnNewRecord Procedure Determine if the specified form is on a new record by checking the value of the NewRecord property.
FormOpen Procedure Open a form in normal or dialog mode and trap for any errors. If the form is already open, just select it.
FormOpenWait Procedure Open a form in normal or dialog mode, and wait until that form is closed with an option to hide the current form. This is useful when a form opens another form and wants to be invisible until the user closes that form. If the form is already open, just select it.
FormOpenReplace Procedure Open a form with all the options and trap for any errors. If the form is open, close it and open it again (useful if an argument is passed and/or running Open and Load events).
FormPropertyChange Procedure Changes the value of the named property on a form.
FormSaveRecord Procedure Saves the current record on the specified form. This is useful if you want to add a [Save] button to your form, and call this procedure from that button to save the current record.
FormsOpen Procedure Determine if any forms are open. This is useful when your application must perform an operation (such as closing) that requires all forms and reports to be closed. See the CloseObjectsOfType() and CloseAllOpenObjects() functions for ways to close open objects.
FormOpenDesign Procedure Open a form in design mode. If it's already open, change the current view to design view.
FormOpenDesignAll Procedure Open all the forms in design view and if visible, minimizes them. Skips forms that are already open.
FormUndoRecord Procedure Undo the changes to the current record on the specified form. This is useful if you want to put an 'Undo' command button on your form to undo edits to the current record or field.
IsSubForm Procedure Determine if the passed form is open as a form or a subform. This can be useful when your database contains a form that is used in regular view mode as well as a subform. If your application has special logic that is affected by whether or not the form is opened as a subform, use this procedure to detect this case.
ListFillForms Procedure Provides a list fill function for a list/combo box for a list of forms. Use the name of this function as the "RowSourceType" property of a listbox or combo box. Do NOT use an "=" sign or parentheses.
ListFillMacros Procedure Provides a list fill function for a list/combo box for a list of macros. Use the name of this function as the "RowSourceType" property of a listbox or combo box. Do NOT use an "=" sign or parentheses.
ListFillModules Procedure Provides a list fill function for a list/combo box for a list of modules. Use the name of this function as the "RowSourceType" property of a listbox or combo box. Do NOT use an "=" sign or parentheses.
ListFillReports Procedure Provides a list fill function for a list/combo box for a list of reports. Use the name of this function as the "RowSourceType" property of a listbox or combo box. Do NOT use an "=" sign or parentheses.
SelectControl Procedure Selects/de-selects the contents of the specified control. This can be useful when you want your code to select all the text in a particular control. By default Access highlights an entire control's contents when you move into that control. You can use this function to remove the highlight by setting the fNone parameter to true.
WaitUntilFormClose Procedure Wait for the form to close, using the Sleep command rather than DoEvents (which eats up CPU processing resources). This is useful to suspend the execution of code until the user closes a form and the form cannot be opened modally.
Form_SetCommandButtons Procedure Set command button properties on the form, including the use of the hyperlink hand while hovering.
Form_SetFonts Procedure Set the font name and size for label, text box, and tab controls.
Form_SetSectionColors Procedure Set the form's section colors to one consistent color (defaults to the Windows System color).
Form_SetSectionColorsIndividually Procedure Set the each of the form's section colors. Use -2147483643 if you want the System Windows color.
ObjectNamesToArray Procedure Loads an array with names of all of an object type.
' Example of modForms
'
' To try this example, do the following:
' 1. Create a new form in your copy of Sample.mdb (in the Total Visual SourceBook sample directory).
'    Alternately, create a new form in the database of your choice, and import the following objects from Sample.mdb:
'       Table:    'Categories'
'       Form:     'Categories'
' 2. Make the Page and Form Header and Footer sections visible
' 3. Set the RecordSource property to the 'Categories'
' 4. Create the following command buttons
'       'cmdTest'
'       'cmdTestCombo'
'       'cmdSelect'
'       'cmdAdd'
'       'cmdFirst'
'       'cmdLast'
'       'cmdNext'
'       'cmdPrevious'
'       'cmdEnableDisable'
'       'cmdClose'
'       'cmdProps'
'       'cmdDelete'
'       'cmdSave'
'       'cmdUndo'
'       'cmdWait'
'       'cmdOpenCloseForms'
' 5. Create the following list boxes:
'       'lstForms'
'       'lstMacros'
'       'lstModules'
'       'lstReports'
' 6. Create a combo box 'cboTest'
' 7. Create a text box 'txtTest'
' 8. Add another form named 'frmProps' with 2 command buttons and 2 text boxes
' 8. Paste this code into the form's module

#If VBA7 Then
  Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal lngMilliSeconds As Long)
#Else
  Private Declare Sub Sleep Lib "kernel32" (ByVal lngMilliSeconds As Long)
#End If

Private Const mcstrFormName As String = "Categories"

Private Sub cmdOpenCloseForms_Click()
  If MsgBox("This will open all forms in design view, then close them all (including this form). " & vbCrLf & _
            "All forms will be saved on close." & vbCrLf & vbCrLf & "Continue?", vbYesNo) = vbYes Then
    FormOpenDesignAll acWindowNormal
    ' wait 2 seconds
    Sleep 2000
    FormCloseAll (True)
  End If
End Sub

Private Sub Form_Open(Cancel As Integer)

  cmdTest.Caption = "Test Access form functions"
  cmdTestCombo.Caption = "Test Combo"
  cmdSelect.Caption = "Select/Deselect"
  cmdAdd.Caption = "Add"
  cmdDelete.Caption = "Delete"
  cmdUndo.Caption = "Undo"
  cmdSave.Caption = "Save"
  cmdFirst.Caption = "First"
  cmdLast.Caption = "Last"
  cmdNext.Caption = "Next"
  cmdPrevious.Caption = "Prev"
  cmdEnableDisable.Caption = "Enable/Disable"
  cmdClose.Caption = "Close"
  cmdOpenCloseForms.Caption = "Open then Close Forms"
  cmdProps.Caption = "Form property"

  cmdWait.Caption = "Wait for " & mcstrFormName

  lstForms.RowSourceType = "ListFillForms"
  lstMacros.RowSourceType = "ListFillMacros"
  lstModules.RowSourceType = "ListFillModules"
  lstReports.RowSourceType = "ListFillReports"

  cboTest.RowSourceType = "Value List"
  cboTest.RowSource = "Red;Blue;Green;Yellow;Magenta"
  txtTest = "Hello Visual SourceBook Users"

  ' Example for Form_SetCommandButtons
  Form_SetCommandButtons Me, "Tahoma", 10, False

  ' Example for Form_SetFonts
  Form_SetFonts Me, "Tahoma", "10", True

  ' Example for Form_SetSectionColors
  Form_SetSectionColors Me, 15590879

End Sub

Private Sub cmdTest_Click()
  Dim astrControls() As String
  Dim astrForms() As String
  Dim intCount As Integer
  Dim intCounter As Integer
  Dim strControls As String
  Dim strForms As String
  Dim ctl As Control

  ' Example of FormsOpen
  If FormsOpen() Then
    Debug.Print "There are open forms"
  Else
    Debug.Print "There are no open forms"
  End If

  ' Example of FormControlsToArray
  intCount = FormControlsToArray(mcstrFormName, astrControls)
  Debug.Print "Form Controls:"
  For intCounter = 0 To intCount - 1
    Debug.Print intCounter & ": " & astrControls(intCounter)
  Next intCounter

  ' Example of FormControlsToString
  intCount = FormControlsToString(mcstrFormName, ";", strControls)
  Debug.Print "Controls: " & strControls

  ' Example of FormNamesToArray
  intCount = FormNamesToArray(astrForms)
  Debug.Print "Form names:"
  For intCounter = 0 To intCount - 1
    Debug.Print intCounter & ": " & astrForms(intCounter)
  Next intCounter

  ' Example of FormNamesToString
  intCount = FormNamesToString(strForms, ";")
  Debug.Print "Forms: " & strForms

  ' Example of FormMoveSizeInches
  FormMoveSizeInches Me.name, 1, 1, 5, 5

  ' Example of FormMoveSizetwips
  FormMoveSizeTwips Me.name, 1440, 1440, 7200, 7200

  ' Example of FormOnNewRecord
  Debug.Print "Form on new record: " & FormOnNewRecord(Me)

  ' Example of FormOpen
  FormOpen mcstrFormName, False

  ' Example of FormsOpenMinimized
  ' Uncomment the following line to test this function
  ' intCount = FormsOpenMinimized()

  ' Example of GetActiveForm
  Debug.Print "Active form: " & GetActiveForm()

  ' Example of GetControlType
  For Each ctl In Me.Controls
    Debug.Print "Control " & ctl.name & " is a " & GetControlType(ctl)
  Next ctl

  ' Example of IsFormOpen
  If IsFormOpen(mcstrFormName) Then
    Debug.Print mcstrFormName & " is open"
  Else
    Debug.Print mcstrFormName & " is not open"
  End If

  ' Example of CurrentFormView
  Debug.Print "Current form view for " & mcstrFormName & " is " & CurrentFormView(Forms(mcstrFormName))

  ' Reopen the form in Normal mode.
  FormOpenReplace mcstrFormName, acNormal

  ' Example form for IsFormOpenInMode
  If IsFormOpenInMode(mcstrFormName, acNormal) Then
    Debug.Print mcstrFormName & " is open in normal mode"
  Else
    Debug.Print mcstrFormName & " is not open in normal mode"
  End If

  ' Reopen the form in Datasheet view
  FormOpenReplace mcstrFormName, acFormDS

  ' Example of IsFormOpenInDatasheetView
  If IsFormOpenInDatasheetView(mcstrFormName) Then
    Debug.Print mcstrFormName & " is open in DatasheetView"
  Else
    Debug.Print mcstrFormName & " is not open in DatasheetView"
  End If

  ' Reopen the form in Datasheet view
  FormOpenReplace mcstrFormName, acNormal

  ' Example of IsFormOpenInFormView
  If IsFormOpenInFormView(mcstrFormName) Then
    Debug.Print mcstrFormName & " is open in FormView"
  Else
    Debug.Print mcstrFormName & " is not open in FormView"
  End If

  ' Example of IsFormOpenInLayoutView
  If IsFormOpenInLayoutView(mcstrFormName) Then
    Debug.Print mcstrFormName & " is open in LayoutView"
  Else
    Debug.Print mcstrFormName & " is not open in LayoutView"
  End If

  ' Example of IsFormOpenInDesignView
  If IsFormOpenInDesignView(mcstrFormName) Then
    Debug.Print mcstrFormName & " is open in DesignView"
  Else
    Debug.Print mcstrFormName & " is not open in DesignView"
  End If

  ' Example of IsSubForm
  If IsSubForm(Me) Then
    Debug.Print Me.name & " is a subform"
  Else
    Debug.Print Me.name & " is a subform"
  End If

  ' Example of Form_SetSectionColorsIndividually
  Form_SetSectionColorsIndividually Me, vbWhite, vbGreen, vbBlue, vbRed, vbYellow

End Sub

Private Sub cmdSelect_Click()
  ' Example of DeSelectControl, SelectControl

  Static fSelected As Boolean

  If fSelected Then
    DeSelectControl Me.txtTest
  Else
    SelectControl Me.txtTest, False
  End If

  fSelected = Not fSelected

End Sub

Private Sub cmdTestCombo_Click()
  ' Example of ComboBoxOpen
  ComboBoxOpen cboTest

  ' Example of ComboSetFirst
  ComboSetFirst cboTest

End Sub

Private Sub cmdEnableDisable_Click()
  ' Example of EnableDisableControls

  Static fEnable As Boolean

  EnableDisableControls Me, 0, fEnable
  fEnable = Not fEnable

  ' wait 2 seconds
  Sleep 2000

  EnableDisableControls Me, 0, fEnable
  fEnable = Not fEnable

End Sub

Private Sub cmdAdd_Click()
  ' Example of FormAddRecord

  FormAddRecord Me

End Sub

Private Sub cmdFirst_Click()
  ' Example of FormMoveFirst

  FormMoveFirst Me

End Sub

Private Sub cmdLast_Click()
  ' Example of FormMoveLast

  FormMoveLast Me

End Sub

Private Sub cmdNext_Click()
  ' Example of FormMoveNext

  FormMoveNext Me

End Sub

Private Sub cmdPrevious_Click()
  ' Example of FormMovePrevious

  FormMovePrevious Me

End Sub

Private Sub cmdClose_Click()
  ' Example of FormClose

  FormClose Me.name, True

End Sub

Private Sub cmdProps_Click()
  Dim intCount As Integer

  ' Example of FormControlPropertiesChange
  intCount = FormControlPropertiesChange(mcstrFormName, "ControlTipText", "Tip changed: " & Now, True)

  ' Example of FormPropertyChange
  If FormPropertyChange(mcstrFormName, "Caption", "New caption: " & Now, True) Then
    Debug.Print "Form caption changed."
  Else
    Debug.Print "Form caption could not be changed."
  End If

  DoCmd.OpenForm mcstrFormName, acNormal, , , , acDialog

End Sub

Private Sub cmdDelete_Click()
  ' Example of FormDeleteRecord

  If MsgBox("Are you sure you want to delete this record? It cannot be recovered once deleted.", vbYesNo) = vbYes Then
    FormDeleteRecord Me, True
  End If

End Sub

Private Sub cmdSave_Click()
  ' Example of FormSaveRecord
  FormSaveRecord Me
End Sub

Private Sub cmdUndo_Click()
  ' Example of FormUndoRecord
  FormUndoRecord Me

End Sub

Private Sub cmdWait_Click()
  ' Example of WaitUntilFormCloses
  MsgBox "before"
  FormOpen mcstrFormName, True
  WaitUntilFormClose mcstrFormName
  MsgBox "after"
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