Visual Basic Realization Dynamic Report

xiaoxiao2021-03-06  41

How to implement dynamic reports in Visual Basic is a problem with high frequency in many VB website forums. In the actual project, the author also encounters this problem. After a deep study, use a simple method in Visual Basic A dynamic report. Align = "marginwidth =" 0 "marginheight =" 0 "src =" http://images.chinabyte.com/adjs/iframe-pip/y-software-pip.html "frameborder =" 0 "width =" 360 "scrolling =" no "height =" 300 ">

There are a lot of tools for making reports in Visual Basic, such as Crystal Report Editor, DataReport Report Form, Printer object, and some third-party controls such as VSVIEW. However, the Crystal Report Editor can only make custom reports, and the Printer object is operated very complicated. Most of the third-party controls need to be purchased, and the cost is not. The author's production dynamic report is implemented through the DataReport form, and the following adopt a specific example explains how to implement a dynamic report. Suppose an Access database stock.mdb is stored in the C disk dongtai directory, "The stock market" is used to record the stock market, the data structure of the "stock market" is shown in 1 table: Table 1 "Stock Market" data structure

Field Name ID StocknostockNamePRICESWINGMAXPRICEMINPRICEBARGAIN data type long integer text text Double-precision type double precision double accuracy double precision long integer

If you want to design such a report, the report output field is determined by the user himself, how to design it with a DataReport form? Step step by step. 1. Familiar with the DataReport Form and Data Report Controls The DataReport Form is a form similar to a normal form. The biggest difference is that the data report control can only be placed on the form. This type of control is only six, they are : RPTLABEL, RPT text Box, RPTIMAGE, RPTSHAPE, RPTLINE, RPTFunction. From the name, you can identify their use, RPTLabel is used to display tags for non-data binding text; often used to make reports page title; RPT text box displays the content of the text binding, that is, the field of displaying the database Content; RPTIMAGE displays a graphic, such as a company's trademark; RPTShape can draw many graphics, emphasize information or enhance visual effects; RPTLine can draw lines in the report, divide the reported area; RPTFunction allows you to note in report titles or reports Area placed, which contains a simple mathematical statistical function. DataReport Forms and Data Reports Controls are shown in Figure 1.

Figure 1 DataReport Forms and Data Reports Controls In order to be able to output all fields, you must place a sufficient number of related report data controls in different regions of the DataReport form. This example is eight, so in the "Totors" area (Section2) Putting eight RPTLabel controls, nine vertical dividing lines and two horizontal lines, eight RPT TEXT BOX controls in the "Detail" area, nine vertical split lines and one horizontal line, As shown in Figure 1. At the same time, for future coding, the following naming method is used for each control: the control name is the control type area number serial number, for example, the "page header" area (Section 2) eight RPTLabel controls are named Label21, label22, label23 ... Label28, the eight RPT TEXT BOX controls of the region (Section1) are named Text11, Text12, Text13 ... Text18, vertical division lines in each region adopt the same named method, but horizontal split lines are controlled by control type area Number, name, such as the upper and lower horizontal lines in the "Page Tap" area, named line2 and line_2, can see the benefits of this naming method in the following code. The most important three data in the DataReport form The control is RPT text box, rptlabel, and rptfunction, you must set their properties separately, RPT Text Box The most important properties are DataField, which indicates the field name of the binding record; RPTLabel's most important attribute is caption, this property indicates to The text content displayed; the most important attribute of RPTFunction is DataField and FunctionType. The former indicates the field name to be bound and statistically recorded, which indicates the type of statistical function. These three controls have a total of GangRow, this attribute value When True, when the displayed content exceeds the control of the control, there is also a lot of properties, such as LeftMargin, Right Margin, TopMargin, Bottommargin, ReportWidth, DataSource, etc., where ReportWidth sets the paper width (must be minus left and right) The width of the margin of the page), the ReportWidth must correspond to the paper size set of the printer, and the DataSource sets the data source of the DataReport form. The various areas in the DataReport form are objects, such as section1.section2, etc. There are also many properties that focus on the Keeptogther property. When KeeptoGether is True, if a record is at the bottom of a page, it is possible to force this record to change to the next page. This guarantees the integrity of the data. The above is simply illustrated that some of the important properties involved in the DataReport form, other unspecified properties can betro in the relevant books and MSDNs. 2. Main interface design by the main interface design As shown in Figure 2. The listBox (LST_CANDID) of the candidate field ATE to display all raw fields, selected fields of listbox (LST_SELECTED) used to record fields selected in the form of report, and the buttons with "<" and ">" headings between the two listboxs are used to add and Remove the field, use a text box (txt_caption) to set the title of the report, and click the "Output Report" button to output the report.

Figure 2 Dynamic report main interface 3. The control field width of the field width is important in the report design, here is implemented by the properties of the LST_SELECTED ITEMDATA, only one subroutine can complete the property assignment of the LST_SELECTETA, the code is as follows. '********************************************************** ***************** Field Width Control Subprogram Private Sub FieldWidth (LST As Listbox) DIM I AS IntegerConst WordWidth As INTEGER = 201 'Western Width Width is 201 缇 For I = 0 to Lst.ListCount - 1Select Case Lst.List (i) Case "Sequence" LST.ItemData (i) = WordWidth * 3case "Stock Code" LST.ItemData (i) = WordWidth * 5case "Stock Name" LST. ItemData (i) = wordwidth * 6case "Closed price" LST.ItemData (i) = wordwidth * 5case "rising" LST.ItemData (i) = wordwidth * 4case "highest price" LST.ItemData (i) = wordwidth * 5case "Lowest price" LST.ItemData (i) = wordwidth * 5case "transaction" LST.ItemData (i) = wordwidth * 4nd selectnext Iend Sub '***************** *********************************************************

