Solved

a really cryptic problem with VB arrays.

Posted on 2003-11-06
3
194 Views
Last Modified: 2010-05-18
hi ,

i am a part-time vb user,
i know that a variant array can take in another array as each item, thus becoming "Array of Arrays"

i have a txt file. which looks like
#Aluminium
~pipes
--Some Data--
~tubes
--Some Data--
~bars
--Some Data--
#nickel
~sheet
--Some Data--
~pipes
--Some Data--
#titanium
~pipes
--Some Data--
~tubes
--Some Data--
~bars
--Some Data--
~sheets
--Some Data--

i want to store all '#' item in on array, then each item in that array is linked to the next '~' items and this in turn is connected to --Some Data--.

eg:
#Alumnium---
                    |-------->pipes------->' --Some Data--'
                                         ------->' --Some Data--'
                                         ------->' --Some Data--'
                    |-------->tubes------->' --Some Data--'
                                         ------->' --Some Data--'
                                         ------->' --Some Data--'


Please help, any strategy other than what i have thought

thanks in Advance

regards
Tenali
0
Comment
Question by:tenali_gowda
[X]
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
  • 2
3 Comments
 
LVL 9

Expert Comment

by:Dang123
ID: 9695344
You could try using a collection and base the key on what you want

eg:
Key: #Alumnium
Contents: pipes|tubes

Key: #Alumnium+pipes
Contents: 1|2|3

Key: #Alumnium+pipes+1
Contents: ' --Some Data--'

Key: #Alumnium+pipes+2
Contents: ' --Some Data--'

   .
   .
   .
0
 
LVL 9

Expert Comment

by:Dang123
ID: 9695355
Another solution may be to use the treeview control (visiable or not) and let it keep track of the relations for you.

0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 50 total points
ID: 9695840
Here is an example of how to do it with collections and have the values displayed on the form via listboxes.  There is no error checking added!  I assume you have a file called data.txt in the same directory as your saved project.  Here is what was inside my data.txt:

#Aluminium
~pipes
1
2
~tubes
3
4
~bars
5
6
#nickel
~sheet
7
8
~pipes
9
10
#titanium
~pipes
11
12
~tubes
13
14
~bars
15
16
~sheets
17
18

Create a new project and add a class module.  Add this code to the class module called Class1:

' Code to go in Class1
Dim storedString As String

Public Property Get stringValue() As String
    stringValue = storedString
End Property

Public Property Let stringValue(newString As String)
    storedString = newString
End Property

Now on your form add three listboxes in a row.  List1 will display alloys, List2 will display the parts for the selected alloy.  List3 will display the data for the selected alloy/part combination.  Here is the code to go on the form:

' Code to go on Form1
Option Explicit

Dim data As New Collection ' Holds data items
Dim alloyParts As New Collection ' Holds part names for each alloy

Private Sub Form_Load()
    Dim alloyPartsList As Collection
    Dim alloyPartData As Collection
   
    Dim curAlloy As String
    Dim curpart As String
    Dim curPartName As Class1
    Dim curDataItem As Class1
   
    Dim inputLine As String
    Dim identifier As String
   
    Dim dataKey As String
   
    curAlloy = ""
    curpart = ""

    Open App.Path & "\data.txt" For Input As #1
    Do While Not EOF(1)
        Line Input #1, inputLine
        identifier = Left(inputLine, 1)
        Select Case identifier
            Case "#"
                curAlloy = inputLine
                List1.AddItem curAlloy
                Set alloyPartsList = New Collection
                alloyParts.Add alloyPartsList, curAlloy
            Case "~"
                curpart = inputLine
                Set curPartName = New Class1
                curPartName.stringValue = curpart
                alloyPartsList.Add curPartName
                Set alloyPartData = New Collection
                dataKey = curAlloy & curpart
                data.Add alloyPartData, dataKey
            Case Else
                Set curDataItem = New Class1
                curDataItem.stringValue = inputLine
                alloyPartData.Add curDataItem
        End Select
    Loop
    Close #1
End Sub

Private Sub List1_Click()
    If List1.ListIndex <> -1 Then
        Dim curAlloyPartsCol As Variant
        Dim curPartItem As Variant
               
        ' populate second list box with part names for selected alloy
        List2.Clear
        List3.Clear
        Set curAlloyPartsCol = alloyParts.Item(List1.List(List1.ListIndex))
        For Each curPartItem In curAlloyPartsCol
            List2.AddItem curPartItem.stringValue
        Next curPartItem
    End If
End Sub

Private Sub List2_Click()
    If List1.ListIndex <> -1 And List2.ListIndex <> -1 Then
        Dim datacol As Variant
        Dim curDataItem As Variant
        Dim dataKey As String
               
        ' grab collection associated with alloy/part combo
        ' then display those items in list3
        List3.Clear
        dataKey = List1.List(List1.ListIndex) & List2.List(List2.ListIndex)
        Set datacol = data.Item(dataKey)
        For Each curDataItem In datacol
            List3.AddItem curDataItem.stringValue
        Next curDataItem
    End If
End Sub

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

737 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