Microsoft KB Archive/166291

From BetaArchive Wiki

Article ID: 166291

Article Last Modified on 1/19/2007



APPLIES TO

  • Microsoft Access 97 Standard Edition



This article was previously published under Q166291

SYMPTOMS

Advanced: Requires expert coding, interoperability, and multiuser skills.

The example provided in the "CommonDialog control" Help topic does not work in Microsoft Access 97.

Microsoft Visual Basic version 4.0 and later has the capability of creating an array of controls. This capability does not exist in Visual Basic for Applications; therefore, some Visual Basic examples that use control arrays cannot be fully implemented in Microsoft Access 97. This article discusses the Visual Basic example for the CommonDialog control, which uses a control array in the Form_Paint procedure.

This article assumes that you are familiar with Visual Basic for Applications and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information about Visual Basic for Applications, please refer to the "Building Applications with Microsoft Access 97" manual.

RESOLUTION

If you want to implement the Visual Basic example for the CommonDialog control, you must exclude or replace the Form_Paint procedure used in the example, and modify the Command1_Click procedure.

Excluding the Form_Paint Procedure

If you decide to exclude the Form_Paint procedure, you can create the form controls used in the example manually in Design view of your form:

   Form: Form1
   -----------------
   Option Group:
      Name: Frame0
   Option Button:
      Name: Option1
      OptionValue: 1
   Option Button:
      Name: Option2
      OptionValue: 2
   Option Button:
      Name: Option3
      OptionValue: 3
   Option Button:
      Name: Option4
      OptionValue: 4
   Option Button:
      Name: Option5
      OptionValue: 5
   Option Button:
      Name: Option6
      OptionValue: 6
   Command Button:
      Name: Command1
      OnClick: [Event Procedure]
   Common Dialog Control:
      Name: CommonDialog1
                

Replacing the Form_Paint Procedure

You can replace the Private Sub Form_Paint procedure with a standard procedure (a procedure in a module outside the form) that will programmatically create the form and the option button controls for you. The following code is an example of how to do this.

NOTE: This code is not considered an efficient solution if you are only creating a few forms. The following code becomes more useful if you need many forms. For example, the Microsoft Access Form Wizard uses similar code.

  1. Create a new module and type the following procedure:

       Sub CreateControls()
          Dim frm As Form
          Dim ctlOptGrp As Control, ctlOptBtn As Control, ctlLabel As Control
          Dim intLeft As Integer, intTop As Integer, intWidth As Integer
          Dim intHeight As Integer
          Dim i As Integer
    
          '''''''''''''''''''
          'Create a new form.
          '''''''''''''''''''
          Set frm = CreateForm
    
          ''''''''''''''''''''''''''''''''''''''''''
          'Set coordinates for Option Group control.
          ''''''''''''''''''''''''''''''''''''''''''
          intLeft = 1440 * 1.75
          intTop = 1440 * 0.25
          intWidth = 1440 * 1.5
          intHeight = 1440 * 1
    
          ''''''''''''''''''''''''''''''''''''''''''''''
          'Create an Option Group in the Detail section.
          ''''''''''''''''''''''''''''''''''''''''''''''
          Set ctlOptGrp = CreateControl(frm.Name, acOptionGroup, acDetail, _
             "", "", intLeft, intTop, intWidth, intHeight)
    
          ''''''''''''''''''''''''''''''''''''''''''''''''''
          'Initialize coordinates for Option Button controls.
          ''''''''''''''''''''''''''''''''''''''''''''''''''
          intLeft = 1440 * 2
          intTop = 1440 * 0.5
    
          ''''''''''''''''''''''''''''''''''''''''''''
          'Create Option Buttons for the Option Group.
          ''''''''''''''''''''''''''''''''''''''''''''
          For i = 1 To 6
             Set ctlOptBtn = CreateControl(frm.Name, acOptionButton, _
                acDetail, ctlOptGrp.Name, "", intLeft, intTop)
             ctlOptBtn.OptionValue = i
             intTop = intTop + (1440 * 0.25)
          Next i
    
          ''''''''''''''''''''''''''''''''''''''''''''''''
          'Initialize coordinates for Option Button labels.
          ''''''''''''''''''''''''''''''''''''''''''''''''
          intLeft = 1440 * 2.25
          intTop = 1440 * 0.455
    
          ''''''''''''''''''''''''''''''''''''''''''''''''''
          'Create Option Button Labels for the Option Group.
          ''''''''''''''''''''''''''''''''''''''''''''''''''
          Set ctlLabel = CreateControl(frm.Name, acLabel, acDetail, _
             "Option1", "Open", intLeft, intTop)
          intTop = intTop + (1440 * 0.25)
          Set ctlLabel = CreateControl(frm.Name, acLabel, acDetail, _
             "Option2", "Save", intLeft, intTop)
          intTop = intTop + (1440 * 0.25)
          Set ctlLabel = CreateControl(frm.Name, acLabel, acDetail, _
             "Option3", "Color", intLeft, intTop)
          intTop = intTop + (1440 * 0.25)
          Set ctlLabel = CreateControl(frm.Name, acLabel, acDetail, _
             "Option4", "Font", intLeft, intTop)
          intTop = intTop + (1440 * 0.25)
          Set ctlLabel = CreateControl(frm.Name, acLabel, acDetail, _
             "Option5", "Printer", intLeft, intTop)
          intTop = intTop + (1440 * 0.25)
          Set ctlLabel = CreateControl(frm.Name, acLabel, acDetail, _
             "Option6", "Help", intLeft, intTop)
    
          ''''''''''''''''''
          'Restore the form.
          ''''''''''''''''''
          DoCmd.Restore
       End Sub
                        
  2. Execute the procedure by typing the following line in the Debug window:

    CreateControls

  3. When the form appears in Design view, add command button and common dialog controls to it:

          Command Button:
             Name: Command1
             OnClick: [Event Procedure]
          Common Dialog Control:
             Name: CommonDialog1
                        

Modifying the Command1_Click Procedure

Whether you create the form and its controls manually or in code, you must also modify the Command1_Click procedure in the example so it does not reference the control array.

Note that the only difference in the following code from that in the Help example is that references to the Option1 control array are replaced with the value of the option group, and the name of the Help file is changed to a Microsoft Access Help file, Acmain80.hlp.

Type the following procedure in the class module of your form:

   Private Sub Command1_Click()
   If Frame0.Value = 1 Then   ' If Open option button selected,
      CommonDialog1.ShowOpen  ' display Open common dialog box.
   ElseIf Frame0.Value = 2 Then    ' Or,
      CommonDialog1.ShowSave  ' display Save common dialog box.
   ElseIf Frame0.Value = 3 Then   ' Or,
      CommonDialog1.ShowColor ' display Color common dialog box.
   ElseIf Frame0.Value = 4 Then   ' Or,
      CommonDialog1.Flags = cdlCFBoth ' Flags property must be set to
                                      ' cdlCFBoth, cdlCFPrinterFonts,
                                      ' or cdlCFScreenFonts before using
                                      ' ShowFont method.
      CommonDialog1.ShowFont  ' Display Font common dialog box.
   ElseIf Frame0.Value = 5 Then   ' Or,
      CommonDialog1.ShowPrinter   ' display Printer common dialog box.
   ElseIf Frame0.Value = 6 Then  ' Or,
      CommonDialog1.HelpFile = "ACMAIN80.HLP"
      CommonDialog1.HelpCommand = cdlHelpContents
      CommonDialog1.ShowHelp  ' Display Visual Basic Help contents topic.
   End If
   End Sub
                

MORE INFORMATION

Most of the Visual Basic Help files in Microsoft Access 97 come from the Microsoft Visual Basic product. When you view the "CommonDialog control" Help topic, you can click the Example jump at the top of the page to view the code example. The first procedure in the example is called:

Private Sub Form_Paint ()


A closer look at this procedure indicates that it will create an array of controls when a form is first opened. In other words, it will programmatically create option buttons on the form when it opens. This is indicated by the following code:

   For i = 1 To 5
      Load Option1(i)   ' Add five option buttons to array.
      Option1(i).Top = Option1(i - 1).Top + 350
      Option1(i).Visible = True
   Next i
                

If you use this example in Microsoft Visual Basic version 4.0 or later, you will find that the code works flawlessly. However, Visual Basic for Applications, the programming language that is included with Microsoft Access 97, is a subset of Microsoft Visual Basic. Visual Basic for Applications does not have the full functionality found in Microsoft Visual Basic. The ability to create control arrays is one of the features excluded from Visual Basic for Applications; therefore, so you cannot use the example for the CommonDialog control, as written, in a Microsoft Access 97 database.

REFERENCES

For more information about ActiveX components, search the Help Index for "ActiveX controls."


Additional query words: common dialog

Keywords: kbprogramming KB166291