I query a MySql table with 5 different records. Then I fill a VB6 Data Report. I have 5 records in the table with fields Name and Surname, problem is the report shows 5 same names instead of five different names.
The 5 same names all belong to the last record fetched from the database, here is the code to fill the Data Report label at runtime:
Set rs = New ADODB.Recordset 'Creates record set
strSQL = "select * 开发者_JAVA百科from person"
rs.Open strSQL, cn, strDBCursorType, strDBLockType, strDBOptions
If rs.EOF Then
GoTo ExitSub
Else
For B = 1 To rs.RecordCount
'MsgBox (rs!Name + " " + rs!Surname)
rptRuntime.Sections("Section1").Controls("lblName").Caption = rs!Name
rs.MoveNext
Next B
End If
rptRuntime.Show
The label that is being filled is placed in the Detail section, named "Section1".
Better to bind to the Recordset or a custom data source object. Procedural code isn't required with DataReports.
This block of code
For B = 1 To rs.RecordCount
'MsgBox (rs!Name + " " + rs!Surname)
rptRuntime.Sections("Section1").Controls("lblName").Caption = rs!Name
rs.MoveNext
Next B
Is going to set rs!Name 5 times in a row, ending up with rptRuntime.Sections("Section1").Controls("lblName").Caption
containing the last change. ALL 5 changes happened before the report is even displayed using
rptRuntime.Show
It goes without saying that you will have a (repeated report) section that has a fixed caption, showing the same caption in each repeat.
精彩评论