The Color Changer

 

Introduction

Microsoft Access (and MSVB 6) ships with two main categories of controls. Regular controls display on the Toolbox. Additional controls are available as ActiveX objects. To access these controls, you can click the More Controls button on the Toolbox and select the one you want.

In this application, we will use three scroll bars to change the color of a frame. All these controls will be positioned on a form.

Creating the Application

This application is simply used to take advantage of some ActiveX controls that accompany MS Access. Most of these controls require VBA to implement their behavior.

Practical Learning: Starting the Exercise

  1. Start Microsoft Access and create a blank database named ColorChanger1
  2. On the main menu, click Insert -> Forms
  3. On the New Forms dialog box, make sure Design View is selected and make sure the combo box is empty:
     
  4. Click OK
  5. Display the Properties window. If you are using MS Access >= 2000, on the Properties window, set the Allow All Design Changes to Design View Only
  6. Save the form as frmColorChanger
  7. Change its properties as follows:
    Caption:                Color Changer
    Record Selectors:    No
    Navigation Buttons: No
    Border Style:          Dialog
  8. To add the first control, while the form is selected, on the Toolbox, click the More Controls button
  9. On the list, scroll down and click Microsoft Forms 2.0 Frame
  10. Then, click on the top-left section of the form
  11. Once again, on the Toolbox, click the More Controls button . Scroll down in the list and click Microsoft Forms 2.0 ScrollBar
  12. Click outside of the previously added frame on the form, for example on the right side
  13. Right-click the new scrollbar and click Copy. Right-click an empty area on the form and click Paste. Paste it again to have three scroll bars.
    Design the dialog box and change the properties of the controls as follows:
     
    Control Name Caption Min Max Value Align Text
    Frame fraPreview          
    ScrollBar scrRed   0 255 63  
    Label lblRed 000       Center
    ScrollBar scrGreen   0 255 63  
    Label lblGreen 000       Center
    ScrollBar scrBlue   0 255 63  
    Label lblBlue 000       Center
  14. While the form is still in Design View, on the Form Design toolbar, click the Code button to open MS Visual Basic 
  15. In the Object combo box, select Form and implement its Load() event as follows:
     
    Private Sub Form_Load()
        Rem Paint the frame with a default gray color
        fraPreview.BackColor = RGB(192, 192, 192)
    End Sub
  16. On the Object combo box, select scrRed and implement its Change() event as follows:
     
    Private Sub scrRed_Change()
        ' Create a color based on a combination of the values of the scroll bars
        ' Use that color to paint the frame
        fraPreview.BackColor = RGB(255 - scrRed.Value, _
                                   255 - scrGreen.Value, _
                                   255 - scrBlue.Value)
    
        ' Show the value of red degree of the current color
        lblRed.Caption = 255 - scrRed.Value
    End Sub
  17. On the Object combo box, select scrGreen and implement its Change() event as follows:
     
    Private Sub scrGreen_Change()
        fraPreview.BackColor = RGB(255 - scrRed.Value, _
                                   255 - scrGreen.Value, _
                                   255 - scrBlue.Value)
    
        lblGreen.Caption = 255 - scrGreen.Value
    End Sub
  18. On the Object combo box, select scrBlue and implement its Change() event as follows:
     
    Private Sub scrBlue_Change()
        fraPreview.BackColor = RGB(255 - scrRed.Value, _
                                   255 - scrGreen.Value, _
                                   255 - scrBlue.Value)
    
        lblBlue.Caption = 255 - scrBlue.Value
    End Sub
  19. Test the application
  20. After using it, close it and return to your programming environment.
 

Copyright 2003-2007 FunctionX, Inc.