Visual Basic Head Ache

  • Thread starter Thread starter RichardCC
  • Start date Start date
R

RichardCC

I have created a very large database 300 forms each with
VB and I have now realised that because I have copied
template forms that I have already created with vb in them
I have hundreds of VB I want to get rid of..Can I use
anything that will delete all code under the heading
general in one go or have I to delete them one at a
time ????
 
I have hundreds of VB I want to get rid of..Can I use
anything that will delete all code under the heading
general in one go or have I to delete them one at a
time ?

Richard,

You delete the modules behind the 300 forms by
setting each form's "HasModule" property to "False".
You can do this for all 300 forms using some vb code
as illustrated below. However...

As this is a very destructive procedure, you will definitely
want to make a backup copy of your database first!

As I thought you may have some forms that you don't
want to remove the modules from, you'll notice that, in
the following code, you can list the forms that need to
keep their modules on the line beginning "Case ...",
eg Case "Form1", "Form2", "Form3", etc.

Regards
Geoff


Sub DeleteFormCode()

' Assumes reference to "Microsoft DAO 3.6 Object Library"
' (set in vba editor on Tools References menu)

' Warning - backup the database first! This code will
' remove code behind all forms, except those listed as
' exceptions.

Dim db As DAO.Database
Dim cntForms As DAO.Container
Dim docFrm As DAO.Document
Dim strFormName As String
Dim frm As Form

On Error GoTo HandleErrors

Set db = CurrentDb()
Set cntForms = db.Containers("Forms")
For Each docFrm In cntForms.Documents
strFormName = docFrm.Name
Select Case strFormName
Case "Form1", "Form2", "Form3"
' List on the above line all the form names
' you DON'T want to remove modules from.
Case Else
' All other forms will have their modules
' removed by this code:
DoCmd.OpenForm strFormName, acDesign
Set frm = Forms(strFormName)
frm.HasModule = False
DoCmd.Close acForm, strFormName, acSaveYes
End Select
Next

Bye:
Set frm = Nothing
Set docFrm = Nothing
Set cntForms = Nothing
Set db = Nothing
Exit Sub

HandleErrors:
MsgBox Err.Description, vbOKOnly, "Error No: " & Err.Number
Resume Bye

End Sub
 
Richard,

I've been thinking about your database; especially
that you've created so many forms from a template
form. This must have been a truly Herculean task!

I don't know whether this would be helpful - you may
already know it - but have you come across using the
'New' keyword to create additional run-time instances
of an existing form? For example:

Public frm as Form_Customers
Sub CreateNewCustomerForm()
Set frm = New Form_Customers
End Sub

This only works if the form in question has a code
module - although no code is needed in the module.

There's some good reference material on how to
manage multiple instances of forms using arrays
or collections.

Just a thought...
Geoff
 
Back
Top