By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,643 Members | 1,089 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,643 IT Pros & Developers. It's quick & easy.

I'm trying to list controls in immediate window... what's wrong with this code?

P: n/a
MLH
Sub ListControlsBttn_Click ()
'************************************************* ************************
' Purpose: Run the Controls Collection for user-specified form.
' The controls collection is a form's default collection.
'************************************************* ************************
On Error GoTo ListControlsBttn_ClickError
Dim ThisForm As String, Msg As String, Title As String, Defvalue As
String
ThisForm = Me.Name
Dim i As Integer, intHowmany As Integer, WhichForm As String

Msg = "Enter form name." ' Set prompt.
Title = "Form Name?" ' Set title.
Defvalue = "frmListThings" ' Set default return
value.
WhichForm = InputBox$(Msg, Title, Defvalue) ' Get user input.
If WhichForm = "" Then Exit Sub
For i = 0 To Forms(WhichForm).Count - 1
intHowmany = intHowmany + 1
Debug.Print intHowmany; ") "; Forms(WhichForm)(i).Name
Next i

ExitButton11_Click:
Exit Sub

ListControlsBttn_ClickError:
Dim r As String, k As String, Message3 As String
r = "The following unexpected error occurred in Sub
ListControlsBttn_Click, CBF on " & ThisForm & "."
k = CRLF & CRLF & "Error # " & Trim$(Str$(Err)) & ": " & QUOTE &
Error$ & QUOTE
Message3 = r & k
MsgBox Message3, 48, "Unexpected Error - " & MyApp$ & ", rev. " &
MY_VERSION$
Resume ExitButton11_Click

End Sub

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxx
If I type in a form name, I get error #2450 (invalid reference to form
'frmMyForm'). If I accept the default form name (frmListThings) I get
what I want. What's wrong?
Nov 12 '05 #1
Share this Question
Share on Google+
7 Replies

P: n/a
Dim ctl As Control
Dim strWhichForm As String

strWhichForm = InputBox$("What form?")
For each ctl In Forms(strWhichForm).Controls
Debug.Print ctl.Name
Next

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"MLH" <CR**@NorthState.net> wrote in message
news:qe********************************@4ax.com...
Sub ListControlsBttn_Click ()
'************************************************* ************************
' Purpose: Run the Controls Collection for user-specified form.
' The controls collection is a form's default collection.
'************************************************* ************************
On Error GoTo ListControlsBttn_ClickError
Dim ThisForm As String, Msg As String, Title As String, Defvalue As
String
ThisForm = Me.Name
Dim i As Integer, intHowmany As Integer, WhichForm As String

Msg = "Enter form name." ' Set prompt.
Title = "Form Name?" ' Set title.
Defvalue = "frmListThings" ' Set default return
value.
WhichForm = InputBox$(Msg, Title, Defvalue) ' Get user input.
If WhichForm = "" Then Exit Sub
For i = 0 To Forms(WhichForm).Count - 1
intHowmany = intHowmany + 1
Debug.Print intHowmany; ") "; Forms(WhichForm)(i).Name
Next i

ExitButton11_Click:
Exit Sub

ListControlsBttn_ClickError:
Dim r As String, k As String, Message3 As String
r = "The following unexpected error occurred in Sub
ListControlsBttn_Click, CBF on " & ThisForm & "."
k = CRLF & CRLF & "Error # " & Trim$(Str$(Err)) & ": " & QUOTE &
Error$ & QUOTE
Message3 = r & k
MsgBox Message3, 48, "Unexpected Error - " & MyApp$ & ", rev. " &
MY_VERSION$
Resume ExitButton11_Click

End Sub

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxx
If I type in a form name, I get error #2450 (invalid reference to form
'frmMyForm'). If I accept the default form name (frmListThings) I get
what I want. What's wrong?

Nov 12 '05 #2

P: n/a
MLH
I found that my code works fine if the form I'm polling
is open. If its closed, I get the error. If its open in either
form view or design view, the code runs as expected.
Is that normally what one should expect?
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxx
On Fri, 26 Dec 2003 11:03:05 +0800, "Allen Browne"
<Al*********@SeeSig.Invalid> wrote:
Dim ctl As Control
Dim strWhichForm As String

strWhichForm = InputBox$("What form?")
For each ctl In Forms(strWhichForm).Controls
Debug.Print ctl.Name
Next


Nov 12 '05 #3

P: n/a
On Thu, 25 Dec 2003 22:12:40 -0500, MLH <CR**@NorthState.net> wrote:

Yes. As the docs say, the Forms collection is the list of running
forms. They don't have to be visible, so DoCmd.OpenForm
"myform",,,acFormReadOnly is an option.

Closed forms are accessed through the Documents collection.

-Tom.

I found that my code works fine if the form I'm polling
is open. If its closed, I get the error. If its open in either
form view or design view, the code runs as expected.
Is that normally what one should expect?
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxx
On Fri, 26 Dec 2003 11:03:05 +0800, "Allen Browne"
<Al*********@SeeSig.Invalid> wrote:
Dim ctl As Control
Dim strWhichForm As String

strWhichForm = InputBox$("What form?")
For each ctl In Forms(strWhichForm).Controls
Debug.Print ctl.Name
Next


Nov 12 '05 #4

P: n/a
tom
Yes, the Forms collection only contains open forms.
In your code, you could check to see if the form is open, and, if not,
open it hidden (and then close it when you're done).

-td
I found that my code works fine if the form I'm polling
is open. If its closed, I get the error. If its open in either
form view or design view, the code runs as expected.
Is that normally what one should expect?

Nov 12 '05 #5

P: n/a
to*****@no.spam.cox.net (Tom van Stiphout) wrote in
<8t********************************@4ax.com>:
On Thu, 25 Dec 2003 22:12:40 -0500, MLH <CR**@NorthState.net>
wrote:

Yes. As the docs say, the Forms collection is the list of running
forms. They don't have to be visible, so DoCmd.OpenForm
"myform",,,acFormReadOnly is an option.

Closed forms are accessed through the Documents collection.


In Access97 and before, yes.

In A2K and later, you can use Project.AllForms("YourForm") to get
to all forms, unopened or not.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 12 '05 #6

P: n/a
On Fri, 26 Dec 2003 20:24:44 GMT, dX********@bway.net.invalid (David
W. Fenton) wrote:

Indeed. The CurrentProject object is a nice addition, and also works
in ADPs.
-Tom.

to*****@no.spam.cox.net (Tom van Stiphout) wrote in
<8t********************************@4ax.com>:
On Thu, 25 Dec 2003 22:12:40 -0500, MLH <CR**@NorthState.net>
wrote:

Yes. As the docs say, the Forms collection is the list of running
forms. They don't have to be visible, so DoCmd.OpenForm
"myform",,,acFormReadOnly is an option.

Closed forms are accessed through the Documents collection.


In Access97 and before, yes.

In A2K and later, you can use Project.AllForms("YourForm") to get
to all forms, unopened or not.


Nov 12 '05 #7

P: n/a
MLH
On Thu, 25 Dec 2003 21:25:11 -0700, Tom van Stiphout
<to*****@no.spam.cox.net> wrote:
On Thu, 25 Dec 2003 22:12:40 -0500, MLH <CR**@NorthState.net> wrote:

Yes. As the docs say, the Forms collection is the list of running
forms. They don't have to be visible, so DoCmd.OpenForm
"myform",,,acFormReadOnly is an option.

And a very good one, I might add. Thx Tom.
Nov 12 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.