Newbie: Code to loop through all forms

C

CJM

I need a simple bit of code to loop through all forms in a database (in
excess of 150) and change some properties within those forms.

I'm more a VB/Web developer so doing this in Access is all new to me!

I tried a basic structure as follows:

Sub Main()

Dim oForm As Form

For Each oForm In Application.Forms
Debug.Print "Formname: " & oForm.Name
Next

Set oForm = Nothing

End Sub

However when I run it within the VBA editor it does nothing; a step-through
reveals that the code doesnt even run through the For Each loop once.

I'm not even sure if running the code through the VBA editor is supposed to
work or whether it should be called a different way.

Thanks
 
D

Dave

Try this (Air Code)


Sub FormList()


For varloop = 0 To Forms.Count - 1
debug.print Forms(varloop).Name
Next


End Function
 
C

CJM

Dave,

I tried your code, but it didnt work either - that is, it worked, but didnt
produce the desired results.

I assume this should just run from the VBA editor?

It appears that Forms.Count in your code is in fact Zero! Why is this?

Thanks

Chris
 
G

gandalf

-----Original Message-----
I need a simple bit of code to loop through all forms in a database (in
excess of 150) and change some properties within those forms.

I'm more a VB/Web developer so doing this in Access is all new to me!

I tried a basic structure as follows:

Sub Main()

Dim oForm As Form

For Each oForm In Application.Forms
Debug.Print "Formname: " & oForm.Name
Next

Set oForm = Nothing

End Sub

However when I run it within the VBA editor it does nothing; a step-through
reveals that the code doesnt even run through the For
Each loop once.

->This is because the forms collection only contains open
forms.
I'm not even sure if running the code through the VBA editor is supposed to
work or whether it should be called a different way.

Thanks

Try
Dim con_tainer As Container
Dim docu_ment As Document
For Each con_tainer In CurrentDb.Containers
For Each docu_ment In con_tainer.Documents
If docu_ment.name= ... then ...
Next
Next
See documents, containers in the ACCESS-VBA-help for more
information
 
D

Dirk Goldgar

CJM said:
I need a simple bit of code to loop through all forms in a database
(in excess of 150) and change some properties within those forms.

I'm more a VB/Web developer so doing this in Access is all new to me!

I tried a basic structure as follows:

Sub Main()

Dim oForm As Form

For Each oForm In Application.Forms
Debug.Print "Formname: " & oForm.Name
Next

Set oForm = Nothing

End Sub

However when I run it within the VBA editor it does nothing; a
step-through reveals that the code doesnt even run through the For
Each loop once.

I'm not even sure if running the code through the VBA editor is
supposed to work or whether it should be called a different way.

Thanks

As others have explained, the Forms collection contains only the forms
that are currently open. If you're using Access 2000 or later, you can
write code modeled on this routine, which I use to turn the
"AllowDesignChanges" property off (or on) for all forms:

'----- start of example code -----
Sub SetAllowDesignChanges(OnOrOff As Boolean)

Dim obj As AccessObject

For Each obj In CurrentProject.AllForms
DoCmd.OpenForm obj.Name, acDesign, , , , acHidden
Forms(obj.Name).AllowDesignChanges = OnOrOff
DoCmd.Close acForm, obj.Name, acSaveYes
Next obj

End Sub

'----- end of example code -----
 
C

CJM

Gandalf/Dirk

Both your solutions seem promising... I've passed the code onto my colleague
who has this problem.

Thanks for your help.

Chris
 
P

Pieter Wijnen

You have to Open all forms to do the work
ie
Dim Ctr As DAO.Container
Dim Doc As DAO.Document
Dim F AS Access.Form
Set Ctr = Currentdb.Containers("Forms")
For Each Doc in Ctr.Documents
DoCmd.OpenForm Doc.Name,AcViewDesign
Set F = Access.Forms(Doc.Name)
...
Next

HTH
Pieter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top