|
-
Aug 2nd, 2008, 07:34 AM
#1
Thread Starter
Hyperactive Member
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:
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 Form_Load()
Dim Itm As String
Dim Desc As String
Dim counter As Integer
List1.Clear
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
For i = LBound(Products) To UBound(Products)
List1.AddItem Products(i).Name
List1.ItemData(List1.NewIndex) = i
Next
Me.Caption = "Total Item : " & List1.ListCount
End Sub
Private Sub List1_Click()
i = List1.ItemData(List1.ListIndex)
lblDescription.Caption = "Description: " & vbNewLine & _
Products(i).Description
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.
-
Aug 2nd, 2008, 09:11 AM
#2
Re: Please help on UDT changes..
Do you mean when you say "apply the changes made"? Please explain further.
-
Aug 2nd, 2008, 10:44 AM
#3
Thread Starter
Hyperactive Member
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.
-
Aug 2nd, 2008, 11:37 AM
#4
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
-
Aug 2nd, 2008, 12:58 PM
#5
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
-
Aug 7th, 2008, 08:58 PM
#6
Thread Starter
Hyperactive Member
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
-
Aug 8th, 2008, 05:30 AM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|