home account info subscribe login search FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Advanced Visual Basic Techniques
(Publisher: John Wiley & Sons, Inc.)
Author(s): Rod Stephens
ISBN: 0471188816
Publication Date: 06/01/97

Search this book:
 
Previous Table of Contents Next


QueryServerForm

QueryServerForm is the MDI form that contains all of the other forms in the ReportList program. This form contains almost no code. Even though it provides the menu bar for all of the other forms, it does very little to handle the menu commands. With the exceptions of the Exit and About QueryServer commands, it simply passes requests to other objects for further processing.

For example, the Refresh List command in the Reports menu invokes the LoadReportNames subroutine provided by a ReportListForm object. The ReportListForm performs the work of fetching the list of available reports.

Private Sub mnuRptRefresh_Click()
    ReportListForm.LoadReportNames
End Sub

Similarly the Save Report As command in the File menu invokes the SaveReportAs subroutine provided by the report display form. The following code uses the MDI form’s ActiveForm property to make the currently active form save itself into a file.

Private Sub mnuFileSaveReportAs_Click()
    ActiveForm.SaveReportAs
End Sub

ReportListForm

The ReportListForm contains a list control and a command button. When the form is first created, its Form_Load event handler calls the LoadReportNames subroutine to fill in the list of available reports. The QueryServerForm also uses this subroutine to refresh the report list when the user invokes the Refresh List command in the Reports menu.

LoadReportNames creates a new object of the ReportLister class in the Librarian server program. It then invokes this object’s GetReportNames function. For the time being, the methods used by the ReportLister object to build the list of reports is unimportant. To subroutine LoadReportNames, all that is important is that this server builds a semi-colon-delimited list of report descriptions and their servers and that it returns the number of reports available.

For example, suppose there are two reports with descriptions “Report 1” and “Report number two,” and suppose they are served by the server programs Server1.Report1 and Server2.Report2. Then Librar-ian.ReportLister.GetReportNames would build the following text string:

Report 1;Server1.Report1;Report number two;Server2.Report2;

LoadReportNames uses the GetToken function to break this string into pieces. It places the report names in the form’s list control and saves the server names in the array ServerName for later use.

Public Sub LoadReportNames()
Dim lister As Object
Dim txt As String
Dim i As Integer
Dim token As String
Dim not_done As Boolean
    ReportList.Clear
  
    Set lister = CreateObject(“Librarian.ReportLister”)
  
    MaxChoice = lister.GetReportNames(txt) - 1
    If MaxChoice < 0 Then
	Beep
	MsgBox “No reports are currently available.”, _
	    vbInformation, “No Reports”
	End
    End If
  
    ReDim ServerName(0 To MaxChoice)
    not_done = GetToken(txt, “;”, token)
    For i = 0 To MaxChoice
	ReportList.AddItem token
	not_done = GetToken(“”, “;”, token)
	ServerName(i) = token
	not_done = GetToken(“”, “;”, token)
    Next i
  
    ‘ Pop this form to the top.
   Me.ZOrder
End Sub

If the user double-clicks on an item in the list, the program triggers the Load command button. This button invokes the FetchReport subroutine to begin generating the selected report. When the report is finished, the subroutine determines whether the result is nonblank. If so, it creates a new ReportForm to display the results. The result will be blank if there is an error creating the report or if the user cancels the report instead of entering any required parameters.

Private Sub ReportList_DblClick()
    CmdLoad_Click
End Sub

Private Sub CmdLoad_Click()
Dim results As String
Dim frm As ReportForm
Dim choice As Integer
    MousePointer = vbHourglass
    DoEvents
  
    choice = ReportList.ListIndex
    FetchReport choice, results
    If results <> “” Then
	Set frm = New ReportForm
	frm.Caption = ReportList.List(choice)
	frm.ReportText.Text = results
	frm.Show
    End If
    MousePointer = vbDefault
End Sub

The FetchReport subroutine uses the server names stored in the ServerName array by the LoadReportNames subroutine to create a server object. Visual Basic’s CreateObject function takes as a parameter the name of the server object to create, and it returns an object of the appropriate type. For example, CreateObject(“Server1.Report1”) would return an object of the Report1 class in the Server1 program.

Once FetchReport has created the ReportList object, it uses the object’s ListParameters function to obtain a string giving the parameters required by the report object. This string lists a prompt and a default value for each parameter, separated by semi-colons. For example, if the report needs the user to enter a reporting date with the default value of 12/12/99, the string might look like the following.

Report Date;12/12/99;

If the parameter string is nonblank, FetchReport creates a ParameterForm to allow the user to enter the necessary parameter values. It then takes the results and passes them to the report object’s LoadReport function. This function uses the parameter values to generate and return the final report.

Public Sub FetchReport(i As Integer, results As String)
Dim obj As Object
Dim params As String
Dim frm As ParameterForm

    On Error GoTo FetchReportError
    Set obj = CreateObject(ServerName(i))
  
    params = obj.ListParameters()
    If params <> “” Then
	Set frm = New ParameterForm
	frm.SetParameters params
	frm.Show vbModal
	If frm.Canceled Then
	    Unload frm
	    Set frm = Nothing
	    Set obj = Nothing
	    Exit Sub
	End If
	params = frm.GetParameters
	Unload frm
	Set frm = Nothing
	results = “”
    End If
  
    results = obj.LoadReport(params)
    Set obj = Nothing
    Exit Sub
FetchReportError:
    Beep
    MsgBox “Error” & Str$(Err.Number) & _
	“ creating ReportList.” & vbCrLf & _
	vbCrLf & Err.Description, vbInformation, _
	“Error Creating Report”
    results = “”
End Sub

The FetchReport subroutine does not really understand what kind of report object it is creating. It simply uses the string obtained earlier from the Librarian as a parameter to the CreateObject function. This is crucial to making changes centralized. The ReportList program will reside on every user’s computer. If this program knows anything about the reports it displays, it would need to be changed whenever the available reports change. If there were 100 user computers, the upgrade would be needed on all 100. Because FetchReport does not depend on knowledge of the reports, this is not an issue. Any necessary changes can be made either in the Librarian or in the report servers.

ParameterForm

The ParameterForm contains a label, a text box, and two command buttons. The label and text box have Index property set to 0. This form is shown in design mode in Figure 12.8.

The ReportListForm passes this form’s SetParameters subroutine a semi-colon-delimited list of field names and default values. SetParameters uses Visual Basic’s Load statement to create one label and one text box for each field. Figure 12.9 shows ParameterForm’s layout for the following parameter list:

Start Date;1/1/80;End Date;12/2/96;Minimum Cost;0.00;Maximum Cost;;


Previous Table of Contents Next


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.