Iterating all forms

R

Ray Cacciatore

Can someone give me an example on how to iterate all forms.

I tried:

Dim dbs As Object

Set dbs = Application.CurrentProject
For Each frm In dbs.AllForms '/// Incompatible type error on this line
.....
Next frm

but I keep getting in incompatible type error.
I'm using DAO.

Ray
 
D

Dirk Goldgar

Ray Cacciatore said:
Can someone give me an example on how to iterate all forms.

I tried:

Dim dbs As Object

Set dbs = Application.CurrentProject
For Each frm In dbs.AllForms '/// Incompatible type error on this line
....
Next frm

but I keep getting in incompatible type error.
I'm using DAO.

Ray

You'd have to have declared frm as an AccessObject:

Dim frm As AccessObject

If you plan to do anything with the form *as an Access form*, you'll
need to open the form and act on the form using a reference to the
opened form in the Forms collection.

For example, here's a sample procedure to clear or set the
AllowDesignChanges property in all forms:

'----- start of 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 code -----
 
R

Ray Cacciatore

I'm trying to iterate all controls within each form. Teh thing is that I can
iterate the form properties but I need to go one step further and iterate
each control within each form. All within an inner loop.

This code does not work:

Dim obj As AccessObject, dbs As Object
Dim ctl As Control
Dim mCount As Integer

Set dbs = Application.CurrentProject

mCount = 0
For Each obj In dbs.AllForms
If obj.Type = acForm Then
For Each ctl In obj.Controls '/// Error on this line
If InStr(1, ctl.Name, "mString") > 0 Then
Debug.Print "Form: " & frm.Name & ": " & ctl.Name
mCount = mCount + 1
End If
Next ctl
End If
Next obj
 
D

Douglas J. Steele

As Dirk told you, you need to open the form in order to work with it.

Dim obj As AccessObject, dbs As Object
Dim ctl As Control
Dim mCount As Integer

Set dbs = Application.CurrentProject

mCount = 0
For Each obj In dbs.AllForms
DoCmd.OpenForm obj.Name, acDesign, , , , acHidden
For Each ctl In Forms(obj.Name).Controls
If InStr(1, ctl.Name, "mString") > 0 Then
Debug.Print "Form: " & obj.Name & ": " & ctl.Name
mCount = mCount + 1
End If
Next ctl
DoCmd.Close acForm, obj.Name, acSaveNo
Next obj
 

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