![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Advanced Visual Basic Techniques
Getting Report ParametersIn QueryServer the FetchReport subroutine defined in the ReportListForm interacts with a report server object. FetchReport creates this object and uses its ListParameters function to obtain a list of the parameters needed by the server. It then allows the user to enter parameter values on a ParameterForm. Once it has obtained these values, FetchReport uses the server objects LoadReport command to generate the report. The new version of FetchReport creates a server object as before. As before, it uses the objects ListParameters function to obtain a list of the parameters needed by the report. At this point, the two versions of FetchReport start to diverge. The previous version uses a ParameterForm to allow the user to enter parameter values. It presents this form modally so the user cannot interact with other parts of the application until the form is closed. Unfortunately, presenting forms modally can cause problems for an asynchronous program. Suppose the program sends out an asynchronous request for a report and then the user asks for another report. The program presents a ParameterForm modally. When the earlier report finishes, it activates its callback object. In AsyncServer the callback object presents a new form to display report results. Unfortunately, Visual Basic does not allow a program to display a new form while a modal form is visible. If AsyncServer is to display report results asynchronously, it must not display ParameterForm modally. For this reason, the program displays the ParameterForm modelessly. If the ParameterForm is displayed modelessly, the FetchReport code will continue immediately after presenting the form without waiting for the user to enter parameter values. This means the rest of FetchReport must change because the values required by the old version are not yet available. Rather than processing those values and starting the report server directly, FetchReport allows the ParameterForm to start the report-generation process after the user has entered the values. The single exception to this rule occurs if the report server has no parameters. Then FetchReport calls the RequestReport subroutine to begin the report generation. RequestReport is also called by the ParameterForm when it finishes.
Public Sub FetchReport(i As Integer)
Dim obj As Object
Dim params As String
Dim frm As ParameterForm
On Error GoTo FetchReportError
Set obj = CreateObject(ServerName(i))
Get parameters.
params = obj.ListParameters()
If params <> "" Then
Create a ParameterForm to get the users
input and request the report.
Set frm = New ParameterForm
Set frm.ReportObject = obj
frm.SetParameters params
frm.TheCaption = ReportList.List(i)
This must be modeless in case a report
returns while it is running.
frm.Show vbModeless
Else
If we do not need parameters, request
the report now.
RequestReport obj, "", ReportList.List(i)
End If
Exit Sub
FetchReportError:
Beep
MsgBox "Error" & Str$(Err.Number) & _
" creating report server." & vbCrLf & _
vbCrLf & Err.Description, vbInformation, _
"Error Creating Report"
End Sub
Imitating ModalityAlthough the program cannot use ParameterForm modally, it can at least ensure that the form remains on top of other application windows. With the SetWindowPos API function used to keep splash screens on top of other windows, the ParameterForms Load event handler makes the form a topmost window. The applications other windows are contained within an MDI form. Disabling the MDI form prevents the user from interacting with the other windows in the application until the ParameterForm is closed. If the user clicks on a form other than the ParameterForm, the application beeps just as if it had presented the ParameterForm modally. Private Sub Form_Load() Dim status As Long Make this window stay on top. status = SetWindowPos(hWnd, HWND_TOPMOST, _ 0, 0, 0, 0, SWP_NOMOVE + SWP_NOSIZE) Disable the main MDI form. QueryServerForm.Enabled = False End Sub Private Sub Form_Unload(Cancel As Integer) Reenable the main MDI form. QueryServerForm.Enabled = True QueryServerForm.SetFocus End Sub If the user clicks ParameterForms Cancel button, the form simply unloads itself. If the user clicks the Ok button, the form starts the report-generation process. It does this by calling the RequestReport subroutine.
Private Sub CmdCancel_Click()
Unload Me
End Sub
Private Sub CmdOk_Click()
Dim params As String
Me.Hide
DoEvents
Create the delimited parameter list.
params = GetParameters()
Request the report.
RequestReport ReportObject, params, TheCaption
This form is done.
Unload Me
End Sub
Requesting ReportsThe RequestReport subroutine finally begins the actual report-generation process. It calls the report server objects LoadReport subroutine, passing it the parameters entered by the user. It also passes the server an object of class Callback. Because asynchronous processing can be quite confusing, RequestReport displays a status message indicating that the report has been requested. The message appears in a label control inside a picture box attached to the bottom of the main MDI form. The SetStatus subroutine makes setting this message a little easier. Another message is presented when the report is returned.
Public Sub RequestReport(obj As Object, params As String, cap As String)
Dim cb As New Callback
SetStatus "Requested report: " & cap
cb.TheCaption = cap Save the caption for use by the callback.
obj.LoadReport params, cb
End Sub
Public Sub SetStatus(txt As String)
QueryServerForm.StatusLabel = txt
DoEvents
End Sub
|
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.
|