Results 1 to 7 of 7

Thread: Please help on UDT changes..

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2008
    Posts
    265

    Question Please help on UDT changes..

    Hi everyone! I'm having a little problem in applying changes to UDT like below. I really have no idea

    vb Code:
    1. Option Explicit
    2.  
    3. Private Type ProductUDT
    4.     Name         As String
    5.     Description  As String
    6. End Type
    7.  
    8. Dim Products() As ProductUDT, i As Long
    9. Dim FileLoc As String
    10.  
    11.  
    12. Private Sub cmdDelete_Click()
    13.     List1.RemoveItem List1.ListIndex
    14.        
    15.     'After the ListIndex removal, then what about the UDT??
    16.        
    17.     Form_Load  'Update list
    18. End Sub
    19.  
    20. Private Sub cmdAddNew_Click()
    21.     'What to do with the UDT changes?
    22.     '???
    23.  
    24.     Form_Load  'Update list
    25. End Sub
    26.  
    27. Private Sub Form_Load()
    28.     Dim Itm     As String
    29.     Dim Desc    As String
    30.     Dim counter As Integer
    31.    
    32.     List1.Clear
    33.     FileLoc = App.Path & "\thefile.txt"
    34.    
    35.     Open FileLoc For Input As #1
    36.          Do Until EOF(1)
    37.             counter = counter + 1
    38.             Input #1, Itm, Desc
    39.            
    40.             ReDim Preserve Products(1 To counter)
    41.            
    42.             Products(counter).Name = Itm
    43.             Products(counter).Description = Desc
    44.          Loop
    45.     Close
    46.      
    47.  
    48.     For i = LBound(Products) To UBound(Products)
    49.         List1.AddItem Products(i).Name
    50.         List1.ItemData(List1.NewIndex) = i
    51.     Next
    52.    
    53.     Me.Caption = "Total Item : " & List1.ListCount
    54. End Sub
    55.  
    56. Private Sub List1_Click()
    57.     i = List1.ItemData(List1.ListIndex)
    58.    
    59.     lblDescription.Caption = "Description: " & vbNewLine & _
    60.                               Products(i).Description
    61. End Sub

    What should I do to save/ apply the changes made. Many thanks
    Last edited by vibiEn; Aug 2nd, 2008 at 07:46 AM.

  2. #2

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2008
    Posts
    265

    Re: Please help on UDT changes..

    What I really want is just simply reload the current string from the listbox into the UDT.
    For example i have 2 items called "item1" and "item2" . Let's suppose that I delete or modify the "item2" and then unload the form. Then by the next time i reopen the form, the modified value will have a new value according to the changes I made.

    Sorry for being a bit complicated. Hope this understandable. Thanks again.

  4. #4
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Please help on UDT changes..

    I'm not sure HOW you are changing the data in List1, but try this in a new project with a Command1 button that changes one of the items.

    Code:
    Option Explicit
     
    Private Type ProductUDT
        Name         As String
        Description  As String
    End Type
     
    Dim Products() As ProductUDT, i As Long
    Dim FileLoc As String
     
     
    Private Sub cmdDelete_Click()
        List1.RemoveItem List1.ListIndex
            
        'After the ListIndex removal, then what about the UDT??
           
        Form_Load  'Update list
    End Sub
     
    Private Sub cmdAddNew_Click()
        'What to do with the UDT changes?
        '???
     
        Form_Load  'Update list
    End Sub
     
    Private Sub Command1_Click()
        List1.List(1) = "changed"
    End Sub
    
    Private Sub Form_Load()
        Dim Itm     As String
        Dim Desc    As String
        Dim counter As Integer
        Dim bDoneOnce As Boolean
        Dim strData() As String
        Dim strParts() As String
        Dim lngIndex As Long
        
        
        List1.Clear
        
        bDoneOnce = GetSetting(App.EXEName, "Application Data", "Run Once", False)
        
        If Not bDoneOnce Then
            FileLoc = App.Path & "\thefile.txt"
            
            Open FileLoc For Input As #1
                 Do Until EOF(1)
                    counter = counter + 1
                    Input #1, Itm, Desc
                    
                    ReDim Preserve Products(1 To counter)
                    
                    Products(counter).Name = Itm
                    Products(counter).Description = Desc
                 Loop
            Close
        Else
            strData = Split(GetSetting(App.EXEName, "Application Data", "Values"), "|")
            ReDim Products(UBound(strData))
            For lngIndex = 0 To UBound(strData)
                strParts = Split(strData(lngIndex), ",")
                Products(lngIndex).Name = strParts(0)
                Products(lngIndex).Description = strParts(1)
            Next
        End If
        
        For i = LBound(Products) To UBound(Products)
            List1.AddItem Products(i).Name
            List1.ItemData(List1.NewIndex) = i
        Next
        
        Me.Caption = "Total Item : " & List1.ListCount
        SaveSetting App.EXEName, "Application Data", "Run Once", True
    End Sub
     
    Private Sub Form_Unload(Cancel As Integer)
        Dim lngIndex As Long
        Dim strDataString As String
        
        For lngIndex = 0 To UBound(Products) - 1
            strDataString = strDataString & List1.List(lngIndex) & "," _
                                          & Products(lngIndex + 1).Description & "|"
        Next
        strDataString = Left$(strDataString, Len(strDataString) - 1)
        SaveSetting App.EXEName, "Application Data", "Values", strDataString
    
    End Sub
    
    Private Sub List1_Click()
        i = List1.ItemData(List1.ListIndex)
        
        lblDescription.Caption = "Description: " & vbNewLine & _
                                  Products(i).Description
    End Sub

  5. #5
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: Please help on UDT changes..

    Here is one way. Note that I couldn't test the code, but it shows you the concept and each added line is commented:
    Code:
    Option Explicit
    
    Option Explicit
     
    Private Type ProductUDT
        Name         As String
        Description  As String
    End Type
     
    Dim Products() As ProductUDT, i As Long
    Dim FileLoc As String
     
     
    Private Sub cmdDelete_Click()
        Dim lngIndex As Long, lngA As Long
        ' ensure listbox has a valid item selected
        If List1.ListIndex >= 0 Then
            ' get the index to be removed
            lngIndex = List1.ItemData(List1.ListIndex)
            List1.RemoveItem List1.ListIndex
            ' decrease all bigger indexes in the listbox
            For lngA = 0 To List1.ListCount - 1
                If List1.ItemData(lngA) > lngIndex Then List1.ItemData(lngA) = List1.ItemData(lngA) - 1
            Next lngA
            ' move data within the UDT
            For lngA = lngIndex To UBound(Products) - 1
                Products(lngA).Name = Products(lngA + 1).Name
                Products(lngA).Description = Products(lngA + 1).Description
            Next lngA
            ' remove the last item?
            If lngA > 0 Then
                ' just resize the array
                ReDim Preserve Products(lngA - 1)
            Else
                ' destroy it entirely
                Erase Products
            End If
        End If
    End Sub
     
    Private Sub cmdAddNew_Click()
        Dim lngNewIndex As Long
        ' attempt to get array pointer: (Not Not Procuts) = 0 if no data in array
        If Not Not Products Then lngNewIndex = UBound(Products) + 1
        ' create the new element
        ReDim Preserve Products(lngNewIndex)
        ' set the data in place
        With Products(lngNewIndex)
            ' just some sample data, I don't know where you would get the values
            .Name = "Name"
            .Description = "Description"
            ' add it in the listbox
            List1.AddItem .Name
            List1.ItemData(List1.NewIndex) = lngNewIndex
        End With
    End Sub
     
    Private Sub Command1_Click()
        List1.List(1) = "changed"
    End Sub
    
    Private Sub Form_Load()
        Dim Itm     As String
        Dim Desc    As String
        Dim counter As Integer
        Dim bDoneOnce As Boolean
        Dim strData() As String
        Dim strParts() As String
        Dim lngIndex As Long
        
        ' fix IDE bug related to Not ArrayName
        Debug.Assert Not strData Or App.hInstance
        
        List1.Clear
        
        bDoneOnce = GetSetting(App.EXEName, "Application Data", "Run Once", False)
        
        If Not bDoneOnce Then
            FileLoc = App.Path & "\thefile.txt"
            
            Open FileLoc For Input As #1
                 Do Until EOF(1)
                    counter = counter + 1
                    Input #1, Itm, Desc
                    
                    ReDim Preserve Products(1 To counter)
                    
                    Products(counter).Name = Itm
                    Products(counter).Description = Desc
                 Loop
            Close
        Else
            strData = Split(GetSetting(App.EXEName, "Application Data", "Values"), "|")
            ReDim Products(UBound(strData))
            For lngIndex = 0 To UBound(strData)
                strParts = Split(strData(lngIndex), ",")
                Products(lngIndex).Name = strParts(0)
                Products(lngIndex).Description = strParts(1)
            Next
        End If
        
        For i = LBound(Products) To UBound(Products)
            List1.AddItem Products(i).Name
            List1.ItemData(List1.NewIndex) = i
        Next
        
        Me.Caption = "Total Item : " & List1.ListCount
        SaveSetting App.EXEName, "Application Data", "Run Once", True
    End Sub
     
    Private Sub Form_Unload(Cancel As Integer)
        Dim lngIndex As Long
        Dim strDataString As String
        
        For lngIndex = 0 To UBound(Products) - 1
            strDataString = strDataString & List1.List(lngIndex) & "," _
                                          & Products(lngIndex + 1).Description & "|"
        Next
        strDataString = Left$(strDataString, Len(strDataString) - 1)
        SaveSetting App.EXEName, "Application Data", "Values", strDataString
    
    End Sub
    
    Private Sub List1_Click()
        i = List1.ItemData(List1.ListIndex)
        
        lblDescription.Caption = "Description: " & vbNewLine & _
                                  Products(i).Description
    End Sub

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2008
    Posts
    265

    Re: Please help on UDT changes..

    Both code show an error: Subscript out of range no.9 when vb tried to ReDim the UDT. Actually I wished for some way that will require no registry access. But It will be ok as long as I can modify the value in the UDT successfully. I've found some source that recommended to use a Collection instead. But they said that It will be slower than using UDT.
    Sorry for the confusing questions. And thanks for all your help

  7. #7
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Please help on UDT changes..

    Speed will only be an issue that must be addressed if you are handling massive volume of data, performing lots of processing on data, platform is a very old PC (e.g. 486 era or earlier), or your users are as fast as Quicksilver or Flash.

    It can be the registry... it can be a textfile... it can be a database... it can be some other data source/repository... the point is you have to persist data for next program run, so primary memory (e.g. variables) is out of the question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width