Loop through text controls to setfocus

G

Guest

I have an unbound form that is used as a type of worksheet for our users.
They will click on various buttons to enable certain controls to enter
values. Here is a scenario: There are 6 text boxes that enable with one
button. I want to set the focus to the first text box that doesn't have a
value (or the first if they all have values). I have a routine that loops
through the control appropriately unfortunately it doesn't loop through the
controls in the order I need. How can I control the order of the loop - is
that possible? Could I use the tab index to accomplish this? If so, I can't
quite figure out how. Thanks for your assistance........Code example below

Here's what I have so far:

For Each ctrl In Me.Controls
strName = ctrl.Name
If ctrl.ControlType = acTextBox Then
If ctrl.Enabled = True And IsNull(ctrl) Then
ctrl.SetFocus
Exit For
End If
End If
Next ctrl

LeAnn
 
G

Guest

The code you have will loop all the controls but it does it in the sequence
in which they were added to the form. At least that is how I think it works.
Suffice to say it is not enough to deem an order.

Have you tried using the tag property for each control? That can be very
handy. You could specify a sort sequence

me.Control1.tag = 1
Me.otherControl.tag = 2
etc...

Then change your routine to process them in tag sequence. Or you could put
the controls name in an array and process them in that sequence. Either way
you will have to add some additional logic.

I hope this helps!
 
G

Guest

Thanks for your reply

Sounds interesting but I haven't used the tag control property before. I
have over 100 controls on this form. In order to get the proper sequence I'd
have to manually set this property on each control or just set the ones I
need the sequence for? Then how would I loop using that?
 
A

Arvin Meyer [MVP]

I am not sure that the Tab index will acomplish what you need. I'd try it
first though since it's so easy to set. If it doesn't consider using the Tag
property to build your own custom order. That would also eliminate the
requirement to test for a textbox controltype, since the tag property can be
used for both purposes (i.e. if tag is empty, move on)
--
Arvin Meyer, MCP, MVP
Microsoft Access
Free Access downloads
http://www.datastrat.com
http://www.mvps.org/access
 
A

Arvin Meyer [MVP]

Apparently great minds think alike <g> Yes you'd need to manually set the
tag property, but only for the 6 textboxes. You;d do something like
(aircode):

Dim ctl As Control
Dim ctr As Variant

For Each ctrl In Me.Controls
ctr= 1
If ctrl.Tag = ctr Then
If ctrl.Enabled = True And IsNull(ctrl) Then
ctrl.SetFocus
Exit For
End If
End If
ctr = ctr + 1
If ctr > 6 Then Exit Sub
Next ctrl
--
Arvin Meyer, MCP, MVP
Microsoft Access
Free Access downloads
http://www.datastrat.com
http://www.mvps.org/access
 
G

Guest

Great suggestion!- I made a few changes. I would rate your posting and
Rick's but my browser does not display the "Was this helpful" button anymore.
Do you know why?

ctr = 1
For Each ctrl In Me.Controls
varTag = ctrl.Tag
strName = ctrl.Name
ctr = CStr(ctr)
If varTag = ctr Then
If ctrl.Enabled = True And IsNull(ctrl) Then
ctrl.SetFocus
Exit For
End If
ctr = ctr + 1
End If
If ctr > 6 Then Exit For
Next ctrl
 
G

Guest

Ooops I should have tested it a bit more. Unfortunately the For Each only
loops through the controls once and if the first empty control say tag = "3"
comes before the ctr reaches 3 then no focus gets set. I made a slight
correction. Thanks for your help.

ctr = 1
Do Until ctr > 6
For Each ctrl In Me.Controls
varTag = ctrl.Tag
strName = ctrl.Name
ctr = CStr(ctr)
If varTag = ctr Then
If ctrl.Enabled = True And IsNull(ctrl) Then
ctrl.SetFocus
bolFocus = True
Exit For
End If
ctr = ctr + 1
End If
'If ctr > 6 Then Exit For
Next ctrl
If bolFocus = True Then Exit Do
Loop
 
G

Guest

Ok, I'm dumb................ My correction still didn't work - same reason.

Solution - delete and readd the controls that came too early in the loop
forcing them to be evaluated later in the loop. The original loop works now.
 

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