+ Reply to Thread
Results 1 to 9 of 9

Select Case

Hybrid View

  1. #1
    Forum Contributor Rick_Stanich's Avatar
    Join Date
    11-21-2005
    Location
    Ladson SC
    MS-Off Ver
    Office365
    Posts
    1,192

    Select Case

    I don't see what I am doing wrong in this code using "Select Case". When activated and the correct string is entered I should get a msgbox pop up.
         Dim sLogin As String
        Dim CapsLock As New SetCapsClass
    
        CapsLock.Value = True
        sLogin = InputBox("Enter Employee Number here!")
        If sLogin = "" Then
            MsgBox "You must enter your Employee Number!"
            sLogin = InputBox("Enter Employee Number here!")
            Select Case sLogin
            Case 1
                If sLogin = "RS61" Then
                    MsgBox "User has full READ/WRITE capabilities!"
                    sLogin = True
                End If
            Case 2
                If sLogin = "JE80" Then
                    MsgBox "User has full READ/WRITE capabilities!"
                    sLogin = True
                End If
            Case 3
                If sLogin = "BC58" Then
                    MsgBox "User has full READ/WRITE capabilities!"
                    sLogin = True
                End If
            Case 4
                If sLogin = "BF56" Then
                    MsgBox "User has full READ/WRITE capabilities!"
                    sLogin = True
                End If
            Case 5
                MsgBox "User has WRITE capabilities ONLY!"
                sLogin = False
            End Select
        End If
    Last edited by Rick_Stanich; 10-16-2008 at 10:19 AM.
    Regards

    Rick
    Win10, Office 365

  2. #2
    Forum Contributor Stuie's Avatar
    Join Date
    09-17-2006
    Location
    Suffolk, UK
    MS-Off Ver
    Excel 2003/2007
    Posts
    432
    Hi Rick_Stanich,

    When you say Case 1 that is checking the value entered into sLogin and saying if it is 1 then do the code in this block.

    same as Case 2, Case 3, etc...

    change it to this.

        Select Case sLogin
            Case "RS61"
                MsgBox "User has full READ/WRITE capabilities!"
                sLogin = True
    
            Case "JE80"
                MsgBox "User has full READ/WRITE capabilities!"
                sLogin = True
            
            Case "BC58"
                MsgBox "User has full READ/WRITE capabilities!"
                sLogin = True
    
            Case "BF56"
                MsgBox "User has full READ/WRITE capabilities!"
                sLogin = True
    
            Case 5
                MsgBox "User has WRITE capabilities ONLY!"
                sLogin = False
        End Select
    and on Case 5 change the 5 to the string your checking for
    There are only 10 types of people in the world:
    Those who understand binary, and those who don't!

  3. #3
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,488
    Your code logic does not make sense.

    For the inner test of Inputted value you would need to convert the string value to a number before the case tests will work,
            MsgBox "You must enter your Employee Number!"
            sLogin = InputBox("Enter Employee Number here!")
            Select Case CLng(sLogin)
    But if you enter 1 the first case test will be true so this code is executed.
            Select Case CLng(sLogin)
            Case 1
                If sLogin = "RS61" Then
                    MsgBox "User has full READ/WRITE capabilities!"
                    sLogin = True
                End If
    Your problem now is that we already know sLogin contains "1" so it can not possible contain "RS61".
    Cheers
    Andy
    www.andypope.info

  4. #4
    Forum Contributor Rick_Stanich's Avatar
    Join Date
    11-21-2005
    Location
    Ladson SC
    MS-Off Ver
    Office365
    Posts
    1,192
    I see, I misunderstood what "Case 1" represented. I thought it meant "Case 1" as the first event and "Case 2" as the second and so on.

    Thanks!

  5. #5
    Forum Contributor Stuie's Avatar
    Join Date
    09-17-2006
    Location
    Suffolk, UK
    MS-Off Ver
    Excel 2003/2007
    Posts
    432
    Yea no problem, easy mistake to make really

  6. #6
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    You don't need the If to test for blank entry statement either
    Option Explicit
    Option Compare Text
    Sub logIn()
     Dim sLogin As String
     'not sure what you need this for. If it setting Caps Lock then use Option Compare Text
    '    Dim CapsLock As New SetCapsClass
    '
    '    CapsLock.Value = True
        sLogin = InputBox("Enter Employee Number here!")
     
            Select Case sLogin
            Case vbNullString
            MsgBox "You must enter your employee number", vbCritical, "Input error"
            Case "RS61"
                    MsgBox "User has full READ/WRITE capabilities!"
                    sLogin = True
            Case "JE80"
                    MsgBox "User has full READ/WRITE capabilities!"
                    sLogin = True           
            Case "BC58"
                    MsgBox "User has full READ/WRITE capabilities!"
                    sLogin = True
            Case "BF56"
                    MsgBox "User has full READ/WRITE capabilities!"
                    sLogin = True
            Case Else
                MsgBox "User has WRITE capabilities ONLY!"
                sLogin = False
            End Select  
        End Sub
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  7. #7
    Forum Contributor Rick_Stanich's Avatar
    Join Date
    11-21-2005
    Location
    Ladson SC
    MS-Off Ver
    Office365
    Posts
    1,192
    One more question. :D

    I now display a msgbox when the Case is true, how do I capture when it is not true? (and show a msgbox).

    HAHA

    You answered my question before I posted, how astute!

  8. #8
    Forum Contributor Stuie's Avatar
    Join Date
    09-17-2006
    Location
    Suffolk, UK
    MS-Off Ver
    Excel 2003/2007
    Posts
    432
    not sure exactly what you mean do you mean to check if it is not a certain value like is not RS61.

    can you explain a little more??

    EDIT: Lol i was too late

  9. #9
    Forum Contributor Rick_Stanich's Avatar
    Join Date
    11-21-2005
    Location
    Ladson SC
    MS-Off Ver
    Office365
    Posts
    1,192
    Case closed!

    hehe

    I kill me!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Select Case Error Handler?
    By hutch@edge.net in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-17-2008, 03:29 PM
  2. Numbers to Text
    By sachinattri in forum Excel General
    Replies: 6
    Last Post: 06-15-2008, 03:07 AM
  3. Coding in visual basic
    By jexy in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 02-06-2007, 08:01 AM
  4. VBA code - color format routine
    By xlwho in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-06-2006, 05:10 AM
  5. type mismatch
    By Mikeyhend in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-16-2006, 01:17 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1