363,927 Members | 2741 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

How do I Access Elements of a UserType Using a String

kaiclubbe
P: 2
Is it possible to reference elements of a UserType indirectly with string variables?

EG.
Expand|Select|Wrap|Line Numbers
  1. Public Type myType
  2.     subTypeOne as Integer
  3.     subTypeTwo as Integer
  4.     subTypeThree as Integer
  5. End Type
  6.  
  7. Public Sub mySub
  8.     Dim myUDT as myType, indirectRef as string, myStr as string
  9.  
  10.     myStr="One"
  11.     indirectRef ="subType" & myStr
  12.     myUDT.(indirectRef) = 32
  13. End Sub
Feb 1 '12 #1
Share this Question
Share on Google+
5 Replies


ADezii
Expert 5K+
P: 7,581
To the best of my knowledge, this will never work.
Feb 1 '12 #2

kaiclubbe
P: 2
how about using collections or dictionaries?
Feb 1 '12 #3

NeoPa
Expert Mod 15k+
P: 20,505
I'm afraid the answer to your question is still No. No collection for the elements of a UserType is presented to the interface. Even the Eval() function was unable to handle the reference.
Feb 1 '12 #4

ADezii
Expert 5K+
P: 7,581
What, exactly, are you trying to accomplish?
Feb 2 '12 #5

ADezii
Expert 5K+
P: 7,581
You, yourself, brings up a very interesting point in using a Collection in order to achieve something parallel as to what you are looking for. It is not the same as a Type...End Type Construct, but it is similar. In a Collection, individual Items can be Set/Retrieved by using its Key Argument, (String) as indicated below:
Expand|Select|Wrap|Line Numbers
  1. Dim col As New Collection
  2. Dim varCol As Variant
  3. Dim intCtr As Integer
  4. Dim strItem As String
  5.  
  6. For intCtr = 1 To 100
  7.   col.Add Item:=intCtr ^ 2, Key:="Item#" & CStr(intCtr)
  8. Next
  9.  
  10. strItem = "Item#" & CStr(75)
  11.  
  12. Debug.Print "The 75th Item in the col Collection is: [" & FormatNumber(col(strItem), 0) & "]"
  13.  
  14. 'Clear the Collection
  15. For intCtr = 1 To 100
  16.   col.Remove 1
  17. Next
OUTPUT of Debug.Print
Expand|Select|Wrap|Line Numbers
  1. The 75th Item in the col Collection is: [5,625]
Feb 2 '12 #6

Post your reply

Help answer this question



Didn't find the answer to your Microsoft Access / VBA question?

You can also browse similar questions: Microsoft Access / VBA