VB and reports (III. Data Connection andCodeImplementation)
When a report template is created almost,Data connection should be established.,Data Connection procedure:
Note that step 7 is required.,If the password cannot be saved,The following error occurs when the query statement is written and then tested.;
This is to ensure data security,The password written in step 1 is only allowed once.,When the database is connected again, the connection will fail if the user name and password are not entered.,So step 7 is necessary..
Then execute the data title generation,Execute Edit to generate table columns Based on fields.
Generate as shown in the right figure
You can drag and drop to adjust the column width..You can also adjust some other attributes through the attribute bar on the right..
So far, the database has been connected successfully.
Next, the code is implemented..Before implementing the following code, add components and references.,Then add the report Control.
The Code is as follows:
Option explicit defines the report object dim report as grprolibctl. gridppreport private sub form_load () dim strsql as string dim strmsg as string strsql = "select * From jiezhang_info where date = '" & Date & "'" 'instantiate the report set report = new grprolibctl. gridppreport' loads the template report. loadfromfile (App. path & "\ dayaccount. GRF ") 'Data source connection report. detailgrid. recordset. connectionstring = connectstring 'use the SELECT query to create a record set report. detailgrid. recordset. querysql = strsql' View Report grdisplayviewer1.report = Report grdisplayviewer1.start end sub private sub cmdrefresh_click () report. detailgrid. recordset. querysql = "select * From jiezhang_info where date = '" & Date & "'" grdisplayviewer1.refresh 'refresh end sub private sub into view_click () report. printpreview (true) 'print and preview end sub private sub into print_click () report. [print] (true) 'print end sub