Article ID: 167222
Article Last Modified on 1/19/2007
Dim f as Form_Customers
-or-
Dim f as New Form_Customers
Dim f as Form Set f = Forms!CustomersNOTE: Before the Set statement, you must programmatically check to see if the form is open. If it isn't, first open the form using the OpenForm method.
Option Compare Database
Option Explicit
Sub SetHasModuleTrue()
On Error GoTo Err_SetHasModuleTrue
Dim MyDb As Database
Dim MyDoc As Document
Dim bolObjectState As Boolean
Dim strMessage As String
Set MyDb = CurrentDb ' Set the current dabatase.
' Run this section to set HasModule property to Yes for
' all the forms that don't already have HasModule set to Yes.
' Go through each form in the database.
For Each MyDoc In MyDb.Containers("Forms").Documents
' Get the state of the form.
bolObjectState = SysCmd(acSysCmdGetObjectState, acForm, _
MyDoc.Name)
If bolObjectState = 0 Then ' If the form is not open,
' Open the form in design view.
DoCmd.OpenForm MyDoc.Name, acDesign, , , , acHidden
' If the HasModule Property is No,
If Not Forms(MyDoc.Name).HasModule Then
Forms(MyDoc.Name).HasModule = True ' set HasModule to
' Yes.
DoCmd.Save acForm, MyDoc.Name ' Save the form.
End If
DoCmd.Close acForm, MyDoc.Name ' Close the form.
Else
' If the form is open but not in Design view,
If Forms(MyDoc.Name).CurrentView = 1 Then
' change the form to Design view.
DoCmd.OpenForm MyDoc.Name, acDesign
End If
' If the HasModule Property is No,
If Not Forms(MyDoc.Name).HasModule Then
Forms(MyDoc.Name).HasModule = True ' set HasModule to Yes
strMessage = "Form '" & MyDoc.Name & "' is already open."
strMessage = strMessage & " The HasModule Propterty has"
strMessage = strMessage & " been set to Yes. Would you"
strMessage = strMessage & " like to save and close the"
strMessage = strMessage & " form now?"
' Ask users if they want to save and close a previously
' opened form. If yes
If MsgBox(strMessage, vbYesNo + vbInformation) = _
vbYes Then
DoCmd.Save acForm, MyDoc.Name ' save the form.
DoCmd.Close acForm, MyDoc.Name 'Close the form.
End If
End If
End If
Next MyDoc ' Go to next form.
' Run this section to set HasModule property to Yes for
' all the Reports that don't already have HasModule set to Yes.
DoCmd.Echo False
' Go through each report in the database.
For Each MyDoc In MyDb.Containers("Reports").Documents
' Get the state of the report.
bolObjectState = SysCmd(acSysCmdGetObjectState, acReport, _
MyDoc.Name)
If bolObjectState = 0 Then 'If the report is not open
' Open the report in Design view.
DoCmd.OpenReport MyDoc.Name, acDesign
' If the HasModule Property is No,
If Not Reports(MyDoc.Name).HasModule Then
' set the HasModule to Yes.
Reports(MyDoc.Name).HasModule = True
DoCmd.Save acReport, MyDoc.Name ' Save the report.
End If
DoCmd.Close acReport, MyDoc.Name ' Close the report.
Else
' If the report is already open, open it in Design view.
DoCmd.OpenReport MyDoc.Name, acDesign
' If the HasModule Property is No,
If Not Reports(MyDoc.Name).HasModule Then
' set HasModule to Yes.
Reports(MyDoc.Name).HasModule = True
strMessage = "Form '" & MyDoc.Name & "' is already open."
strMessage = strMessage & " The HasModule Propterty has"
strMessage = strMessage & " been set to Yes. Would you"
strMessage = strMessage & " like to save and close the"
strMessage = strMessage & " report now?"
' Ask users if they want to save and close a previously
' opened report. If yes
If MsgBox(strMessage, vbYesNo + vbInformation) = _
vbYes Then
DoCmd.Save acReport, MyDoc.Name ' save the report.
DoCmd.Close acReport, MyDoc.Name 'Close the report.
End If
End If
End If
Next MyDoc ' Go to the next report.
DoCmd.Echo True
Exit_SetHasModuleTrue:
Exit Sub
Err_SetHasModuleTrue:
DoCmd.Echo True
MsgBox Err.Description
Resume Exit_SetHasModuleTrue
End Sub
Function TestNewInstance ()
Dim f as Form_Customers
End Function
?TestNewInstance()
Additional query words: predefined
Keywords: kberrmsg kbprb kbprogramming KB167222