UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome to UtterAccess! Please ( Login   or   Register )

Custom Search
 
   Reply to this topicStart new topic
> Cycle Thru Controls In Tab Order?, Access 2003    
 
   
whdyck
post Mar 20 2017, 12:12 PM
Post#1



Posts: 226
Joined: 20-July 11
From: Winnipeg, MB, Canada


I have the following code that cycles thru a form's controls to see if any required values are missing. If there are, it builds an error message informing the user of the missing values:

CODE
    For Each ctl In pfrmSavingForm.Controls
        If ctl.ControlType = acComboBox Or ctl.ControlType = acTextBox Then
            If ctl.Enabled And Not (ctl.Locked) And ctl.Visible And ctl.Controls.Count > 0 Then
                ' Control is editable and has an attached label
                strLabel = ctl.Controls.Item(0).Caption
                If Left(strLabel, 1) = "*" Then
                    ' The control is required
                    If Nz(ctl.Value, "") = "" Then
                        ' The control is empty, so append to the error message
                        strRequiredFieldsMissing = strRequiredFieldsMissing & vbNewLine & strLabel
                    End If
                End If
            End If
        End If
    Next ctl

This code would typically run in the form's BeforeUpdate event.

Problem is that the control order seems to be the order the controls were created. I'd prefer to build the error message in the tab-order of the controls.

Is this possible?

Thanks.

Wayne
Go to the top of the page
 
theDBguy
post Mar 20 2017, 12:22 PM
Post#2


Access Wiki and Forums Moderator
Posts: 70,391
Joined: 19-June 07
From: SunnySandyEggo


Hi Wayne,

It should be possible, but you may have to work harder for it. You can use the TabIndex property of the control to go through them in order.

It might make it simpler for you if you store each message in an array, so you can control the order of the messages when you assemble them for display to the user.

--------------------
Just my 2 cents... "And if I claim to be a wise man, it surely means that I don't know" - Kansas
Microsoft Access MVP | Access Website | Access Blog | Email
Go to the top of the page
 
mklein
post Mar 20 2017, 01:09 PM
Post#3



Posts: 219
Joined: 7-August 12
From: BC, Canada


How many controls are we talking about? Sometimes, if I have a small subset of controls that I need to work with, I re-expose them as a custom array on the form. Consider something like...
CODE
private m_myControls

property get MyControls as variant
   'this property loads a custom control array on first reference, and then exposes it for the lifetime of this instance of the form
   if isempty(m_myControls) then m_myControls = Array(me.firstname, me.lastname, me.initial, me.dateofbirth, me.phone, me.email)
   MyControls = m_myContols
end property

...so with that code we now have a public array of ordered controls as a public property of the form, and we can write code against that custom property like...
CODE
    For Each ctl In me.MyControls
        with ctl
            If .Enabled And Not .Locked And .Visible And Nz(.Value, "") = "" Then
                ' The control is empty, so append to the error message
                strRequiredFieldsMissing = strRequiredFieldsMissing & vbNewLine & .controls(0).caption
            End If
        End With
    Next ctl

So that saves us checking every control on the form, so the loop is shorter, and we know exactly which controls that loop will handle, so we can run fewer tests to validate the controls in the loop.
Maybe this gives you some ideas...
This post has been edited by mklein: Mar 20 2017, 01:10 PM

--------------------
| Mark Klein | Access 2010 | Windows 10 | Visual Studio 2013
Go to the top of the page
 
ADezii
post Mar 20 2017, 02:38 PM
Post#4



Posts: 1,735
Joined: 4-February 07
From: USA, Florida, Delray Beach


  1. Here is a Demo that should clearly illustrate how you can return all the Controls on a Form in Tab Order fashion. Controls in the Header and Footer Sections will be ignored, while only Controls in the Detail Section, in this case a specific Tab (Page) will be taken into consideration. For this Demo, I returned all Controls in the General_Page Tab of the Customer Details Form of the Northwind Sample Database. I use Nested Loops to accomplish this.
    CODE
    Dim ctl As Access.Control
    Dim bytNumOfControls As Byte
    Dim bytCtr As Byte
    Dim bytTabIdx As Byte

    Debug.Print "Control Name"; Tab(25); "Tab Index"
    Debug.Print "--------------------------------------------"

    With Me
      bytNumOfControls = .Controls.Count
        For bytCtr = 0 To bytNumOfControls - 1
          For bytTabIdx = 0 To bytNumOfControls - 1
            If .Controls(bytCtr).TabIndex = bytTabIdx And _
               .Controls(bytCtr).Parent.Name = "General_Page" Then
                  Debug.Print .Controls(bytCtr).Name; Tab(25); .Controls(bytCtr).TabIndex
                    Exit For
            End If
          Next bytTabIdx
        Next bytCtr
    End With
  2. OUTPUT:
    CODE
    Control Name            Tab Index
    --------------------------------------------
    Company                  0
    First Name               1
    Last Name                2
    Job Title                3
    Business Phone           4
    Mobile Phone             5
    Fax Number               6
    Address                  7
    City                     8
    State/Province           9
    ZIP/Postal Code          10
    Country/Region           11
    Attachments              12
    E-mail Address           13
    Web Page                 14
    Notes                    15
    Text255                  16
    Command257               17

This post has been edited by ADezii: Mar 20 2017, 02:38 PM
Go to the top of the page
 
whdyck
post Mar 21 2017, 02:15 PM
Post#5



Posts: 226
Joined: 20-July 11
From: Winnipeg, MB, Canada


On my form, multiple controls have the same tabindex.

For example, if I test using this in the Immediate window
?Forms!frmUntPurQuote.cboPurchaseID.TabIndex
?Forms!frmUntPurQuote.txtUnitNo.TabIndex
both answers are 0.

How is that possible? I thought they were all distinct.

Wayne
Go to the top of the page
 
mklein
post Mar 21 2017, 02:30 PM
Post#6



Posts: 219
Joined: 7-August 12
From: BC, Canada


I think each container that hosts a controls collection will maintain it's own TabIndex, so each section of a form, and each page of a tab control will maintain a TabIndex starting at zero. To test this, add a form header and drag a control onto it. Check the TabIndex of the control. Similarly, paste a control onto a tab page and check the TabIndex. This being the case a single form--and even a single section of a form--might host many controls with a TabIndex of zero.

You can, however, expose your own ordered array of controls, or construct your own collection of controls and completely determine the order that way.

CODE
Function GetOrderedArrayOfContols()
   GetOrderedArrayOfControls = Array(Me.Field2, Me.Field1, Me.Field4, Me.Field3)
End Function

This post has been edited by mklein: Mar 21 2017, 02:38 PM

--------------------
| Mark Klein | Access 2010 | Windows 10 | Visual Studio 2013
Go to the top of the page
 
whdyck
post Mar 22 2017, 12:11 PM
Post#7



Posts: 226
Joined: 20-July 11
From: Winnipeg, MB, Canada


QUOTE
I think each container that hosts a controls collection

Thanks, you're right. Thanks for the tip.

Wayne
Go to the top of the page
 
mklein
post Mar 22 2017, 12:47 PM
Post#8



Posts: 219
Joined: 7-August 12
From: BC, Canada


You bet Wayne!

--------------------
| Mark Klein | Access 2010 | Windows 10 | Visual Studio 2013
Go to the top of the page
 


Custom Search
RSSSearch   Top   Lo-Fi    21st August 2017 - 05:13 AM