Solved

a really cryptic problem with VB arrays.

Posted on 2003-11-06
3
189 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
  • 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 85

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

706 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now