![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Advanced Visual Basic Techniques
The following code shows the MakeReport subroutine for the Report5 class. This class processes free-format SQL queries. It is the simplest of the report server classes because it does not need to interpret the parameters specified by the user. The MakeReport subroutines of all of the example report servers waste between 5 and 10 seconds to simulate long queries. This makes the reports slow enough that the user can select a second report before the first finishes to test the applications asynchronous features.
Public Sub MakeReport() Dim query As String Dim not_done As Boolean Dim results As String Unload the WakeUpForm. Unload TheWakeUpForm Get the query. not_done = GetToken(TheParams, ";", query) Execute the query. results = query & vbCrLf & vbCrLf & _ ProcessSelect(DB_NAME, query) Waste some time to simulate a long query. Pause Rnd * 5000 + 5000 Send the results to the callback object. TheCallback.DisplayReport results We dont need this any more. Set TheCallback = Nothing End Sub Displaying ResultsWhen the callback objects DisplayReport subroutine is invoked, control returns to the ReportList client application. The client displays the report results and waits for the next user action or for another asynchronously generated report to return. Displaying the ReportFormThe callback class used by ReportList is fairly simple. It contains one variable, TheCaption, which indicates the caption the report should display. This variable is set in the RequestReport subroutine that originally creates the callback object. The callback object provides one subroutine, DisplayReport, that is called by the report server object. DisplayReport creates a new ReportForm, sets the forms Caption, and inserts the report into the forms single text control for display.
Public Sub DisplayReport(txt As String)
Dim frm As New ReportForm
frm.Caption = TheCaption
frm.ReportText.Text = txt
frm.Show
Set the MDI forms status message.
SetStatus "Returned report: " & TheCaption
End Sub
Displaying the ReportReportForm displays the report results much as the ReportForm does in the Query Server application. Its only interesting component is the SaveReportAs subroutine that allows the user to save the displayed report into a file. See the description of ReportForm in Chapter 12 for more details. Displaying MessagesJust as a modal ParameterForm can cause problems for AsyncServer, so too can the MsgBox statement. Because MsgBox presents a modal dialog, the application can run into trouble if a callback object tries to display a report while a call to MsgBox is running. To prevent this problem, Report List displays error messages through a form of its own rather than using MsgBox. MsgForms ShowMsg subroutine takes as parameters a message to display and a caption to give the message dialog. It then presents the message, as shown in Figure 13.7. MsgForm is fairly simple and does not provide many of the features available with the MsgBox statement. It does not rearrange its controls to take into account the size of the message. It also does not provide an assortment of images and buttons the way MsgBox does. It simply presents a message modelessly. MsgForms Form_Load event handler centers the form and uses the SetWindowPos API function to keep the window on top of the other windows in the application. Like ParameterForm, it disables the applications main MDI form. The Form_Unload event handler reenables the MDI form.
Public Sub ShowMsg(txt As String, cap As String)
MsgLabel.Caption = txt
Caption = cap
Me.Show
End Sub
Private Sub CmdOk_Click()
Unload Me
End Sub
Private Sub Form_Load()
Dim status As Long
Center the form.
Left = (Screen.Width - Width) / 2
Top = (Screen.Height - Height) / 2
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
DoEvents
End Sub
Private Sub Form_Unload(Cancel As Integer)
Reenable the main MDI form.
QueryServerForm.Enabled = True
QueryServerForm.ActiveForm.SetFocus
End Sub
SummaryWhile Visual Basic itself does not support asynchronous processing, it provides all the tools you need to implement it yourself. After receiving a request from a client, a server can use a timer object to schedule an asynchronous action for later. It can then return control to the client so the client can perform other tasks. When the timer awakens the server, it can perform its necessary tasks and then notify the client by using a callback object when it has finished. Using these techniques, a Visual Basic program can access the full power of a network of computers that might otherwise remain underused.
|
|
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.
|