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

How to put and access different types of data easily in an array?

Access Version or Conversion

How to put and access different types of data easily in an array?

by  vincentmairiaux  Posted    (Edited  )
Unfortunately, there is no such a thing as a variable type called "InputDataFromMyTestSessionOnThisParticularCase".
But you can create it. Nice? Easy? Yup:

Create your new type of variable, called e.g. TestResult (or anything you want), composed of several conventional variable types proposed by VB6. Once this new variable is created, you can declare an array of this new type of variant. Each component inside the new variable can be accessed separately (read from and write to it).

For an example, do exactly as described:

1. create a new project in VB6
2. click on project in the menu bar, select Add Module
3. double click on this new module to open it
4. copy and paste the following code between ***** lines into the module:

Code:
'********************
'first declare the new type of variable:
Type TestResult
    Today As Date
    TimeOfTest As String * 8 'rem: means 8 characters in each string
    Var1 As Single 'or Integer if ther are no decimals
    Var2 As Single
    Var3 As Single 'even more "Varx" as you need them
End Type


'then declare the array made of your new type of variable:
Public TestResults(100) As TestResult
'*********************

5. get back to the form in your project
6. place a humble command button on your form, called Command1 (what a humor!)
7. double click on it, and copy and paste the following code (also between the ****** lines:

Code:
'*********************
Private Sub Command1_Click()
    TestResults(50).Var1 = 5.2
    Debug.Print TestResults(50).Var1
    Debug.Print TestResults(50).Var2
End Sub
'*********************

8. run the application (press F5)
9. click on the button on the screen, and stop the application
10. look at the debug-window: you should read 5,2 (result of TestResults(50).Var1 containing that value) and 0 (result of TestResults(50).Var2 which is still empty).

Understood how it works? You can put any type of VB variables inside your own new variable. Further inside your code, separate the new variable name by a dot (.) to its component in order to access one of its component.

If you don't know how many items you will need to put in your array, declare a dynamic array and apply in your code the "Redim" method or the "Redim Preserve" method to e.g. increment the array before writing to it.

If any problem, email me.
Good luck.

Vincent.

reach me at vincent.mairiaux@yucom.be
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top