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

AGH cant find Item under Collection, nor can I find index # for curent

Status
Not open for further replies.

Karl Blessing

Programmer
Feb 25, 2000
2,936
0
0
US
I got a collection, I got a listbox keeping track of it, I cannot do Collection(&quot;Name To Find&quot;) nor Collection.Item(&quot;Name to Find&quot;)<br>
I cant search those<br>
and I Cant figure out how to get the Index once I find the Item I want to remove. or even work with <p>Karl<br><a href=mailto:kb244@kb244.8m.com>kb244@kb244.8m.com</a><br><a href= </a><br>Experienced in , or have messed with : VC++, Borland C++ Builder, VJ++6(starting),VB-Dos, VB1 thru VB6, Delphi 3 pro, Borland C++ 3(DOS), Borland C++ 4.5, HTML, ASP(somewhat), QBasic(least i didnt start with COBOL)
 
Update: so far this seems to work<br>
<br>
Public Sub SicShape(Name As String, ShapeType As String, SIC As String, Desc As String, employers As String)<br>
On Error GoTo SError<br>
Dim NewSh As New Shapes<br>
Dim NewS2 As New Sic2<br>
Dim NewS4 As New Sic4<br>
Dim idx As Integer<br>
Dim sicid As String<br>
Dim bExists As Boolean<br>
Dim objSicStuff As Object<br>
Set objSicStuff = CreateObject(&quot;wiDataAccess.Dataconnection&quot;)<br>
<br>
objSicStuff.ConnectionString = &quot;dsn=siccode;uid=sa;pwd;&quot;<br>
objSicStuff.Connect<br>
<br>
If Desc = &quot;&quot; Then<br>
Desc = objSicStuff.GetSicTitle(SIC)<br>
End If<br>
<br>
'Dont know if this next line will work<br>
bExists = True<br>
Set NewSh = Shapes.Item(Name)<br>
sicid = Trim(SIC)<br>
If Len(sicid) = 2 Then<br>
NewS2.SicDesc = Desc<br>
NewS2.SicCode = sicid<br>
NewS2.total = 0<br>
<br>
Set rs = objSicStuff.GetSic4Titles(sicid)<br>
Do While Not rs.EOF<br>
NewS4.SicCode = rs!SicCode<br>
NewS4.SicDesc = rs!sictitle<br>
NewS2.Sic4.Add NewS4, rs!SicCode<br>
NewS2.total = NewS2.total + 1<br>
Set NewS4 = New Sic4<br>
rs.movenext<br>
Loop<br>
NewSh.Sic2.Add NewS2, sicid<br>
<br>
Else<br>
NewS4.SicCode = sicid<br>
NewS4.SicDesc = Desc<br>
NewSh.Sic4.Add NewS4, sicid<br>
End If<br>
If bExists = False Then<br>
NewSh.Name = Name<br>
Shapes.Add NewSh, Name<br>
End If<br>
<br>
Exit Sub<br>
SError:<br>
If Err.Number = 5 Then<br>
bExists = False<br>
End If<br>
Err.Clear<br>
Resume Next<br>
Exit Sub<br>
<br>
End Sub <p>Karl<br><a href=mailto:kb244@kb244.8m.com>kb244@kb244.8m.com</a><br><a href= </a><br>Experienced in , or have messed with : VC++, Borland C++ Builder, VJ++6(starting),VB-Dos, VB1 thru VB6, Delphi 3 pro, Borland C++ 3(DOS), Borland C++ 4.5, HTML, ASP(somewhat), QBasic(least i didnt start with COBOL)
 
<br>
Karl,<br>
<br>
The basic problem is that you must know the index to each item that you add to the collection -- you have 2 options:<br>
<br>
Option 1:<br>
<br>
When you add a name to the collection, use the same name as the key to that item. For example,<br>
<br>
col.Add &quot;Name 1&quot;, &quot;Name 1&quot;<br>
<br>
Then when you can use col(List1) or col.Item(List1) to retrieve the collection item.<br>
<br>
Option 2:<br>
<br>
As you add an item to the collection, add the same item to the list box. Then when the user selects an item from the listbox, use the index of the listbox item that was selected to retrieve the collection item. For example,<br>
<br>
c.Add &quot;Item 1&quot;<br>
List1.AddItem &quot;Item 1&quot;<br>
<br>
Then you can use c.Item(list1.ListIndex + 1) to retrieve the collection item.<br>
<br>
Hope this helps!
 
Here's another approach:<br>
Use the itemdata property to store the index for each item you've added to the collection, and the newindex property to get the index of the last item added.<br>
<br>
Dim itemname as string<br>
Dim c as collection<br>
Dim i as integer<br>
<br>
for i = 1 to 10<br>
&nbsp;&nbsp;&nbsp;&nbsp;itemname = &lt;assign your itemname values here&gt;<br>
&nbsp;&nbsp;&nbsp;&nbsp;c.add Itemname<br>
&nbsp;&nbsp;&nbsp;&nbsp;list1.additem Itemname <br>
&nbsp;&nbsp;&nbsp;&nbsp;list1.itemdata(list1.newindex) = i<br>
next<br>
<br>
<br>
then you can retrieve the values by doing<br>
<br>
c.item(list1.itemdata(list1.listindex))<br>
<br>
the benefit of doing it this way is that it will work with a sorted list box.<br>
<br>
<br>
<p>nick bulka<br><a href=mailto:nick@bulka.com>nick@bulka.com</a><br><a href= > </a><br>
 
