Home

Introduction to Button-Based Windows Controls

The Button

Introduction

A button is an object or control the user clicks to produce an action. To support buttons, Microsoft Access VBA provides a class named Button.

Practical LearningPractical Learning: Introducing Windows Controls

  1. Start Microsoft Access
  2. In the list of files, click FunDS1 from the previous lesson
  3. In the Navigation Pane, right-click the Department Store form and click Design View

Creating a Button

To visually create a button, after displaying the form or report in the Design View, in the Controls section of the Ribbon, click the Button Button and click the form or report. To assist with the action that a button should perform, Microsoft Access provides a wizard that leads to a long list of action. You are encouraged to use that wizard. Of course, you can also take care of the actions yoursellf.

Practical LearningPractical Learning: Introducing Buttons

  1. In the Controls section of the Ribbon, click the Button Button
  2. Click the form.
    If a wizard starts, click Cancel
  3. While the button is still selected on the form, in the Property Sheet, click the Other tab and click Name
  4. Type cmdStoreItems and press Enter
  5. Save the Department Store form

Characteristics of a Button

The Caption of a Button

The primary visual aspect of a button is its caption. To support it, the Button class is equipped with a property named Caption. To visually set the caption of a button, access its Property Sheet. In the Format or the All property page, click Caption and type a string of your choice. To programmatically specify the caption of a button, access its Caption property and assign a string of your choice. Here is an example:

Private Sub cmdToday_Click()
    cmdToday.Caption = "Submit Time Sheet"
End Sub

