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

Collections to Labels

Status
Not open for further replies.

meintsi

Technical User
Jul 22, 2002
181
US
This is my first attempt at a vb program.

After reading external data (each read can have a different number of strings in it) and adding them to a collection, I'm having a problem with the syntax to "title" a series of labels in a form from the collected data.

Here is where I am having difficulty.

' How many configurations do we have?
numConfigs = Part.GetConfigurationCount()
frmConfigSpecific.lblNumConfigs.Caption = numConfigs & " Configurations Found"

' Store the names of the configurations
Names = Part.GetConfigurationNames()
For i = 0 To (numConfigs - 1)
ConfigNames.Add (Names)
Next i

'For Each i In Names
' frmConfigSpecific.lblConfig Str(i) = i
' If i = numConfigs Then Exit For
' End If
'Next i



Also, is it possible here to somehow define how many label boxes appear in my 'frmConfigSpecific' ?
*Remember.......
If you don't use your head,
your going to have to use your feet.
 
I think the problem is here
For Each i In Names
frmConfigSpecific.lblConfig Str(i) = i
If i = numConfigs Then Exit For
End If
Next i

The "i" you are using is an object in your collection, and you are then using it as an index to your labels. To do this I would imagine you would use

Code:
Dim i as integer

For i = 1 To ConfigNames.Count
  frmConfigSpecific.lblConfig(i) = ConfigNames.Item(i)
Next i

When you say ", is it possible here to somehow define how many label boxes appear in my 'frmConfigSpecific'"
do you mean how can you create labels at runtime so that you have one for each item in your collection?

If so you could do that as follows:
Make sure that there is one lblConfig on your form and set its Index to 0.

In your loop which sets the label caption you can create the label before setting it as follows:
Code:
Dim i as integer

For i = 1 To ConfigNames.Count
  Load frmConfigSpecific.lblConfig(i) 'Create a label
  frmConfigSpecific.lblConfig(i) = ConfigNames.Item(i)
  frmConfigSpecific.lblConfig(i).Top = i * 200 'Or whatever
  frmConfigSpecific.lblConfig(i).left = 100    'Or whatever
  frmConfigSpecific.lblConfig(i).Visible = True

Let me know if this is what you were after and if it works ok :)

Daren
Must think of a witty signature
 
Darren,

Using your suggestion I get the following error:

Compile Error!
Method or Data Member not Found

with the following(shown in bold) highlighted

Dim i as integer

For i = 1 To ConfigNames.Count
frmConfigSpecific.lblConfig(i) = ConfigNames.Item(i)
Next i

Now my question is, the labels I am trying to fill are named
lblConfig1, lblConfig2, lblConfig3 and lblConfig4
(In order to iron out this step my form has these four labels predefined)

Do I need to rename these in order for this method to work?

In response to the second part, yes, that is what i am wanting to do but first I was trying to get the configuration names into the labels. *Remember.......
If you don't use your head,
your going to have to use your feet.
 
Your labels need to be an array of labels.

Add a label to the form, name it lblConfig then copy and paste the label to the form. VB asks if you want to make an array - answer yes. Then paste twice more and grag the labels where you want them. They are then named lblConfig(0) to lblConfig(3) and you're done Let me know if this helps
________________________________________________________________
If you are worried about how to post, please check out FAQ222-2244 first

'There are 10 kinds of people in the world: those who understand binary, and those who don't.'
 
Create the labels as JohnWM says but make sure they are numbered 1 to 4 not 0 to 3. ;-) The easiest way to do this would be to copy and paste the label again to get a lblConfig(4) (and ignore lblConfig(0) for now).

When you are ready to create the labels dynamically, then just delete all of the labels except lblConfig(0).

[thumbsup2]




Must think of a witty signature
 
I'm trying to do something very simlilar to meintsi's original question. I made a collection based on user selected dates. I now need to put those selected dates into labels on a data report. I have made a nested query but am receiving an error. Here is the code as I have it so far:

This section created the date collection:
For i = 0 To lstDate.ListCount - 1
dtSelection = lstDate.List(i)
myCol.Add dtSelection, CStr(sKey)
sKey = sKey + 1
Next i

This section tries to create a rptLabel collection:
Dim colLabel As New Collection
Dim ctlLabel As Control
Dim x As Integer

For Each ctlLabel In rptTIS.Sections(2).Controls
If TypeName(ctlLabel) = "RptLabel" Then
colLabel.Add ctlLabel
End If
Next ctlLabel

This section tries to put the selcted dates into the labels:
For i = 1 To myCol.Count
For x = 1 To colLabel.Count
rptTIS.Sections(2).Controls("colLabel(x)").Caption = myCol(i)
Next x
Next i

When I run the app I get an error on the line "For Each ctlLabel In rptTIS.Sections(2).Controls" & the error is Type mismatch. I'm just not sure what my problem is at this point. Anyone have any suggestions?

Corinne
 
Where you have

Dim ctlLabel As Control

What happens if you try

Dim ctlLabel As Label


Must think of a witty signature
 
I get the same error message........ any other suggestions?

Corinne
 
I had the same problem - there is something odd about report controls - couldn't find the proper solution so I cheated and used variant!
Code:
Dim ctlLabel
________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
I'm not sure if this has any applicability here, but since we're talking about collections. It's my understanding that when iterating collections you should use For Each instead of For i = 1 To ConfigNames.Count, etc. What this function does is put data into a labels collection from a recorsdet.

Dim SQL As String
SQL = "SELECT * FROM tblAccounts WHERE AcctID = " & cboAcctID & ""

objRec.Open SQL, objConn, adOpenStatic, adLockReadOnly, adCmdText
Dim i As Integer
Dim lbl As Label
For Each lbl In Me.Label1
lbl = objRec(i)
i = i + 1
Next lbl
Set lbl = Nothing
objRec.Close
Set objRec = Nothing
 
In response to DarenNM - I relooked at you suggestion and since Label didn't work............ duh I tried rptLabel and now I get past that point of my problem. Thanks for the suggestion! I'm still looking into how to nest two collections so that when the report pops the first date in the date collection goes into the caption for the first label in the label collection ...........any suggestions?

This is what I have so far:

Dim colLabel As New Collection
Dim ctlLabel As RptLabel
Dim x As Integer

For Each ctlLabel In rptTIS.Sections(2).Controls
If TypeName(ctlLabel) = "RptLabel" Then
colLabel.Add ctlLabel
End If
Next ctlLabel

For i = 1 To myCol.Count
For x = 1 To colLabel.Count
rptTIS.Sections(2).Controls (colLabel(x)).Caption = myCol(i)
Next x
Next i

Thanks,
Corinne
 
johnwm

How did you solve the problem of filling the labels on the report? I'm having a tough itme with this so any help will be appreciated.

Thanks,
Corinne
 
I only needed to fill a few rptLabels programmatically, so I used their index:

dr1.Sections(1).Controls(7).Caption = Format(DTPicker1.Value, "ddd d/m")
________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
John,

So for your example above the actual date that went into each of the labels in your collection was the same date? In mine I need to list a different date in each label based on user selection. This is what I have so far:

This section places all of the dates in a collection:

For i = 0 To lstDate.ListCount - 1
dtSelection = lstDate.List(i)
myCol.Add dtSelection, CStr(sKey)
sKey = sKey + 1
Next i

This section creates a label collection & tries to put the dates into the label captions:

For Each ctlLabel In rptTIS.Sections(2).Controls
If TypeName(ctlLabel) = "RptLabel" Then
colLabel.Add ctlLabel
End If
Next ctlLabel
For i = 1 To colLabel.Count
For x = 1 To myCol.Count
rptTIS.Sections(2).Controls(i).Caption = myCol.Item(CStr(sKey), (x))
Next x
Next i

I know that the problem is in the line setting the caption on the side of trying to cycle through the dates - Can you see what I'm missing?

Thanks for any help,

Corinne
 
John,

So for your example above the actual date that went into each of the labels in your collection was the same date? In mine I need to list a different date in each label based on user selection. This is what I have so far:

This section places all of the dates in a collection:

For i = 0 To lstDate.ListCount - 1
dtSelection = lstDate.List(i)
myCol.Add dtSelection, CStr(sKey)
sKey = sKey + 1
Next i

This section creates a label collection & tries to put the dates into the label captions:

For Each ctlLabel In rptTIS.Sections(2).Controls
If TypeName(ctlLabel) = "RptLabel" Then
colLabel.Add ctlLabel
End If
Next ctlLabel
For i = 1 To colLabel.Count
For x = 1 To myCol.Count
rptTIS.Sections(2).Controls(i).Caption = myCol.Item(CStr(sKey), (x))
Next x
Next i

I know that the problem is in the line setting the caption on the side of trying to cycle through the dates - Can you see what I'm missing?

Thanks for any help,

Corinne
 
Sorry I wasn't using a collection, so can't help [sad]
________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top