Using Scripting

When working with RPX files, all necessary report code must be included with the RPX file in the form of a script because any Visual Basic code used to create the report in not saved into the RPX file. Also, the end user will need to use an ActiveScripting language to make any type of programmatic changes to a report.

Note: For a more detailed explanation of scripting examine chapter 14 in the standard edition user's guide.

ActiveReports provides two different methods to help make scripting easier and more versatile with Visual Basic. The report object's AddCode method allows code to be added, in the form of a string, at runtime and the AddNamedItem method adds functions and subs contained inside the Visual Basic code to the scripting name space. Continuing with the designer sample we will use both methods to demonstrate how each item is setup. Because RPX files are not secure files, it is highly suggested that all sensitive information be left out of the RPX file. Since the project is currently using a data control, with the connection string specified, the connection sting will be visible in the RPX file. It is highly recommended to use AddNamedItem to allow the Visual Basic project to retrieve the Recordset and pass this to the DataControl. The following demonstrates how to convert the sample project to take advantage of the AddNamedItem method.

Using AddNamedItem

  1. Add a class module to the project and set its name to clsFunctions.

    Note: When working with AddNameItem, the subs and functions must be wrapped within a class.

  2. In Visual Basic's references list, select the newest Microsoft ActiveX Data Objects Library.
  3. Add the following function to clsFunctions:

    Public Function getRSet() As ADODB.Recordset
    Dim rs As ADODB.Recordset
    Dim cn As ADODB.Connection
    Dim cnnString As String
    On Error GoTo errHndl
     
    Set cn = New ADODB.Connection
    Set rs = New ADODB.Recordset
     
    'Connect to DB and get recordset
    cnnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Program Files\Microsoft Visual Studio\VB98\NWIND.MDB;Persist Security Info=False"
    cn.Open cnnString
    rs.Open "Select * from customers order by country", cn
    Set getRSet = rs
     
    Set rs = Nothing
    Set cn = Nothing
     
    Exit Function
     
    errHndl:
    MsgBox "Unable to get recordset: " & Err.Number & ": " & Err.Description
    Set rs = Nothing
    Set cn = Nothing
    End Function

  4. Make the following modifications to the prepViewer sub to make the report object and script aware of the added class:

    Private Sub prepPreview()
    On Error GoTo errHndl
    'Writes the designer's layout
    'to the report so it can be previewed.
    ard.SaveToObject rpt
    'Saves the report object to the specified style
    rpt.Save App.Path & "\sample report.rpx", ddSOFile
    'Resets report
     
    'Activate the Script debugger and refresh the script
    rpt.ScriptDebuggerEnabled = True
    rpt.ResetScripts
     
    ' Use AddNamedItem to add the function to the scripting name space
    rpt.AddNamedItem "vbCode", New clsFunctions
     
    rpt.Restart
    'Run the new report
    rpt.Run False
    'Add the report to the veiwer
    Set arv.ReportSource = rpt
     
    'Disable menu items in preview mode
    mFile.Enabled = False
    mEdit.Enabled = False
     
    Exit Sub
     
    errHndl:
    MsgBox "Error Previewing the Report: " & Err.Number & " " & Err.Description
    End Sub

  5. Save and run the project.
  6. Select DataControl1 on the designer and clear out the ConnectionString and Source string.
  7. Select the Script icon image\ebd_Ebd10.gif and add the following code to the ActiveReport Document OnDataInitialize sub:

    Sub OnDataInitialize
    set rpt.datacontrol1.recordset = vbcode.getrset
    End Sub

  8. Select the Report Preview tab to use the new function.

Using AddCode

  1. Add the following code to clsFunctions:

    Public Function IIf(Expression, TruePart, FalsePart)
    IIf = VBA.IIf(Expression, TruePart, FalsePart)
    End Function
     
    Public Function Format(Expression, sFormat)
    Format = VBA.Format(Expression, sFormat)
    End Function

  2. Add the following code to frmMain:

    Private Function HelperCode() As String
    Dim sCode As String
    sCode = ""
    sCode = sCode & _
    "Public Function IIf(expr, exprTrue, exprFalse)" & vbCrLf & _
    "If expr Then IIf = exprTrue Else IIf = exprFalse" & vbCrLf & _
    "End Function" & vbCrLf
     
    sCode = sCode & _
    "Public Function Format(expr, fmt)" & vbCrLf & _
    "Format = vbCode.Format(expr, fmt)" & vbCrLf & _
    "End Function"
    End Function

  3. Add the following code to prepPreview to use the AddCode method:

    Private Sub prepPreview()
    On Error GoTo errHndl
    'Writes the designer's layout
    'to the report so it can be previewed.
    ard.SaveToObject rpt
    'Saves the report object to the specified style
    rpt.Save App.Path & "\sample report.rpx", ddSOFile
    'Resets report
     
    'Activate the Script debugger and refresh the script
    rpt.ScriptDebuggerEnabled = True
    rpt.ResetScripts
     
    'Add IIf helper code
    rpt.AddCode HelperCode()
     
    'Use AddNamedItem to add the function to the scripting name space
    rpt.AddNamedItem "vbCode", New clsFunctions
     
    rpt.Restart
    'Run the new report
    rpt.Run False
    'Add the report to the veiwer
    Set arv.ReportSource = rpt
     
    'Disable menu items in preview mode
    mFile.Enabled = False
    mEdit.Enabled = False
     
    Exit Sub
     
    errHndl:
    MsgBox "Error Previewing the Report: " & Err.Number & " " & Err.Description
    End Sub

  4. Save and run the project.

    Note: The samples contained in this section are designed to demonstrate the fundamentals for using the end-user report designer. More advanced samples can be found in the sample directory and in Data Dynamics' online knowledgebase at http://www.datadynamics.com/kb.