|
Bind a DataReport
To an ADO Recordset at Run Time
PRODUCT :Microsoft Visual Basic for Windows
PROD/VER:WINDOWS:6.0
OPER/SYS:WINDOWS
KEYWORDS:kbVBp600 kbADO200 kbcode
======================================================================
---------------------------------------------------------------------
The information in this article applies to:
- Microsoft Visual Basic Professional and Enterprise Editions for
Windows, version 6.0
---------------------------------------------------------------------
SUMMARY
=======
The DataReport is a powerful tool and it's easy to build complex reports
by
dragging and dropping fields out of the DataEnvironment window. However,
there are times when you may want to bind the DataReport directly to an
ActiveX Data Objects (ADO) recordset rather than to the DataEnvironment.
For example, you may have built a hierarchical query with ADO, or you
may
have an n-tier application that receives a recordset from a business
object.
This article helps you understand how to bind a DataReport directly to
an
ADO recordset.
MORE INFORMATION
================
First, build a hierarchical query with the DataEnvironment. Next, create
a
simple DataReport that is based on your query and bound to the
DataEnvironment.
Use the DataEnvironment to connect to the Northwind database (NWind.mdb)
that is included with Visual Basic by following these steps:
1. Create a new Standard EXE project in Visual Basic.
2. Add a DataEnvironment to that project and rename it deCustomerOrders.
3. Rename the initial connection to cnNWind
3. Set the connection to use the Microsoft.Jet.OLEDB.3.51 OLE DB provider.
4. Locate the Northwind database on your machine.
5. Add a command to the connection and rename it Customers.
6. Set the Customers command to query the Customers table.
7. Add a child command to the Customers command and rename it Orders.
8. Set the Orders command to query the Orders table.
9. Relate the two commands on the CustomerID field on the Relation tab.
10. Add a DataReport to the project and rename it rptCustomerOrders.
11. Set the DataSource property of the DataReport to deCustomerOrders.
12. Set the DataMember property of the DataReport to Customers.
13. Right-click on the DataReport and clear "Show Report Header/Footer".
14. Right-click on the DataReport and clear "Show Page Header/Footer".
15. Right-click on the DataReport and select "Insert Group Header/Footer".
16. Drag the CustomerID and CompanyName fields from the Customers command
in the DataEnvironment onto the Group Header section.
17. Drag the OrderID and OrderDate fields from the Orders command in the
DataEnvironment onto the Detail section.
18. Add a CommandButton to your form.
19. Add the following code to your form:
Private Sub Command1_Click()
rptCustomerOrders.Show
End Sub
20. Run the project, click on the CommandButton and you should see the
report with the customer and order information.
21. To bind the DataReport directly to the hierarchical recordset generated
by the DataEnvironment, add the following code:
Private Sub Form_Load()
Dim intCtrl As Integer
With rptCustomerOrders
Set .DataSource = Nothing
.DataMember = ""
Set .DataSource = deCustomerOrders.rsCustomers
With .Sections("Section2").Controls
For intCtrl = 1 To .Count
If TypeOf .Item(intCtrl) Is RptTextBox Or _
TypeOf .Item(intCtrl) Is RptFunction Then
.Item(intCtrl).DataMember = ""
End If
Next intCtrl
End With
.Show
End With
End Sub
Note: If you omit steps 13 and 14, you need to change "Section2"
to
"Section6" in the preceding code.
22. Run the project, and you should see the report with the customer and
order information.
The DataReport uses the DataSource and DataMember properties to find the
top-level command on which the report is based. For example, if you have
a
hierarchical query in the DataEnvironment containing Customers, Orders,
and
Order Details information but you only want to show the Orders and Order
Details information, then you should set the DataSource property to be
the
DataEnvironment, and the DataMember property to be the Orders command.
Each field on the DataReport has two properties that allow the
DataEnvironment to determine what information to show on the report:
- DataMember
- DataField.
Use the DataMember property to select the level of the hierarchy that
contains the information you want to display. Use the DataField property
to
select the field you want to display.
For example, the CustomerID field is in both the Customers and the Orders
table. If you want to show the CustomerID field with the rest of the
customer information, set DataMember to Customers. If you want to show
the
CustomerID with the rest of the Order information, set DataMember to
Orders.
When you bind directly to a recordset object as shown in step 21, the
DataSource property of the DataReport should be set to the recordset object
and the DataMember property should be set to an empty string. For the
fields on the report, the DataMember property of the top-level recordset
information (customer information in this case) should be set to an empty
string. For information other than that which is in the top-level
recordset (Order information in this case), the DataMember property of
the
report TextBoxes should be set to the name of the command (Orders in this
case).
返回
Set a Custom Range
for Value Axis with MSChart Control
PRODUCT :Microsoft Visual Basic for Windows
PROD/VER:WINDOWS:5.0
OPER/SYS:WINDOWS
KEYWORDS:
======================================================================
---------------------------------------------------------------------
The information in this article applies to:
- Microsoft Visual Basic Learning, Professional, and Enterprise Editions
for Windows, version 5.0
---------------------------------------------------------------------
SUMMARY
=======
When plotting points with the Microsoft Chart control, the scale for the
value axis is scaled to represent only the range of numbers needed to
display the data points being charted. For example, if the data points
you
are charting are between 1000 and 1010, the full range of 0 through 1010
is
not needed and may not be used.
MORE INFORMATION
================
At times it may be desirable or necessary to specify a custom range for
the
value axis. Microsoft Chart offers properties that allow the user to
customize this setting. To set a custom range, three properties must be
set: Auto, Minimum, and Maximum. Below are the steps to create a sample
application that demonstrates how to configure the chart's scale
programmatically.
Step-by-Step Example
--------------------
1. Open a new Standard EXE project. Form1 is created by default.
2. Choose Components from the Project menu, and add a reference to
"Microsoft Chart Control."
3. Add a Chart control to Form1 (sample data is automatically supplied).
4. Add a CommandButton to Form1.
5. Paste the following code into Form1's code module:
Private Sub Command1_Click()
' Set chart type to 2d bar
Form1.MSChart1.chartType = VtChChartType2dBar
' Use manual scale to display y axis (value axis)
With Form1.MSChart1.Plot.Axis(VtChAxisIdY).ValueScale
.Auto = False
.Minimum = -100
.Maximum = 100
End With
End Sub
6. Run the sample project.
7. Note that the range of the Y axis is 0 to 100.
8. Click the CommandButton.
9. Note that the range of the Y axis now reflects the -100 to 100 range
that was set programmatically.
返回
Create
a Stored Procedure in ASP using the Create Procedure SQL Command
Just drop this code on a new ASP page, change the connection string and
run it. It will create a new stored procedure called CountProducts that
will count the number of Products in the table called Products.
Dim Con
set con = Server.CreateObject("ADODB.Connection")
Con.Open "MY-CONNECTION-STRING"
Con.Execute "CREATE PROCEDURE CountProducts AS Select Count(*) from Products"
返回
Back to top
|