Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Indexing Forms Collection using form name

Status
Not open for further replies.

StuMunro

Programmer
Mar 2, 2001
29
0
0
GB
I am trying to do the following but without success. This is just test code all in the same procedure, the real code is trying to use the forms collection else where.

Dim frm As Form_testRepairs
Dim f As Form

Set frm = New Form_testRepairs

For Each f In Forms
Debug.Print f.Name
Next
Debug.Print Forms("testRepairs").Name


when "Debug.Print Forms("testRepairs").Name" line is reached I get a run time error

Error # 2450 can't find the form 'testRepairs' referred to in a macro expression or Visual Basic code.@* The form you referenced may be closed or may not exist in this database

What name should I use to refer to this form in the forms collection?
 

From access Help:

The Forms collection contains all of the currently open forms in a Microsoft Access database.

To list all forms in the database, whether open or closed, enumerate the AllForms collection of the CurrentProject object. You can then use the Name property of each individual AccessObject object to return the name of a form.

AllForms Collection Example

The following example prints the name of each open AccessObject object in the AllForms collection.

Sub AllForms()
Dim obj As AccessObject, dbs As Object
Set dbs = Application.CurrentProject
' Search for open AccessObject objects in AllForms collection.
For Each obj In dbs.AllForms
If obj.IsLoaded = TRUE then
' Print name of obj.
Debug.Print obj.Name
End If
Next obj
End Sub
Terry

------------------------------------
People who don't take risks generally make about two big mistakes a year. People who do take risks generally make about two big mistakes a year. -Peter Drucker
 
It's important in this case to know what version of Access you are using. Access 2000 uses the AllForms collection but Access 97 does not.

I think it important to use the debug capabilities to explore why something doesn't work. Merely set a breakpoint on your line of code by pressing F9, run the code until the pointer stops, open the debug/immediate window and type: ?Forms("testRepairs").Name

You should get exactly the same error. Now try: ?Screen.ActiveForm.Name

Use the system capabilities for debugging. They would help answer many of the questions, just by testing, before asked.

By the way, I use the same method of referencing forms. You can either refer to them using the Screen object, or Forms!("formname"), or Forms!(index).

Steve King

Steve King Growth follows a healthy professional curiosity
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top