Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 429
  • Last Modified:

Refer to user-defined type elements via variables?

I have a user-defined type:

Public Type TaxRecord
  Tax1 as Long
  Tax2 as Long
  Tax3 as Long
  Tax4 as Long
    ...
  Tax100 as Long
End Type

If I create a variable of type TaxRecord, such as:

Dim MyTax as TaxRecord

How can I refer to the ELEMENTS of the UDT via variables - or can I do that at all?

I would like to be able to do something like this:

For ctr = 1 to 100
  varElement = "MyTax.Tax" & ctr
  Debug.Print Eval(varElement)
Next ctr

But that does not work for me - I get an error "Microsoft Access cannot find the name MyTax".  In my experience with Access, Eval() rarely works at all for anything.

Is there any way I can refer to the elements of a UDT via variables?

Thanks in advance for your help.
0
smiley_strat
Asked:
smiley_strat
  • 3
  • 3
  • 2
1 Solution
 
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
You might try this:

varElement = MyTax.Tax & Cstr(ctr)
0
 
smiley_stratAuthor Commented:
Changing the ctr to a string did not work.  The Eval() still comes back with "Microsoft Office Access cannot find the name 'MyTax' you entered in the expression"

Yet MyTax is absolutely defined as being of type TaxRecord and I can refer to it elsewhere in the code with no problems.
0
 
Patrick MatthewsCommented:
Hello smiley_strat,

Instead of a user defined type, why not just use an array dimensioned 1 to 100?  Then you can refer to any
given element rather easily by passing the index...

Regards,

Patrick
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
What is your overall goal of using this? Perhaps there is an easier way, as Patrick has pointed out.
0
 
smiley_stratAuthor Commented:
The Tax1, Tax2, .... TaxN fields are actually only part of the UDT I'm trying to work with.  The UDT matches fields in a table, and in order to streamline the I/O process I am attempting to use the UDT.

I know that using a Recordset object would allow me to use the Fields collection, in which case I could refer to .Fields("Tax" & ctr) and that would work fine.

However, I inherited this code to some extent and am trying to make use of the UDTs, if possible.
0
 
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
I see.

I've always used UDTs as nothing more that a data structure within a class, but I'm sure others have different experiences with them. I would be surprised if you could access them dynamically, as you're trying to do, but I could be wrong. A UDT doesn't expose an "item" collection, so I don't see any way to do it, but then I've never really gotten that deep into them.
0
 
Patrick MatthewsCommented:
smiley_strat,

I've never been a fan of UDTs.  You can usually accomplish the same thing with classes.

In any event, if the UDT contains other members beside Tax1, Tax2, ..., TaxN, could you recast it so that one
member of the UDT is an array containing Tax1, Tax2, ..., TaxN?

Patrick
0
 
smiley_stratAuthor Commented:
matthewspatrick: That sounds like a workable idea, and unless it turns out that there really is a way to refer to the elements of a UDT variably (though it looks like that just isn't possible) I will use your suggestion as the solution.  But I'll leave the question open a little longer to see if any other potential solutions come along.
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now