Convert 'IF THEN ELSE' to 'Select Case'

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tasawer
    New Member
    • Aug 2009
    • 106

    Convert 'IF THEN ELSE' to 'Select Case'

    Hi,

    I wish to convert my code below, to 'Select Case' but I am not having any joy, so I need help with this please.

    I have 'HomePhone' field where phone number is entered as a sequence of numbers. on the AFTERUPDATE statement, my code applies appropriate formatting for the type of number eg mobile or international etc.

    I need to add more options but 'IF THEN' becomes rather messy.

    Alternative suggestions will be considered.

    Best Regards

    Code:
    txtField = me.HomePhone
    If Mid(txtField, 1, 3) = "020" Then
        txtField = Mid(txtField, 1, 3) & "-" & Mid(txtField, 4, 4) & "-" & Mid(txtField, 8, 4)
        Else
        If Mid(txtField, 1, 2) = "00" Then
            txtField = Mid(txtField, 1, 4) & "-" & Mid(txtField, 5, 3) & "-" & Mid(txtField, 8, 3) & "-" & Mid(txtField, 11, 4)
            Else
            txtField = Mid(txtField, 1, 5) & "-" & Mid(txtField, 6, 3) & "-" & Mid(txtField, 9, 3)
            End If
        End If
  • MMcCarthy
    Recognized Expert MVP
    • Aug 2006
    • 14387

    #2
    You can't do a select case because your are looking at different parts of the Me!HomePhone field each time and not a consistant value.

    If you need to separate phone numbers by International, National, Mobile, etc then you will have to add a field to hold that value and ask the user to select the type of phone number accordingly.

    If your only concern is display then you will have to add a mask to the user input text box. The phone number would still be stored without the separators but it will display correctly.

    Comment

    • ADezii
      Recognized Expert Expert
      • Apr 2006
      • 8834

      #3
      A Select Case..End Select Construct is not feasible in your case for the reason Mary stated, but if you wish to stay the course, simply change the Syntax. You can also add as many ElseIf Statements as you like with the Else catching all non-qualifiers:
      Code:
      txtField = Me.HomePhone
      
      If Mid(txtField, 1, 3) = "020" Then
        txtField = Mid(txtField, 1, 3) & "-" & Mid(txtField, 4, 4) & "-" & _
                   Mid(txtField, 8, 4)
      ElseIf Mid(txtField, 1, 2) = "00" Then
        txtField = Mid(txtField, 1, 4) & "-" & Mid(txtField, 5, 3) & "-" & _
                   Mid(txtField, 8, 3) & "-" & Mid(txtField, 11, 4)
      Else
        txtField = Mid(txtField, 1, 5) & "-" & Mid(txtField, 6, 3) & "-" & _
                   Mid(txtField, 9, 3)
      End If

      Comment

      • tasawer
        New Member
        • Aug 2009
        • 106

        #4
        Thanks for this. I will continue with endless ElseIF's :)

        Comment

        • NeoPa
          Recognized Expert Moderator MVP
          • Oct 2006
          • 32656

          #5
          It is possible, if somewhat counter-intuitive, to use a Select Case construct in this case. You have to work at it from a different angle. The format of it is :
          Code:
          Select Case True
          Case A=1
            ...
          Case B=3
            ...
          Case Else
            ...
          End Select
          You certainly lose some of the efficiency of the underlying code in this scenario, but it does enable all the cases (including multiple cases per clause if required) to be clearly readable as a list in your code. I wouldn't recommend it in all cases, but where you have many and complicated tests, it does help to keep them organised and easier to maintain.

          Comment

          Working...