Is it possible to use variable to specify key in user defined types?

Posted on 2003-03-20
Medium Priority
Last Modified: 2010-05-01
Is it possible to use a variable to specify a .Key for a user defined type?

For instance, if i define this:
Private Type myType
   lName as String * 20
   fName as String * 20
   pAge  as string * 3
End Type
Dim myRecord as myType

I know I can access the keys like this:
With myRecord
  .lName = "Last Name"
  .fName = "First Name"
  .pAge  = 23
End With

But is it possible to specify the keys ( .lName, .fName, .pAge ) via variables?
Dim lKey as String, fKey as String, pKey as String
lKey = ".lName"
fKey = ".fName"
pKey = ".pAge"

With myRecord
   lKey = "Last Name"
   fKey = "First Name"
   pKey = 23
End With
or maybe something like
myRecord(lKey) = "Last Name"

Or is there maybe some other way to specify the keys via referencing an index or something?
Question by:salinasj
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 11

Expert Comment

ID: 8179224
Not that I know of.

With myRecord
  lKey = "Last Name"
  fKey = "First Name"
  pKey = 23
End With

this will assign individual values to those variables.

myRecord(lKey) = "Last Name"

this is not a valid as well. I tried it VB.  I am interested in the outcome of this qustion as well.

Expert Comment

ID: 8179450
No. but you can do the following:

Private Type myType
  lName as String * 20
  fName as String * 20
  pAge  as string * 3
End Type

Dim myRecord() as myType

redim myRecord(50)
myRecord(0).lName="Last Name"

myRecord(AVariableThatHoldIndex).lName="Last Name"

Accepted Solution

Rubyn earned 500 total points
ID: 8179723

Private Type MyType
    Key As String
    FName As String
    LName As String
    Age As Long
End Type

'Holds UDTS
Private m_MyTypes() As MyType

'Returns the count of UDT created
Private Property Get MyTypeCount() As Long
On Error Resume Next
    MyTypeCount = UBound(m_MyTypes) - LBound(m_MyTypes) + 1&
End Property

'Returns the index of UDT (Search UDTs with key)
Private Property Get MyTypeGetIndex(Key As String) As Long
Dim Index As Long
    For Index = 0 To MyTypeCount - 1&
        If m_MyTypes(Index).Key = Key Then MyTypeGetIndex = Index: Exit Property
    Let MyTypeGetIndex = -1
End Property

'Reads UDT
Private Property Get MyTypes(Key As String) As MyType
Dim Index As Long
    Let Index = MyTypeGetIndex(Key)
    If Not Index = -1 Then
        MyTypes = m_MyTypes(Index)
        Call Err.Raise(Number:=1, Description:="Key Not Found")
    End If
End Property

'Writes UDT
Private Property Let MyTypes(Key As String, MyType As MyType)
Dim Index As Long
    Let Index = MyTypeGetIndex(Key)
    If Index = -1 Then
        Let Index = MyTypeCount
        ReDim Preserve m_MyTypes(Index)
        Let MyType.Key = Key
    End If
    Let m_MyTypes(Index) = MyType
End Property

Private Sub Form_Load()
Dim MyType As MyType
    Let MyType.FName = "First"
    Let MyType.LName = "Last"
    Let MyType.Age = 10
'Save UDT with key
    Let MyTypes("Any Key") = MyType
'Load UDT with key
    Let MyType = MyTypes("Any Key")
'Access UDT variables directly also
    Call MsgBox(MyTypes("Any Key").FName)
End Sub

Without using classes and collection object. we can read and write with key string.
I think this will be usefull for all. :)

Author Comment

ID: 8179817


Expert Comment

ID: 8179925
create a collection:
Private mCol As Collection

Public Function Add(iname As String, fname As String, page as string,Optional sKey As String) As mytype
    'créer un nouvel objet
    Dim objNewMember As mytype
    objNewMember.iname = NOM
    If Len(sKey) = 0 Then
        mCol.Add objNewMember
        mCol.Add objNewMember, sKey
    End If

    'renvoyer l'objet créé
     Add = objNewMember

End Function

i didn't test it, i use object in my collection

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month9 days, 4 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question