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

Enumerating Textboxes

P: n/a
I want to do a quick check of a group of textboxes only to see if they
contain anything.

I tried this code but it appears to go from box to box based on the
boxes time of creation.

Is it possible to reset creation time so the loop checks from top to
bottom without skipping around.

Private Sub Command49_Click()
Dim ctl As Control
For Each ctl In Me.Controls
If ctl.ControlType = acTextBox Then
If ctl.Tag = "Verify Data" Then
If ctl.Value > "" Then
ctl.Value = ctl.Value
Else
MsgBox "Missing Data"
ctl.SetFocus
Exit For
End If
End If
End If
Next
End Sub

Thanks, Rick

Nov 18 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
Hi Rick,

Suggest doing a Cut and then Paste of the textboxes in your form. Perhaps
that will reset the
creation time of the textboxes in top to bottom order.

HTH. Linda

"2D Rick" <rb*******@compuserve.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
I want to do a quick check of a group of textboxes only to see if they
contain anything.

I tried this code but it appears to go from box to box based on the
boxes time of creation.

Is it possible to reset creation time so the loop checks from top to
bottom without skipping around.

Private Sub Command49_Click()
Dim ctl As Control
For Each ctl In Me.Controls
If ctl.ControlType = acTextBox Then
If ctl.Tag = "Verify Data" Then
If ctl.Value > "" Then
ctl.Value = ctl.Value
Else
MsgBox "Missing Data"
ctl.SetFocus
Exit For
End If
End If
End If
Next
End Sub

Thanks, Rick

Nov 18 '05 #2

P: n/a
Order stayed the same if I Cut and Pasted within same form and pasting
into new form.

Thanks for the reply
RICK

Nov 18 '05 #3

P: n/a
2D Rick wrote in message
<11**********************@g43g2000cwa.googlegroups .com> :
I want to do a quick check of a group of textboxes only to see if they
contain anything.

I tried this code but it appears to go from box to box based on the
boxes time of creation.

Is it possible to reset creation time so the loop checks from top to
bottom without skipping around.

Private Sub Command49_Click()
Dim ctl As Control
For Each ctl In Me.Controls
If ctl.ControlType = acTextBox Then
If ctl.Tag = "Verify Data" Then
If ctl.Value > "" Then
ctl.Value = ctl.Value
Else
MsgBox "Missing Data"
ctl.SetFocus
Exit For
End If
End If
End If
Next
End Sub

Thanks, Rick


I think there was a thread in this NG not long ago discussing
this. If I'm not mistaken, I think the answer was no (at least
for practical purposes), think of collections as unordered.

Here's one suggestion
- use a naming convention on the boxes that makes it
possible to loop them as "control array", i e

txt1, txt2, txt3..

for lngcounter = 1 to N
msgbox me.controls("txt" & cstr(lngcounter)).value
next lngcounter

--
Roy-Vidar

Nov 18 '05 #4

P: n/a
How do you know that the code is looping through the textboxes based on
creation time? I'm not challenging you, just curious as to how that
works. You may wish to change the tab order of your textboxes so it's
sequential from top to bottom of the form to see if this will solve the
issue.

The code I usually use to loop through textboxes is thus:
Dim ctl As Control
For Each ctl In frmFormName.Controls
If ctl.ControlType = acTextBox Then
If ctl.Text="" Then
MsgBox "Textless"
End If
End If
Next ctl

Nov 18 '05 #5

P: n/a
Thanks Roy And Steve

The tab order is set from top to bottom and works as planned.
The textboxes were created from top to bottom on day one.
Since then I have shuffled the placement of the text boxes to help in
input flow of data.
They must have some hidden reference in controls collection????????

Rick

Nov 18 '05 #6

P: n/a
I have just looked at the Object Browser and note that a Textbox has a
property TabIndex. You could use this to sort the Controls collection.

Give it try

Nov 18 '05 #7

P: n/a
Jim's got a point. You could loop through all available textboxes and
store their names in an array along with their TabIndex, then sort the
array by TabIndex, and loop through the array checking the Text
property of each textbox.

Nov 18 '05 #8

P: n/a
"Squirrel" <wi*****@covad.net> wrote in
news:67***************************@msgid.meganewss ervers.com:
Suggest doing a Cut and then Paste of the textboxes in your form.
Perhaps that will reset the
creation time of the textboxes in top to bottom order.


The z order is set by when you put the control on the form, so if
you bring the control to front (or sent id to back) you are moving
it in the z order.

I also think the basic task being performed is *wrong* in the first
place. If the value is required, then don't allow the record to be
saved until all fields are filed out. That can be done by having the
AfterUpdate event of each of the required controls check all the
required fields and enable the SAVE button when everything is filled
out. This would include informing a user that a required field can't
be Null if they delete the value in a required field.

I just think it's user-hostile to present multiple validation
messages like this, one field after the other. Instead, it's better,
seems to me, to tell the user that all required fields have not been
filed out, tell them which ones are missing, and then dump them back
to the form, rather than hitting them with message box after message
box.

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

P: n/a
I always find your advice useful or though provoking, so I'll consider
what you've written.
Thanks for the reply
Rick

ps. your colorful skirmishes with the prickly bunch lighten my day.

Nov 18 '05 #10

P: n/a
Thanks Jim and Steve,
Storing and sorting an array are new to me but not "out of range" of my
knowledge base.
Rick

Nov 18 '05 #11

P: n/a
David is right about the underlying theory. Another way to make this
procedure more user-friendly would be to simply check all the text
fields when the user clicks the Save button. One thing I usually do is
create a string and then use IF statements to add items to the string.
Example:
If txtBox1="" Or IsNull(txtBox1) Then
str=str+"Please enter a value for txtBox1." + vbCrLf
ElseIf txtBox2="" Or IsNull(txtBox2) Then
str=str+"Please enter a value for txtBox2." + vbCrLf
End If

lblErrorMessage=str

This way, if the user leaves both boxes empty, they are presented with
the following message:
Please enter a value for txtBox1.
Please enter a value for txtBox2.

At the same time, and without extra code, if the user leaves only one
box blank, he will still get the appropriate message.

Nov 21 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.