4. Set the DataReport Form Properties, control the property of the DataReport form, and the control output format can also be done with a sub-process, which is required to set the properties for DataReport and data controls, Visual Basic Most do not appear during design coding. Under normal circumstances, there is no need to be strange, the complete code is as follows.

'********************************************************** **************** Sets Various parameters of the report form, control the output format Private sub setDataReport (Lst AS ListBox, DTR AS Datareport, Scaption As String) Dim leftpos as Long 'leftpos storing the control variable of the left property reportwidth Dim as Long' reportwidth overall width is variable storage Datareport Dim i as Integer 'total width seeking For i = 0 to lst.ListCount - 1reportwidth = reportwidth lst.ItemData (i) Next 'initialization with DTR' Setting data sources, page margins, headings, horizontal split lines, Section1, 2 zone first vertical division line .leftmargin = 1440.rightmargin = 1440.topmargin = 1440.BOTTOMMARGIN = 144. ReportWidth = Printer.width - 2880 - 20Set .datasource = RS 'RS is the adoDB data source .SEctions ("Section4"). Controls.Item ("Label41"). CAPTION = Scaption.sections ("Section4"). Controls.Item ("Label41"). Width = ReportWidth.sections ("Section4"). Controls.Item ("Label42"). Width = ReportWidth.sections ("Section2"). Controls.Item ("line_2"). Width = ReportWidth. Sections ("Section2"). Controls.Item ("line2"). Width = ReportWidth.sections ("Section1"). Controls.Item ("line1"). Width = ReportWidth.sections ("Section2"). Controls.Item ("line20"). LEF T = 0.Sections ("section1"). Controls.Item ("line10"). Left = 0ful with 'Setting data for section1, zone setting data LEFTPOS = 0for i = 0 to Lst.ListCount - 1Dtr.SEctions ("Section2" ) .Controls.item ("Label1" & (i 1)). Caption = Lst.List (i) DTR.SEctions ("Section2"). Controls.Item ("Label1" & (i 1)). Width = LST.ItemData (i) DTR.SECTIONS ("Section2"). Controls.Item ("Label1" & (i 1)). Left = leftposdtr.sections

Section1 "). Controls.Item (" Text1 "& (i 1)). DataField = Lst.List (i) DTR.SEctions (" Section1 "). Controls.Item (" Text1 "& (i 1)) .Width = LST.ItemData (i) DTR.SEctions ("Section1"). Controls.Item ("Text1" & (i 1)). Left = leftposdtr.sections ("section2"). Controls.Item ("line2 "& (i 1)). LEFT = LeftPOS LST.ItemData (i) DTR.SEctions (" Section1 "). Controls.Item (" LINE1 "& (i 1)). Left = leftpos lst.itemdata (i) Leftpos = LeftPOS LST.ItemData (i) 'Special Format Special Field Special Processing IF LST.List (i) = "Closed Price" or Lst.List (i) = "Minimum Price" Or Lst.List (i ) = "HOVERALIES). Controls.Item (" Text1 "& (i 1))). DataFormat.Format =" ¥ #, ## 0.00 "end ifif Lst.List (i) = "Turning" thendtr.sections ("Section1"). Controls.Item ("Text1" & (i 1)). DataFormat.Format = "#, ## 0.00" end ifif Lst.List (i) = " Transaction volume "ThendTr.sections (" Section1 "). Controls.Item (" Text1 "& (i 1)). DataFormat.Format =" #, ## 0 "End ifnext i 'Volumes must be set DataField properties, such as LST.List (0), otherwise an error, but it will make it unusable "other unused Label, the Line control is also not visible I = Lst.ListCountWhile i <(Dtr.Sections). Controls .C Ount - 10) '10 The total number of controls for non-TEXT controls for the section1 area i = i 1dtr.sections ("Section1"). Controls.Item ("Text1" & I) .Datafield = Lst.List (0) DTR. Sections ("Section1"). Controls.Item ("Text1" & i) .visible = falsedtr.sections ("section2"). Controls.Item ("line2" & i) .visible = falsedtr.sections ("Section1"

) .Controls.Item ("line1" & i) .visible = falsedtr.sections ("section2"). Controls.Item ("text1" & i) .visible = false weendend sub '********************* *********************************************************** ****************** 5. The report output report output is done by clicking the "Output Report" button (cmd_report), the code is as follows.

'********************************************************** ********************************************************************* Private Sub CMD_Report_Click () If LST_SELECTED.LISTCOUNT <> 0 Thencall FieldWidth (LST_SELECTED) Set field Width Call SetDataReport (LST_SELECTED, DTREPORT, TRIM $ (txt_caption)) 'Settings Report Properties' Show method to display DataReport forms, this example is named DTREPORTDTREPORT.SHOW ELSEMSGBOX "in this example" Please select the field entry to output ", Vbokonly vbinformationnd iFend sub '************************************************************ ***********************************************

6. After printing the DataReport form in the DataReport form, the appearance is shown in Figure 3. The upper portion of the form has three function buttons, the first button is used to print the report, and the second button can output the report to the HTML format file, the third button is used to scale the report to facilitate the browsing. Figure 3 Dynamic Report

转载请注明原文地址:https://www.9cbs.com/read-51645.html

New Post(0)