Practical LearningPractical Learning: Setting the Caption of a Button

  1. While the button is still selected on the form, in the Property Sheet, click the Format tab and click Caption
  2. Press the Space bar twice and type Store Items...
  3. On the Ribbon, click Home
  4. In the Text Formatting section of the Ribbon, click the arrow of the Font combo box and select Bodoni MT Black (if you don't have that font, select Garamond or Times New Roman)
  5. Still in the Font section of the Ribbon, click the arrow of the Font Size button and select 20
  6. Save the form

The Size of a Button

The size of a button is primarily managed as done for the other controls. Furthermore, you can resize a button to fit its caption, you can proceed as done for a label: Select it and double-click one of its borders.

Practical Learning: Resizing a Button

  1. While the button is still selected on the form, double-click one of its borders
  2. Save the form

A Picture on a Button

To make your button appealing, you can make it display a small picture. To assist you with this the Button class is equipped with a property named Picture

To visually specify the picture of a button, access its Property Sheet. In the Format or the All property page, click Picture and click its ellipsis button Browse. To assist with pictures, Microsoft Access ships with many pre-designed icons. If you click the Picture field, the Picture Builder dialog box will present a list in the Available Pictures section:

Picture Builder

If you click an item in the Available Pictures list, its preview would display on the left side. If none of the pictures suits you, click Browse and select a picture. After selecting the picture, click OK.

You can display only the caption, only the picture, or both the picture and the caption on a button. If you decide to use both, to let you control the position of the picture with regards to the caption, the button has a property named Picture Caption Alignment in its Property Sheet.

Practical LearningPractical Learning: Displaying a Picture on a Button

  1. While the button is still selected on the form, in the Property Sheet, click the Format tab, click Picture, and click its ellipsis button Browse
  2. In the Available Pictures of the Picture Builder dialog box, scroll down and click MS Access Table:

    Picture Builder

  3. Click OK
  4. In the Property Sheet, in the Format tab, change the Picture Caption Alignment field to Right
  5. Right-click the button on the form and click Copy
  6. Right-click an unoccupied area of the form and click Paste
  7. While the newly pasted button is still selected on the form, in the Property Sheet, click the All tab and set the Name to Name: cmdShoppingSessions
  8. Click Caption and replace Store Items with Shopping Sessions
  9. Right-click one of the buttons on the form and click Copy
  10. Right-click an unoccupied area of the form and click Paste
  11. In the Property Sheet, change the Name to cmdEmployees
  12. Replace (only) the text of the Caption with Employees
  13. In the Controls section of the Ribbon, click the Button Button
  14. Click the form.
    If a wizard starts, click Cancel
  15. While the button is still selected on the form, in the Property Sheet, click the All tab and change the following characteristics:
    Name: cmdNewStoreItem
    Caption: New Store Item...
    Font Name: Bodoni MT Black (if you don't have that font, select Garamond or Times New Roman)
    Font Size: 20
  16. Save the form

Colors on a Button

Like all Windows controls in Microsoft Access, a button can be painted with a color specified in the Back Color field of its Property Sheet. The caption can also display with a font and a color of your choice. One way you can enhance the appearance of a button is to paint with different color based on how the user is currently using the control. To support this, the button is equipped with the following properties in the Format or the All tab of its Property Sheet:

Practical LearningPractical Learning: Using Colors on a Button

  1. While the new button is still selected on the form, in the Property Sheet, click the Format tab and change the following characteristics:
    Hover Color: Accent 1, Lighter 80%
    Pressed Color: #1F497D
    Hover Color: Accent 5, Darker 50%
    Pressed Fore Color: Background 1 (white)
  2. On the form, right-click the New Store Item button and click Copy
  3. Right-click an unoccupied area of the form and click Paste
  4. Right-click an unoccupied area of the form and click Paste
  5. Right-click an unoccupied area of the form and click Paste
  6. On the form, right-click the Tables label and click Copy
  7. Right-click an unoccupied area of the form and click Paste
  8. Click the newly pasted label to put into edit mode
  9. Replace its text with Forms and press Enter
  10. Complete the design of the form as follows:

    Fun Department Store - Database Switchboard

    Control Name Caption
    Label Label   Tables
    Label Label   Forms
    Line Line    
    Button Button cmdStoreItems Store Items...
    Button Button cmdNewStoreItem New Store Item...
    Button Button cmdShoppingSessions Shopping Sessions...
    Button Button cmdNewShoppingSession New Shopping Session...
    Button Button cmdEmployees Employees...
    Button Button cmdNewEmployee New Employee...
    Button Button cmdClose Close
  11. Save form
  12. On the Ribbon, click File and click Open
  13. In the list of files, click Whirl Water Shine from the previous lesson
  14. In the Navigation Pane, right-click Bill Preparation and click Design View
  15. In the Controls section of the Ribbon, click the Button Button and click below the Form Footer bar on the form
  16. Complete the design of the form as follows:

    Whirl Water Shine - Bill Preparation - Buttons Additions

    Control  Caption  Name  
    Button Button Submit cmdSubmit
    Button Button Close cmdClose
  17. Save the Bill Preparation form

Introduction to the Web Browser

Overview

A web browser is a control that shows a file. The file can be a regular picture or a web document that is identified as a Uniform Resource Locator (URL). A web browser is very flexible with the type of document it can show. Still, you must follow some rules to prepare the document.

The primary type of document intended for a browser is a webpage. You probably already know how to create such a document. A browser can also be asked to display a picture. The web browser of Microsoft Windows supports various or all types of pictures, including those with the extensions .bmp, .jpg, .jpeg, .gif, .png, etc.

Practical Learning: Introducing Web Browsers

  1. On the Ribbon, click File and click New
  2. Click Blank Desktop Database
  3. Set the file name to States Statistics2
  4. Click Create
  5. On the Ribbon, click File and click Options
  6. In the left list of the Access Options dialog box, click Current Database
  7. In the main list, click Overlapping Windows
  8. Click OK on the dialog box
  9. Click OK on the message box
  10. On the Ribbon, click File and click Close
  11. In the list of files, click StatesStatistics2
  12. On the Ribbon, click Create and click Table Design
  13. In the first field of the table, type StateName and press Tab
  14. In the Tools section of the Design tab of the Ribbon, click the Primary Key button Primary Key
  15. Change the fields as follows:
    Field Name Data Type Field Size Format Decimal Places Caption
    StateName   40     State Name
    Abbreviation   5     Abbrv
    AreaSqrMiles Number   Standard 0 Area in Sqr Miles
    AreaSqrKms Number   Standard 0 Area in Sqr Kms
    AdmissionUnionDate         Admission to Union
    AdmissionUnionOrder   Byte     Admission Order
    Capital   40      
    Region   25      
    StateWebsite Hyperlink       State's Website
    Wikipedia Hyperlink        
  16. Close the table
  17. When asked whether you want to save, click Yes
  18. Set the table name to States and click OK (the resources that accompany these lessons include a spreadsheet named States Statistics; you can open it and use it to create records for the States table)
  19. In the Navigation Pane, double-click the States table
  20. If necessary, on the Ribbon, click Home.
    In the Home tab of the Ribbon, change the following characteristics:
    Change the following characteristics:
    Font Name: Bookman Old Style (if you don't have that font, select Times New Roman)
    Font Color: Gold, Accent 4, Lighter 80% (Theme Colors: 8th column, 2nd row)
    Background Color: Blue, Accent 1, Darker 50% (Theme Colors: 5th column, 6th row)
    Alternate Row Color: Blue, Accent 1 (Theme Colors: 5th column, 1st row)

    States Stastistics - Table Formatting

  21. Close the States table
  22. On the Ribbon, click Create and click Form Design
  23. In the Property Sheet, set the Record Source to States
  24. Right-click the body of the form and click Form Header/Footer
  25. Drag the Form Footer down completely until it can't go any lower (it may go down to the 22 measure maximum)
  26. Save the form as States
  27. In the Tools section of the Design tab of the Ribbon, click Add Existing Fields
  28. In the Field List, double-click StateName, Abbreviation, AreaSqrMiles, AreaSqrKms, AdmissionUnionDate, AdmissionUnionOrder, Capital, and Region
  29. Double-click any control or section on the form to display the Property Sheet
  30. Complete the design of the form as follows:

    Web Browser

  31. Save the form

Creating a Browser

A web browser can be added to a form and not to a report. To get it, after displaying the form in Design View, in the Controls section of the Design tab of the Ribbon, click the Web Browser Control Web Browser Control and click the form.

Practical Learning: Introducing the Web Browser

  1. In the Controls section of the Ribbon, click the Web Browser Control Web Browser Control
  2. Click below the other controls on the form.
    If a wizard starts, click Cancel
  3. In the Controls section of the Ribbon, click the Web Browser Control Web Browser Control and click below the previously added browser on the form.
    If a wizard starts, click Cancel

Characteristics of a Web Browser

The Control Source of a Web Browser

Probably the most important detail of a web browser is the document it displays. This is specified by the Control Source property. You cannot create a web browser on a table. Instead, you can create a text-based field, then use it as the record source of a browser.

When performing data entry, enter the name of the file with extension, the complete path and name of the file, or the URL of the web page that the browser would display.

Practical Learning: Using a Web Browser

  1. On the form, click the first (or one of the previously added) web browser(s)
  2. In the Property Sheet, click Data.
    Click Control Source and set its value to StateWebsite
  3. On the form, click the other web browser
  4. In the Data tab of the Property Sheet, set the Control Source to Wikipedia
  5. Save the form
  6. On the Ribbon, click File and click Open
  7. In the list of files, click Exercise3 from Lesson 22
  8. On the Ribbon, click Create and click Form Design
  9. In the Property Sheet, change the following characteristics of the form:
    Record Selectors: No
    Navigation Buttons: No
  10. Right-click the form and click Form Header/Footer
  11. In the Property Sheet, change the Back Color of the Detail section to Background Form
  12. Save the form as Navigator
  13. In the Controls section of the Ribbon, click the Text Box and click the Form Header section of the form
  14. In the Property Sheet, change the characteristics of the text box as follows:
    Name: txtAddress
    Border Color: Text 1
    Horizontal Anchor: Both
  15. Change the caption of the accompanying label to Web/Document Address:
  16. In the Controls section of the Ribbon, click the Button and click the form.
    If a wizard starts, click Cancel
  17. In the Property Sheet, change the characteristics of the button as follows:
    Name: cmdShow
    Caption: Show
    Horizontal Anchor: Right
  18. In the Controls section of the Ribbon, click the Web Browser Control Web Browser Control and click the Detail section of the form.
    If a wizard starts, click Cancel
  19. In the Property Sheet, change the following characteristics of the browser:
    Name: wbNavigator
    Border Color: Text 1
    Horizontal Anchor: Both
    Vertical Anchor: Both
  20. Position the controls as follows:

    Web Browser

  21. On the form, right-click the Show button and click Build Event...
  22. In the Choose Builder dialog box, click Code Builder and click OK
  23. Implement the event as follows:
    Private Sub cmdShow_Click()
        Dim strAddress As String
        
        strAddress = txtAddress
        wbNavigator.ControlSource = "='" & strAddress & "'"
        txtAddress = strAddress
    End Sub
  24. Return to Microsoft Access and switch the form to Form View
  25. Type an address, such as http://www.microsoftaccess.com/, in the text box and click the Show button

    Web Browser

  26. Change the address to something else, such as https://www.google.com/#q=VBA+Access, and click Show

    Web Browser

  27. Save the form

Web Browser Events

The web browser has many events appropriate for its functionality:

The Events of a Web Browser

We already know that you can submit the path of a file or a URL to a browser. When a file path or a URL is given to a web browser, before it processes it, the control fires an event named On Before Navigate2. It appears as follows:

Private Sub wbNavigator_BeforeNavigate2(ByVal pDisp As Object, _
					URL As Variant, _
					flags As Variant, _
					TargetFrameName As Variant, _
					PostData As Variant, _
					Headers As Variant, _
					Cancel As Boolean)

End Sub

If there is no problem in this event, the control shows the file or the web page. When the control has finished displaying the document, the web browser fires the On Document Complete event. If there is a change on the document, the control fires an On Progress Change event.

When a web browser has received a file path or a URL, it makes an attempt to show that file or the web page. If it encounters a problem, it fires an On Navigation Error event.

At any time, and if you allow it, the user can change the document the control is displaying. When a new document must be displayed, the control fires an On Updated event.

Page Breaks

Introduction

If you create a form or report that has many or large objects, you can divide the form or report in sections called pages (this has nothing to do with the Page Header or the Page Footer sections of a form or report) . Then, you can ask Microsoft Access to display only the desired section when necessary. To make this possible, Microsoft Access provides a special control named Page Break.

As done for all controls, before creating a page break, you must display the form or report in the Design View. To get a page break, in the Controls section of the Ribbon, click the Insert Page Break control Insert or Remove Page Break and click the desired section of the form or report. Normally, you should add it to the Detail section. You can add as many page breaks as you need.

Practical Learning: Introducing Page Breaks

  1. On the Ribbon, click File and click Open
  2. In the list of files, click StatesStatistics2 used earlier
  3. In the Navigation Pane, right-click the States form and click Design View
  4. In the Controls section of the Ribbon, click the Insert Page Break control Insert or Remove Page Break
  5. On the form, click just below the top web browser and position it as follows:

    Page Breaks

  6. Scroll down on the form.
    In the Controls section of the Ribbon, click the Insert Page Break control Insert or Remove Page Break
  7. On the form, click between both web browsers and position it as follows:

    States Statistics - Page Breaks

  8. Save the form

Implementing Page Breaks

You can provide buttons that the user will click to access an area delimited by the page break. To support page breaks, the Form class is equipped with a method named GoToPage. In the same way, the static DoCmd is equipped with the same method. Its syntax is:

Form|DoCmd.GoToPage(ByVal PageNumber As Lond, _
		    Optional ByVal Right As Long = 0, _
		    Optional ByVal Down As Long = 0)

This method takes as argument the section to access. The first section, or the section above the first page breake has an index of 1, the second page break has an index of 2, and so on.

Practical Learning: Using Page Breaks

  1. In the Controls section of the Ribbon, click the Button Button and click below the Form Footer bar of the form.
    If a wizard starts, click Cancel
  2. Right-click the newly added button and click Copy
  3. Right-click an empty area of the Form Footer section and click Paste
  4. In the Property Sheet, change the characteristics of the new button as follows:
    Name: cmdStatesSummary
    Caption: Statistics Summary
  5. Right-click an empty area of the Form Footer section and click Paste
  6. In the Property Sheet, change the characteristics of the new button as follows:
    Name: cmdCharacteristics
    Caption: Characteristics
  7. Right-click an empty area of the Form Footer section and click Paste
  8. In the Property Sheet, change the characteristics of the new button as follows:
    Name: cmdStateWebsite
    Caption: State's Website
  9. Right-click an empty area of the Form Footer section and click Paste
  10. In the Property Sheet, change the characteristics of the new button as follows:
    Name: cmdWikipedia
    Caption: Wikipedia
  11. Right-click an empty area of the Form Footer section and click Paste
  12. In the Property Sheet, change the characteristics of the new button as follows:
    Name: cmdClose
    Caption: Close
  13. Right-click the Statistics Summary and click Build Event...
  14. In the Choose Builder dialog box, click Code Builder and click OK
  15. Implement the event as follows:
    Private Sub cmdCharacteristics_Click()
        DoCmd.GoToPage 1
    End Sub
  16. In the Object combo box, select mdStateWebsite and implement the event as follows:
    Private Sub cmdStateWebsite_Click()
        GoToPage 2
    End Sub
  17. In the Object combo box, select cmdWikipedia and implement the event as follows:
    Private Sub cmdWikipedia_Click()
        GoToPage 3
    End Sub
  18. In the Object combo box, select cmdStatesSummary and implement the event as follows:
    Private Sub cmdStatesSummary_Click()
        DoCmd.OpenTable "States"
    End Sub
  19. In the Object combo box, select cmdClose and implement the event as follows:
    Private Sub cmdClose_Click()
        DoCmd.Close
    End Sub
  20. Return to Microsoft Access and close the form
  21. When asked whether you want to save, click Yes
  22. In the Navigation Pane, double-click the States form
  23. Click the different buttons

    States Statistics - Page Breaks

    States Statistics - Page Breaks

    States Statistics - Page Breaks

  24. Close Microsoft Access
  25. When asked whether you want to save the form, click Yes

Previous Copyright © 2016-2022, FunctionX, Inc. Next