2

I'm writing some code to interface with my company's online timesheet system. I would like to use Enums or something similar to store values for the websites elements to make them easier to change or access. I've tried to reference the members in these enums using a qualifier but get an invalid qualifier error message. I can reference the enums without a qualifier but then this removes my reasoning for using them. Is there any way around this?

'Add initial user
Public Enum FirstUser

    User = 47
    ID = 50
    pass1 = 56
    pass2 = 59
    logBtn = 67
    backbtn = 133
    MenuChanged = 138
    logOut = 141

End Enum

'Delete user
Public Enum delUser

    User = 21
    UserBtn = 43
    Ok = 46
    After = 58

End Enum

Sub mySub()
    dim del as delUser

    msgbox del.User

End Sub

2 Answers 2

3

When you define:

dim del as delUser

... then del can take one of the values of your Enum delUser, and thus it is like an Integer -- it does not have a structure-like data type. So this syntax -- which seems to suggest that del has member properties -- is indeed invalid:

MsgBox del.User

You can do this:

del = delUser.User ' You assign the value 47
MsgBox del

... which will show 47.

Alternative Data Structure: Collection

Maybe you were looking for something else. You could for instance use Collection:

Dim del As New Collection

del.Add 47, "User"
del.Add 50, "ID"

MsgBox del("User") ' 47

Alternative Data Structure: Class

Collections still have limitations. You could look into classes, which you could use to create (User) objects with all the properties you need.

Read about it with some examples here.

You need then to create a Class Module (separate from your sheet and workbook code), provide it a name (let's say "clsUser"), and provide the properties. A very simple one could look like this:

Private sUserName As String

Property Let UserName(name As String)
    sUserName = name
End Property

Property Get UserName() As String
    UserName = sUserName
End Property

Then in your original module, you can write things like this:

Dim u As clsUser
Dim u2 As clsUser

Set u = New clsUser()
u.UserName = "Sarah"

Set u2 = New clsUser()
u2.UserName = "John"

MsgBox u.UserName & ", " & u2.UserName
Sign up to request clarification or add additional context in comments.

2 Comments

Friendly hint to example call: Set u = New clsUser; btw the semicolon ; isn't VBA syntax for ending lines
Thanks, @T.M. Corrected. Not sure how that survived four years without comments ;-)
2

Enums are quite limited, dim del as delUser basically makes del a vanilla Long type variable (so no properties or methods, hence the error when you try to use a dot) with a "hint" to the compiler that its supposed to contain a value in the enum delUser. I say supposed to because there is no type checking whatsoever, the only real benefit is the clarity in the declaration and the intellisense support.

If you want the value of User read it from the enum itself: MsgBox delUser.User

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.