Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Enum long value from string

Status
Not open for further replies.

redapples

Technical User
May 1, 2003
215
GB
I have defined an enum that I need to catch from string input

Code:
Private Enum logins
    cmsadmin = 0
    clifton = 1
    margaret = 2
    craig = 3
    summer = 4
    cygent = 5
    stopover = 6
    wernham = 7
    fonthill = 8
    css = 9
    trinity = 10
    urquhart = 11
    icds = 12
    admin = 13 'here only for testing
End Enum
What I am attempting to do is catch the CurrentUser and assign a number value to them I figured this would be the easiest way (not sure now) or a least a way to cut down my code.

I can referrence the enum by using logins.css for example to return 9 but how do I use my input string to select an enum value?

Code:
dim e as logins
e = logins.CurrentUser() 'does not work
how do I get the quotes of a string for example I believe in VB there is the [enum].parse(typeofenum, value) method but not it wopuld seem in VBA.

any help welcome of course.

redapples

Want the best answers? See FAQ181-2886

 
Why not use test$ = environ.username, this will pick up the user's name and assign it to test$.
 
As far as I can see, you can't use Enum for this as it is acting like a list of constants

The way to use it in VBA is

msgbox Logins.clifton

which would show a message box with a 1 in it

I have yet to find a way of associating the Enum with a variable--This won't work:
myVar = environ("username")
msgbox Logins.myVar



Rgds, Geoff

Never test the depth of water with both feet

Help us to help you by reading FAQ222-2244 before you ask a question
 
molby - fine assign it to a string but how then to get the enum value from that string?

xlbo - me either, so I gave up and used a collection instead, though I am not sure that there is any advantage to that over writing a lengthy Select Case statement which is what I was trying to avoid.

Ho hum, looks prettier though :)

redapples

Want the best answers? See FAQ181-2886

 
You could have an array of strings with the user names then write a function to run through it and return the index number that matches. Something like this (I've assumed you're using Option Base 1, lngElements holds the number of elements in your array and aryLogins is the array of login names):

Code:
Option Explicit
Option Base 1

Dim lngElements As Long
Dim aryLogins() As String

Public Sub Start()
    
    ' Dummy routine to set things up
    
    lngElements = 3
    ReDim aryLogins(lngElements)
    
    aryLogins(1) = "Jim"
    aryLogins(2) = "Bob"
    aryLogins(3) = "Fred"
    
    MsgBox GetIndex("Bob")
    
End Sub

Public Function GetIndex(strSearch As String) As Long
    
    ' Inefficient function to find index number
    ' -1 = error
    
    Dim lngLoop As Long
    Dim lngIndex As Long
    
    lngIndex = -1
    
    For lngLoop = 1 To lngElements
        
        If UCase(aryLogins(lngLoop)) = UCase(strSearch) Then
            
            lngIndex = lngLoop
            
        End If
        
    Next lngLoop
    
    GetIndex = lngIndex
    
End Function

Amongst other things it needs re-writing with a 'while' loop instead of a 'for' but hopefully it'll get you started.

Nelviticus
 
As I say I used a collection much like you array function

Code:
Option Compare Database
Option Explicit
Public loginsCollection As New Collection
Public log As New Classlogin 'classlogin a classmodule declares loginvalue as an int

Public Sub populateLogins()

    log.LoginValue = 0
    loginsCollection.Add Item:=log, Key:="cmsAdmin"
    log.LoginValue = 1
    loginsCollection.Add Item:=log, Key:="clifton"
    log.LoginValue = 2
    loginsCollection.Add Item:=log, Key:="margaret"
    log.LoginValue = 3
    loginsCollection.Add Item:=log, Key:="craig"
    log.LoginValue = 4
    loginsCollection.Add Item:=log, Key:="summer"
    log.LoginValue = 5
    loginsCollection.Add Item:=log, Key:="cygent"
    log.LoginValue = 6
    loginsCollection.Add Item:=log, Key:="stopover"
    log.LoginValue = 7
    loginsCollection.Add Item:=log, Key:="wernham"
    log.LoginValue = 8
    loginsCollection.Add Item:=log, Key:="fonthill"
    log.LoginValue = 9
    loginsCollection.Add Item:=log, Key:="css"
    log.LoginValue = 10
    loginsCollection.Add Item:=log, Key:="trinity"
    log.LoginValue = 11
    loginsCollection.Add Item:=log, Key:="urquhart"
    log.LoginValue = 12
    loginsCollection.Add Item:=log, Key:="icds"
    log.LoginValue = 13
    loginsCollection.Add Item:=log, Key:="admin"

End Sub

Private Sub Form_Load()
    populateLogins
    strLogin = LCase(CurrentUser())
    msgbox loginsCollection(strLogin).LoginValue
End Sub

probably should have posted this 1st time I answered

Redapples

Want the best answers? See FAQ181-2886

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top