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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reference a user-defined type field with a variable

Status
Not open for further replies.

jflo

Programmer
Mar 13, 2001
44
0
0
CA
Using A97, I've created a user-defined type that contains the informations from a medical information form. The type has information for about 50 questions and looks a bit like this:

Public Type MedicalQ
intYN1 As Integer
str1 As String
intYN2 As Integer
str2 As String
...
intYN7 As Integer
str7 As String
strDate7 As String
strDetails7 As String
...
End Type

The type works fine but I need to reference some of the fields in loops with variable. Lets say I have Dim myMedical As MedicalQ. I want to be able to reference somethig like myMedical.(<a string variable equal to str7>) = &quot;Last june I had my prostate checked!&quot;


 
Hey, how does it feel to have your prostate checked? :))

I had the same problem than you a while ago and the only solution I found is to do a select case with the variable.

Like : Select case strVar
case is &quot;str7&quot;
myMedical.str7
etc.

I know this is not very optimal but I don't think there is any other way to do it.
 
I'm late answering this and not sure whether you consider your question answered. Have you defined your Type in a module or class? Is the attempt to use your variable in the same module or class? Do you have a syntax error in a line not related to the user defined type to prevent Intellisense from functioning (Compile the code)?

I might also suggest you use an array rather than a user defined type with 50 different items. It reduces the hard coding and allows any number of questions.

Public Type MedicalQ
QuestionNbr As Integer
Answer As String
End Type
Dim mMyMedical() As MedicalQ

Public Sub As NewQuestion(QNbr As Integer, QAnswer As String)
Dim i As Integer
i = UBound(mMyMedical)
i = i + 1
ReDim Preserve mMyMedical(i)
End Sub

Public Sub ShowAllQuestions
Dim i As Integer
For i = 0 to UBound(mMyMedical)
Debug.Print &quot;Nbr: &quot; & mMyMedical(i).QNbr & &quot; &quot; _
& &quot;Answer: &quot; & mMyMedical(i).QAnswer
Next i
End Sub


----------------------
scking@arinc.com
Life is filled with lessons.
We are responsible for the
results of the quizzes.
-----------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top