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
Note: When working with AddNameItem, the subs and functions must be wrapped within a class.
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
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
Sub OnDataInitialize
set rpt.datacontrol1.recordset = vbcode.getrset
End Sub
Using AddCode
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
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
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
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.