Microsoft KB Archive/289793

= How To Dynamically Populate a Group Data Report in Visual Basic =

Article ID: 289793

Article Last Modified on 7/15/2004

-

APPLIES TO


 * Microsoft Visual Basic 6.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition Service Pack 3
 * Microsoft Visual Basic 6.0 Enterprise Edition Service Pack 4
 * Microsoft ActiveX Data Objects 2.0
 * Microsoft ActiveX Data Objects 2.1
 * Microsoft ActiveX Data Objects 2.1 Service Pack 2
 * Microsoft ActiveX Data Objects 2.5
 * Microsoft ActiveX Data Objects 2.6
 * Microsoft ActiveX Data Objects 2.7

-



This article was previously published under Q289793



SUMMARY
This article explains how to create a Group Data Report programmatically without binding it to any data at design time.



Step-by-Step Example
 Open a Standard EXE project in Visual Basic. Form1 is created by default. Add a Command button to Form1. On the Project menu, click References, and then add a reference to Microsoft ActiveX Data Objects Library. On Project menu, click to add DataReport1. If Add Datareport is not on the Project menu, add it from the Designers tab located on the Project menu under Components. (Make sure that you deselect Selected Items Only in the Project menu under Components.) Open Datareport1 and ensure that the Detail, Page Header, and Page Footer sections are visible. If the Report Header or Footer sections are visible, remove them by right-clicking the report and removing the check mark from Show Report Header/Footer. Right-click and select Insert Group Header/Footer. A new Section, Section4, is added to the Datareport. Add one rptLabel control and one rptTextbox control to the new section.</li> Add two rptLabel controls and two rptTextbox controls to the Detail section of the Datareport.</li>  Copy and paste the following code into the form: Dim cn As New ADODB.Connection Dim rs As New ADODB.Recordset Dim cmd As New ADODB.Command Dim rs1 As New ADODB.Recordset Private Sub Command1_Click Dim q As Integer Dim intCtrl As Integer Dim x As Integer Dim z As Integer x = 0 q = 0 z = 0

With DataReport1 .Hide Set .DataSource = rs     .DataMember = &quot;&quot; With .Sections(&quot;section4&quot;).Controls For intCtrl = 1 To .Count If TypeOf .Item(intCtrl) Is RptLabel Then .Item(intCtrl).Caption = &quot;City&quot; & &quot; :&quot; q = q + 1 End If     If TypeOf .Item(intCtrl) Is RptTextBox Then .Item(intCtrl).DataMember = &quot;&quot; .Item(intCtrl).DataField = &quot;City&quot; End If     Next End With q = 0 With .Sections(&quot;Section1&quot;).Controls For intCtrl = 1 To .Count If TypeOf .Item(intCtrl) Is RptLabel Then .Item(intCtrl).Caption = rs1.Fields(q).Name & &quot; :&quot; q = q + 1 End If         If TypeOf .Item(intCtrl) Is RptTextBox Then .Item(intCtrl).DataMember = &quot;Command1&quot; .Item(intCtrl).DataField = rs1(z).Name z = z + 1 End If     Next intCtrl End With .Refresh .Show End With End Sub

Private Sub Form_Load

Command1.Caption = &quot;Show Report&quot; cn.Open &quot;Provider=MSDATASHAPE; Data Provider=Microsoft.JET.OLEDB.4.0;&quot; & _ &quot;Data Source=D:\Program Files\Microsoft Visual Studio\VB98\Nwind.mdb;&quot; With cmd .ActiveConnection = cn          .CommandType = adCmdText .CommandText = &quot; SHAPE {SELECT FirstName,Lastname,City FROM `Employees`} AS Command1 COMPUTE Command1 BY 'City'&quot; .Execute End With With rs          .ActiveConnection = cn           .CursorLocation = adUseClient .Open cmd End With Set rs1 = rs(0).Value

End Sub </li></ol>

<div class="references_section">