Enumerating Textboxes

Enumerating Textboxes

am 18.11.2005 00:25:19 von 2D Rick

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

Re: Enumerating Textboxes

am 18.11.2005 03:59:29 von Squirrel

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" wrote in message
news:1132269919.837608.164110@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
>

Re: Enumerating Textboxes

am 18.11.2005 18:30:22 von 2D Rick

Order stayed the same if I Cut and Pasted within same form and pasting
into new form.

Thanks for the reply
RICK

Re: Enumerating Textboxes

am 18.11.2005 19:08:08 von RoyVidar

2D Rick wrote in message
<1132269919.837608.164110@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

Re: Enumerating Textboxes

am 18.11.2005 19:20:12 von Steve

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

Re: Enumerating Textboxes

am 18.11.2005 19:37:05 von 2D Rick

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

Re: Enumerating Textboxes

am 18.11.2005 19:47:14 von Keith Tizzard

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

Re: Enumerating Textboxes

am 18.11.2005 21:13:16 von Steve

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.

Re: Enumerating Textboxes

am 18.11.2005 21:36:10 von dXXXfenton

"Squirrel" wrote in
news:67656$437d438c$45035d2d$21048@msgid.meganewsservers.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

Re: Enumerating Textboxes

am 18.11.2005 22:28:24 von 2D Rick

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.

Re: Enumerating Textboxes

am 18.11.2005 22:36:32 von 2D Rick

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

Re: Enumerating Textboxes

am 21.11.2005 15:05:22 von Steve

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.