Results 1 to 5 of 5
  1. #1
    shylock is offline Competent Performer
    Windows 7 64bit Access 2013 64bit
    Join Date
    Sep 2018
    Location
    Dayton, Ohio
    Posts
    100

    Select Case using Tab Index

    I have a form with, say, 5 controls. I want to use a Select Case statement using the tab index of the controls to check for valid data in each control before saving the data to a table. How do I reference the tab index in the Select Case? Is the following correct?



    Select Case me!controls.TabIndex
    Case 0
    Case 1
    Case 2
    Case 3
    Case 4
    Case Else
    End Select

    Thanks, in advance for your help.

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    You want to check all the controls? Typically you'd use a For/Next loop, either using the Tag property to limit the check to the appropriate controls, or the type of control. Even if you used the tab index, you'd have to do it within a loop. What you have wouldn't work. The loop looks like:

    Code:
      Dim ctl                     As Control
    
      For Each ctl In Me.Controls
        'do your thing
      Next ctl
    so within that loop you could have your Select/Case, testing ctl.TabIndex.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    Join Date
    Jun 2010
    Location
    Belgium
    Posts
    1,035
    Hi,

    testing the tabindex is a bit tricky as it could change when you add a new control on your form. Why not use the tag property? It is a property you can use at your own convenience and won't change until you yourself change it on purpose in the design environment or in code.

  4. #4
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,643
    Here's a public function I use to check that required fields are not empty. I'm guessing you want to use select case as each field may have a different kind of validation.
    You'll notice in the code below that I test the tags text using instr(). I do this so I can use the tag property in more than one procedure but you could also use this to differentiate what validation to do on that control.

    Code:
    Public Function frmValid(frm As Form) As Boolean
    
    
        Dim ctl As Variant
        Dim flg As Boolean
        Dim strMsg As String
    
    
        flg = True
    
    
        For Each ctl In frm.Controls
    
    
            If InStr(1, ctl.Tag, "V8") Then
    
    
                If Nz(ctl, "") = "" Then
    
    
                    flg = False
    
    
                    ctl.BorderColor = vbRed
    
    
                    strMsg = strMsg & Space(20) & "* " & ctl.Controls.Item(0).Caption & vbNewLine
    
    
                Else
    
    
                    ctl.BorderColor = vbBlack
    
    
                End If
    
    
            End If
    
    
        Next ctl
    
    
        frmValid = flg
    
    
        If flg = False Then
    
    
            MsgBox "The following item(s) are required:" & vbNewLine & vbNewLine & strMsg
    
    
        End If
    
    
    End Function
    HTH

  5. #5
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,820
    If you don't want code to cycle through all controls (there could be hundreds), give your 5 target controls similar names, like: Data1, Data2, etc.

    Then a loop could be:

    Code:
    For x = 1 to 5
        If Me("Data" & x) & "" = "" Then
            'do something
        Else
            'do this
        End If
    Next
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Pl sql case select
    By mrmmickle1 in forum Queries
    Replies: 1
    Last Post: 11-17-2015, 11:14 PM
  2. Select Case Error
    By LaughingBull in forum Access
    Replies: 13
    Last Post: 09-11-2015, 02:05 PM
  3. Should I /can I use a Select Case statement?
    By Gina Maylone in forum Access
    Replies: 1
    Last Post: 12-13-2014, 12:08 PM
  4. Select case help
    By killermonkey in forum Programming
    Replies: 7
    Last Post: 10-25-2013, 05:09 PM
  5. select case problem
    By Mclaren in forum Programming
    Replies: 3
    Last Post: 11-17-2011, 01:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums