For Each Record?

seth
11-16-2002, 01:06 PM
Im trying to make a news program that will simply read from the database but, Is there a way i can make it create a new label box or whatever for each record in recordset?

Ales Zigon
11-16-2002, 01:32 PM
Sure it is. First place one Label on your form and set it's index to 0 (zero), so it's name will look like this: Label1(0). Then in your main loop, simpla load the next instance of this label. Like this:Load Label1(x), where [/color=red]x[/color] is the next index (1, 2, 3,...).
So, in the end, you'd end up with something like this'your recordset is established...
i = 1
Do Until RS.EOF
Load Label1(x)
'show your label
Label1(x).Visible = True
Label1(x).Caption = RS.Whatever
x = x + 1
Loop

seth
11-16-2002, 02:06 PM
i still dont get the Load Label part

this is what i have


''News Setup
iSQL = "Select * from News where NewsTech='Jade' Or 'All'"
RS.Open iSQL, oConn, adOpenKeyset, adLockOptimistic
lblNewsS.Caption = """" & RS!NewsSubject & """" & Chr(32) & "By" & Chr(32) & RS!NewsPoster & Chr(32) & "-" & Chr(32) & RS!NewsDate
lblNewsMemo.Caption = RS!NewsMemo

Ales Zigon
11-17-2002, 06:03 AM
You said, you want to create new label for each record pooled from the DB, right?
Well, first of, you'd want to loop thru all the records returned by your recordset ( as there can be more than one Jade or All in the NewsTech field) and create the new instance of the lblNewsS and lblNewsMemo labels. To do this, you'll have to, first create a control array of these labels (set the index of the first label to 0 (zero) or copy/paste it to your form). Then, you'll have to somehow loop thru your recordset (RS) and get the data for the first pair of labels (index = 0). Then, you incerement the index and go thru all the process again untill you reach the end of your recordset (RS.EOF=TRUE).
Offcourse, prior to your looping, it would be good, to check the lenght of the recordset and load appropriate number of labels with some sort of loop or take my upper advice and load them as you read the data. This way, you'll be able to fill/reposition them "on the fly".

Well, in short: what you do is actually make a new copy of the existing object (label) for each record in your recordset. Every copy of this label is recognized by it's index property. So the first (the original) label would look something like this: lblNewsS(0) where 0 is its index. Every copy of this label will have index incremented by one (lblNewsS(1), lblNewsS(2), etc.

seth
11-17-2002, 09:20 AM
i still dont get that method the code you posted will only work with one label . I tried an RS.NextRecordset and i get an error so i think it has somthing to do with the connection

seth
11-17-2002, 09:23 AM
i = 0
Do Until RS.EOF
Load Label1(i)
'show your label
Label1(i).Visible = True
Label1(i).Caption = RS.Whatever
i = i + 1
Loop
Is that what you meant?

Ales Zigon
11-17-2002, 01:13 PM
Yes. Actually, I made a small typo there (sory). Just after i = i + 1 , you'd go to the next record (RS.MoveNext).
Even better would be if you'd load your labels outside (before) Do-Loop. Something like this:

Dim x%
For x = 1 To RS.Recordcount - 1
Load Label1(x)
Label1(x).Visible = True
Next x


Then you could fill them in your main loop much easier.

If this doesn't help, maybe you could share your code...

seth
11-17-2002, 01:21 PM
the load label whats that mean it says its already loaded

seth
11-17-2002, 01:26 PM
And the values never set


''News
iSQL = "Select * from News where NewsTech='Jade' Or NewsTech='All' ORDER BY NewsDate"
RS.Open iSQL, oConn, adOpenForwardOnly, adLockReadOnly
Dim x%
I = 3
For x = 1 To RS.RecordCount - 1
lblNewsS(I).Visible = True
If I = 0 Then
GoTo Done
End If
lblNewsS(I).Caption = """" & RS!NewsSubject & """" & Chr(32) & "By" & Chr(32) & RS!NewsPoster & Chr(32) & "-" & Chr(32) & RS!NewsDate
lblNewsMemo(I).Visible = True
lblNewsMemo(I).Caption = "" & RS!NewsMemo
I = I - 1
Next x


Done:
I = 3
RS.Close
Exit Sub

Ales Zigon
11-17-2002, 02:47 PM
It's hard to say from your code, but try it this way:
iSQL = "Select * from News where NewsTech='Jade' Or NewsTech='All' ORDER BY NewsDate"
RS.Open iSQL, oConn, adOpenForwardOnly, adLockReadOnly
If Not RS.EOF And Not RS.BOF Then
RS.MoveLast
RS.MoveFirst
Dim x%
For x = 1 To RS.RecordCount - 1
Load lblNews(x)
Load lblNewsMemo(x)
lblNewsS(x).Visible = True
lblNewsMemo(x).Visible = True
Next x
I = 0
Do Until RS.EOF
lblNewsS(I).Caption = """" & RS!NewsSubject & """" & Chr(32) & "By" & Chr(32) & RS!NewsPoster & Chr(32) & "-" & Chr(32) & RS!NewsDate
lblNewsMemo(I).Caption = "" & RS!NewsMemo
I = I + 1
RS.MoveNext
Loop
End If

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum