HasModule (1 Viewer)

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 08:40
Joined
Sep 12, 2006
Messages
15,614
I am trying to check all my forms "hasmodule" property

can i get at this without opening the form in design mode, as this looks clumsy?
 

jdraw

Super Moderator
Staff member
Local time
Today, 04:40
Joined
Jan 23, 2006
Messages
15,364
Dave,

If you just want to know if the Form has a module, you could run something like this. Not pretty, but a 1 time thing.

Code:
Sub ShowFormHasModule()

On Error GoTo Err_Handler

Dim db As DAO.Database
Dim doc As DAO.Document

Dim lngFormCount As Long
Set db = CurrentDb

For Each doc In db.Containers("Forms").Documents
          DoCmd.OpenForm doc.name, acDesign, WindowMode:=acHidden
    With Forms(doc.name)
       lngFormCount = lngFormCount + 1
       Debug.Print .name & " - Has Module  " & .HasModule
       DoCmd.Close acForm, .name
    End With
Next doc
Exit_Point:
Set doc = Nothing
Set db = Nothing

MsgBox "*** Scanned " & lngFormCount & " forms."
Exit Sub
Err_Handler:
MsgBox Err.Description, vbExclamation, "Error " & Err.number
Resume Exit_Point
End Sub
 

MarkK

bit cruncher
Local time
Today, 01:40
Joined
Mar 17, 2004
Messages
8,178
Here's a function that checks if the module (VBComponent) of the form exists in the IDE...
Code:
Function FormHasModule(ByVal FormName As String) As Boolean
[COLOR="Green"]   'ensure that given FormName is prefixed with 'Form_'[/COLOR]
   If left(FormName, 5) <> "Form_" Then FormName = "Form_" & FormName
[COLOR="Green"]   'if this reference fails then the form does not have a module 
   'since form modules are VB Components[/COLOR]
   On Error Resume Next
   FormHasModule = VBE.ActiveVBProject.VBComponents.Item(FormName).Name = FormName
End Function
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 08:40
Joined
Sep 12, 2006
Messages
15,614
jdraw: that's what i am doing, but i didn'twant to open the forms in design mode if I didn't have to. - anyway, it didn't olve my problem - see next


lagbolt: this might do it.

the problem is, corruption has removed the code from some modules in a large app. i thought checking the hasmodule property would identify them, but it hasn't. hasmodule is still true, but the code has gone - so now I need a way of checking each form to see if there is anything in the code module.
 

ChrisO

Registered User.
Local time
Today, 19:40
Joined
Apr 30, 2003
Messages
3,202
Why not just reinstate the last backup?

Chris.
 

ChrisO

Registered User.
Local time
Today, 19:40
Joined
Apr 30, 2003
Messages
3,202
Code:
Public Sub FormModulesWithNoCode()
    Dim obj As AccessObject
    
    For Each obj In Application.CurrentProject.AllForms
    
        On Error Resume Next
        
        [color=green]' From lagbolt.[/color]
        Debug.Print VBE.ActiveVBProject.VBComponents.Item("Form_" & obj.Name).Name;
        
        If Err.Number = 0 Then
            DoCmd.OpenForm obj.Name, acViewDesign
            
            If Forms(obj.Name).Module.CountOfLines = 0 Then
                Debug.Print "        <<< has module with no code."
            Else
                Debug.Print
            End If
            
            DoCmd.Close acForm, obj.Name, acSaveNo
        End If
        
    Next obj
    
End Sub
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 08:40
Joined
Sep 12, 2006
Messages
15,614
chris

thanks for the line check code - that will do it, I am sure

as for backups, some of the forms are little used, and it appears that the corruption/missing code has been there for a few versions now. so i am having to go back a few versions to find code for the forms that have lost it, and repair them on a ad-hoc basis.

that's why it will help to idenitify the problem forms in code. (there are over 200 forms, so I need an automated procedure - there are about 10-15 with no module at all -ie hasmodule=false, but they are all OK - they do not have modules.)
 

Galaxiom

Super Moderator
Staff member
Local time
Today, 19:40
Joined
Jan 20, 2009
Messages
12,849
there are over 200 forms

Sounds like one hell of an app. If I didn't know you better I would definitely wonder if the developer had sufficiently considered the potential for usng less but more versatile forms.
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 08:40
Joined
Sep 12, 2006
Messages
15,614
probably but you know how these things grow. It becomes harder and harder to redesign the whole thing.
 

Simon_MT

Registered User.
Local time
Today, 08:40
Joined
Feb 26, 2007
Messages
2,177
Dave,

I didn't have the problems you were facing however I did migrate all the Form 'Modules' to Functions, so I could find them, originally developed under Access 1997. It was just easier in the long run.

Simon
 

ChrisO

Registered User.
Local time
Today, 19:40
Joined
Apr 30, 2003
Messages
3,202
This business of losing code in modules seems to be becoming more prevalent.
Might be just A2007 and A2010 because I’ve never had it happen in A2003.

In any case, attached is a little A2003 demo (A2000 file format).
It has some test cases, Form, Form with no module, Form with module but no code, Report with module and code, Report with module but no code, Standard 90k module, Class module with no code.

To run the test, open module mdlCheckModules and run Sub CheckModules.

What it should do is:-
1. Open every module it can find except module mdlCheckModules.
2. Build a list of modules in a table.
3. Check if the Form or Report has a module.
4. Check if the Form, Report, Standard or Class module has code.
5. If there is code then dump that code to a text file.
6. Produce a Report of the findings.

The text dump is done relative to the *.mdb file it is testing as in:-
Directory of Test File.
--- ModuleBackups
------Directory named the same as the file under test.
---------Directory Date/Time stamped.
---------Directory Date/Time stamped.
---------Directory Date/Time stamped.

It could be rewritten to allow a browse for file but that could also involve a password.

This has only been tested on A2003 MDB files so I don’t know how it would respond under later versions.

Edit:
Please be aware that some Forms need a class module even if there is no code in the module.

Chris.
 

Attachments

  • CheckModules_A2K3_V1.zip
    67.7 KB · Views: 109
Last edited:

Users who are viewing this thread

Top Bottom