Thanks for your responces tho, but I've already figured out you have to assing the key as well, and btw heres the code so far in SicCodes.cls, with the exception that some properties has been added to other collections.<br>
(also i cant have Lists involded in my code, must be completely self contained, now just to figure out how to export the collection)<br>
<br>
Public Shapes As New Collection<br>
<br>
Public Sub SicShape(Name As String, ShapeType As String, Sic As String, Desc As String, employers As String)<br>
' Add entire Shape's Sic code tree, if Sic2 was added under shape, then all Sic4s that follow it are added too<br>
On Error GoTo SError<br>
Dim NewSh As New Shapes<br>
Dim NewS2 As New Sic2<br>
Dim NewS4 As New Sic4<br>
Dim idx As Integer<br>
Dim sicid As String<br>
Dim bExists As Boolean<br>
Dim objSicStuff As Object<br>
Set objSicStuff = CreateObject(&quot;wiDataAccess.Dataconnection&quot;)<br>
<br>
objSicStuff.ConnectionString = &quot;dsn=siccode;uid=sa;pwd;&quot;<br>
objSicStuff.Connect<br>
<br>
If Desc = &quot;&quot; Then<br>
Desc = objSicStuff.GetSicTitle(Sic)<br>
End If<br>
<br>
'Dont know if this next line will work<br>
bExists = True<br>
Set NewSh = Shapes.Item(Name)<br>
sicid = Trim(Sic)<br>
If Len(sicid) = 2 Then<br>
NewS2.SicDesc = Desc<br>
NewS2.SicCode = sicid<br>
NewS2.total = 0<br>
<br>
Set rs = objSicStuff.GetSic4Titles(sicid)<br>
Do While Not rs.EOF<br>
NewS4.SicCode = rs!SicCode<br>
NewS4.SicDesc = rs!sictitle<br>
NewS2.Sic4.Add NewS4, rs!SicCode<br>
NewS2.total = NewS2.total + 1<br>
Set NewS4 = New Sic4<br>
rs.movenext<br>
Loop<br>
NewS2.SEmploy = employers<br>
NewSh.Sic2.Add NewS2, sicid<br>
<br>
Else<br>
NewS4.SicCode = sicid<br>
NewS4.SicDesc = Desc<br>
NewS4.SEmploy = employer<br>
NewSh.Sic4.Add NewS4, sicid<br>
End If<br>
If bExists = False Then<br>
NewSh.Name = Name<br>
NewSh.SType = ShapeType<br>
NewSh.SDesc = Desc<br>
Shapes.Add NewSh, Name<br>
End If<br>
<br>
Exit Sub<br>
SError:<br>
If Err.Number = 5 Then<br>
Debug.Print Err.Description + &quot; occured in SicShape&quot;<br>
bExists = False<br>
End If<br>
Err.Clear<br>
Resume Next<br>
Exit Sub<br>
<br>
End Sub<br>
<br>
Public Sub ShapeRemove(Name As String)<br>
On Error GoTo DError<br>
Shapes.Remove (Name)<br>
Exit Sub<br>
DError:<br>
If Err.Number = 5 Then<br>
Debug.Print Err.Description + &quot; Occured in Shape Remove&quot;<br>
Err.Clear<br>
Resume Next<br>
End If<br>
Debug.Print &quot;Unknown Error: &quot; + Err.Number + &quot; &quot; + Err.Description + &quot; in Shape Remove&quot;<br>
End Sub<br>
<br>
Public Sub ShapeClear()<br>
Dim Num As Integer<br>
For Num = 1 To Shapes.Count<br>
Shapes.Remove 1<br>
Next Num<br>
<br>
End Sub<br>
<br>
Public Sub ShapeChange(OldName As String, NewName As String, SicCode As String)<br>
On Error GoTo CError<br>
Dim OldSH As Shapes<br>
Dim OldS2 As Sic2<br>
Dim OldS4 As Sic4<br>
Dim NewSh As New Shapes<br>
Dim Exists As Boolean<br>
<br>
Exists = True<br>
Set OldSH = Shapes.Item(OldName)<br>
If Exists = False Then<br>
Exit Sub<br>
End If<br>
For Each OldS2 In OldSH.Sic2<br>
NewSh.Sic2.Add OldS2<br>
Next<br>
For Each OldS4 In OldSH.Sic4<br>
NewSh.Sic4.Add OldS4<br>
Next<br>
NewSh.Name = NewName<br>
Shapes.Add NewSh, NewName<br>
Shapes.Remove OldName<br>
Exit Sub<br>
CError:<br>
Debug.Print Err.Description + &quot; occured in ShapeChange&quot;<br>
Exists = False<br>
Err.Clear<br>
Resume Next<br>
End Sub<br>
<br>
Sub SRemoveSic(Shape As String, Sic As String)<br>
On Error GoTo SRError<br>
Dim SicSH As Shapes<br>
Sic = Trim(Sic)<br>
Dim Exists As Boolean<br>
Exists = True<br>
Set SicSH = Shapes.Item(Shape)<br>
If Exists = False Then Exit Sub<br>
If Len(Sic) = 2 Then<br>
SicSH.Sic2.Remove Sic<br>
Else<br>
SicSH.Sic4.Remove Sic<br>
End If<br>
<br>
SRError:<br>
Debug.Print Err.Description + &quot; occured in SicRemove&quot;<br>
Exists = False<br>
Err.Clear<br>
Resume Next<br>
End Sub<br>
<br>
<p>Karl<br><a href=mailto:kb244@kb244.8m.com>kb244@kb244.8m.com</a><br><a href= </a><br>Experienced in , or have messed with : VC++, Borland C++ Builder, VJ++6(starting),VB-Dos, VB1 thru VB6, Delphi 3 pro, Borland C++ 3(DOS), Borland C++ 4.5, HTML, ASP(somewhat), QBasic(least i didnt start with COBOL)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top