Online Book Reader

Home Category

Access Cookbook - Ken Getz [165]

By Root 2045 0
one.

Set the form's AutoCenter property to Yes.

Add the following code to the declarations section at the top of the form's module (replacing the values with the actual names for your table and splash form, if you've used different names):

Private Const acbPreloadTable = "zstblPreloadForms"

Private Const acbSplashForm = "frmSplash"

Create a new event procedure for the form's Open event. (If you're unsure of how to do this, see How Do I Create an Event Procedure? in the Preface of this book.)

Add the following code to the event procedure:

Private Sub Form_Open(Cancel As Integer)

' Preload forms.

Dim db As DAO.Database

Dim rst As DAO.Recordset

Dim varFormName As Variant

On Error GoTo HandleErr

DoCmd.OpenForm acbSplashForm

Set db = CurrentDb( )

' Preload the forms listed in zstblPreloadForms.

Set rst = db.OpenRecordset(acbPreloadTable, dbOpenSnapshot)

Do While Not rst.EOF

varFormName = rst("FormName")

If Not IsNull(varFormName) Then

DoCmd.OpenForm FormName:=varFormName, _

WindowMode:=acHidden, OpenArgs:="StayLoaded"

End If

rst.MoveNext

Loop

ExitHere:

DoCmd.Close acForm, acbSplashForm

If Not rst Is Nothing Then

rst.Close

End If

Set rst = Nothing

Exit Sub

HandleErr:

MsgBox "Error " & Err.Number & ": " & Err.Description, , "Form Open"

Resume ExitHere

End Sub

You can also copy this code from the frmSwitchboard1 form (not the frmSwitchboard form) in 08-01.MDB. (The frmSwitchboard1 version of the form always preloads forms, thus eliminating all the code associated with the "Preload and keep loaded forms" checkbox.)

Create an event procedure for the switchboard form's Close event. Add this code to the event procedure:

Private Sub Form_Close( )

' Unload preloaded forms

Dim db As DAO.Database

Dim rst As DAO.Recordset

Dim varFormName As Variant

On Error GoTo HandleErr

Set db = CurrentDb( )

' Unload the forms listed in zstblPreloadForms

Set rst = db.OpenRecordset(acbPreloadTable, dbOpenSnapshot)

Do Until rst.EOF

varFormName = rst("FormName")

If Not IsNull(varFormName) Then

DoCmd.Close acForm, varFormName

End If

rst.MoveNext

Loop

ExitHere:

If Not rst Is Nothing Then

rst.Close

End If

Set rst = Nothing

Exit Sub

HandleErr:

MsgBox "Error " & Err.Number & ": " & Err.Description, , "Form Open"

Resume ExitHere

End Sub

Create the following functions in a global module (or import the basStayLoaded module from 08-01.MDB):

Public Function acbOpenForm(strFormName As String, _

fStayLoaded As Boolean) As Boolean

' Open specified form and pass it the

' StayLoaded argument.

On Error GoTo acbOpenFormErr

If fStayLoaded Then

DoCmd.OpenForm strFormName, OpenArgs:="StayLoaded"

Else

DoCmd.OpenForm strFormName

End If

acbOpenFormExit:

Exit Function

acbOpenFormErr:

MsgBox "Error " & Err.Number & ": " & Err.Description, _

vbOKOnly + vbCritical, "acbOpenForm"

Resume acbOpenFormExit

End Function

Public Function acbCloseForm(frmToClose As Form)

' If StayLoaded is True, hide the form instead of closing it.

On Error GoTo acbCloseFormErr

If InStr(frmToClose.OpenArgs, "StayLoaded") > 0 Then

frmToClose.Visible = False

Else

DoCmd.Close acForm, frmToClose.Name

End If

acbCloseFormExit:

Exit Function

acbCloseFormErr:

MsgBox "Error " & Err.Number & ": " & Err.Description, _

vbOKOnly + vbCritical, "acbCloseForm"

Resume acbCloseFormExit

End Function

Throughout your application, when you create code that opens a form and you wish to load that form only once, call the acbOpenForm function from Step 8. If you wish to open a form from code, you can use this syntax:

Call acbOpenForm("formname", True)

You can also call the function directly from an event property. In this case, enter the following in the event property:

=acbOpenForm("formname", True)

For those forms that you don't wish to keep loaded, change the second parameter of acbOpenForm to False.

For each form you are preloading or loading with the acbOpenForm function, add a command button with the caption "Close". Enter the following

Return Main Page Previous Page Next Page

®Online Book Reader