$#Overview of Solutions
Solutions is a self-documenting application that presents a collection of the forms, reports, and interface elements most commonly used to create applications. Many examples use features new to this version of Microsoft Access.
Who Should Use Solutions
You'll find the examples in Solutions more useful if you already know how to create queries, forms, reports, and macros and are familiar with the information on creating applications in Part 1, "Connecting the Dots," and Chapter 5, "Access Basic Fundamentals," in Building Applications.
Tips for Using Solutions
All the examples in this application are available from the Solutions form, which is displayed when you open the Solutions database. On the Solutions form, first select a category of examples in the upper list box. To open a sample form or report, double-click the example in the lower list box, or select the example and choose the OK button. Solutions automatically displays the sample form or report and its Highlights topic from Show Me, the Solutions Help system.
The Show Me Highlights topic identifies the techniques used to create the form or report. Click the technique you're interested in, and then follow the step-by-step instructions to see how the technique was applied to the form or report.
Tips for Using Show Me
The Show Me topic for a form or report is automatically displayed when the check box at the bottom of the Solutions form is selected. To display Show Me for a form in Form view or a report in Print Preview, press F1 or click the Show Me button on the toolbar.
The main features of Show Me are similar to those of Microsoft Access Help:
To use a keyword or phrase to look for a topic, click the Search button in the Show Me window.
To print a topic, choose Print Topic from the File menu in the Show Me window.
To keep the Show Me window on top, choose Always On Top from the Help menu in the Show Me window.
$#Highlights of the Limit List Contents Form
$#
The Limit List Contents form shows how to use an option group to limit the values displayed in a list box. This technique makes the Limit List Contents form work:
Displaying only the products in the selected category.
You can use the values of one control to determine the values displayed in a list. If the group of determining values is stable, such as the categories in the Northwind database, you can display them in an option group. For example, when you select a category in the Limit List Contents form, only products in that category are displayed.
Overview
Include the Category ID field in the underlying query for the Select Product list box. Set the criteria for Category ID to an expression that limits the list to those products whose Category ID matches the Category ID selected in the Select Category option group. Requery the product list after you select a category.
Objects Used in this Example
Type | Name | |
Table | Products | |
Query | Limit Product List 2 | |
Form | Limit List Contents | |
Step by Step 1 Create the Limit Product List 2 query. Table |
Field (Sort) | Criteria |
Products | Product Name (Ascending) | -- |
Product ID | -- | |
Category ID | Forms![Limit List Contents]![Select Category] | |
2 Design the Limit List Contents form. a) Set the following form properties. Property |
Setting | |
RecordSource | Products | |
Caption | Limit List Contents | |
DefaultView | Single Form | |
ViewsAllowed | Form | |
ShortcutMenu | No | |
ScrollBars | Neither | |
RecordSelectors | No | |
NavigationButtons | No | |
AutoCenter | Yes | |
b) Create an unbound option group, and set its Name property to Select Category. c) For each category in the option group, add an option button, and type the category name in each label. d) Set the OptionValue property for each option button to a Category ID. For example, the Category ID for Beverages is 1, so you should set the OptionValue property of the Beverages option button to 1. e) Set the DefaultValue of the option group to 1. f) Create an event procedure for the AfterUpdate property of the option group. g) In the Declarations section of the Form.Limit List Contents module, type: Option Explicit h) Create an unbound list box, and set its properties as follows. Property |
Value | |
Name | Select Product | |
RowSourceType | Table/Query | |
RowSource | Limit Product List 2 | |
ColumnCount | 2 | |
ColumnWidths | ;0 in | |
BoundColumn | 2 | |
Cross References For information on |
See | |
Creating event procedures | Building Applications, Chapter 3, "Introducing Access Basic," and Chapter 5, "Access Basic Fundamentals" | |
Creating forms | User's Guide, Chapter 15, "Designing Forms," Chapter 16, "Customizing Forms" | |
Creating queries | User's Guide, Chapter 11, "Designing Select Queries" | |
|
$#
Highlights of the Add All To List Form
$#Add "(all)" to a List
When you use a list box or combo box to enter selection criteria, you might want to be able to specify all records. On the Add All To List form, the list in the Select Customer combo box includes "(all)."
Overview
Create a function that adds "(all)" to the list in a combo box or list box. Create a list box or combo box, and set its RowSourceType property to the name of the function.
Objects Used in this Example
Object | Name |
Form | Add All To List |
Module | Add All To List |
Step by Step 1 Create the Add All To List module. a) In the Declarations section of the module, type: Option Explicit b) Create the AddAllToCompanyList function. 2 Create the Add All To List form. a) Set the following form properties. Property |
Setting |
RecordSource | Customers |
Caption | Add All To List |
DefaultView | Single Form |
ViewsAllowed | Form |
ShortcutMenu | No |
ScrollBars | Neither |
RecordSelectors | No |
AutoCenter | Yes |
b) Add a form header and footer to the form. Set the Height property of the form footer to 0. c) Create a combo box in the form header, and set its properties as follows. Property |
Setting |
Name | Select Customer |
RowSourceType | AddAllToCompanyList |
RowSource | -- |
ColumnCount | 2 |
ColumnWidths | 3 in;0 in |
BoundColumn | 2 |
LimitToList | Yes |
AfterUpdate | [Event Procedure] |
d) In the detail section of the form, add text boxes for the Customer ID, Company Name, Contact Name, Phone, and Fax fields. Cross References For information on |
See |
Creating event procedures | Building Applications, Chapter 3, "Introducing Access Basic," and Chapter 5, "Access Basic Fundamentals" |
Creating forms | User's Guide, Chapter 15, "Designing Forms," and Chapter 16, "Customizing Forms" |
|
$#
$#
Highlights of the Reattach NWIND Form$#
Attach Tables at StartupObject | Name | |
Macro | AutoExec | |
Module | Reattach NWIND | |
Step by Step 1 Create the Reattach NWIND module. a) Create the AreTablesAttached function. b) Create the GetMDBName function. c) Create the GetMDBName2 function. d) Create the StringFromSz function. 2 Create an AutoExec macro with the following action. Condition |
Action | Arguments |
Not AreTablesAttached() | DoMenuItem | Menu Bar: Database Menu Name: File Command: Close Database |
This action should be the first one in your AutoExec macro. You can add other actions after this one; for example, you can add an OpenForm action that displays a startup form or a main form. Cross References For information on |
See | |
Creating event procedures | Building Applications, Chapter 3, "Introducing Access Basic," and Chapter 5, "Access Basic Fundamentals" | |
Creating macros | User's Guide, Chapter 24, "Macro Basics" | |
$# AreTablesAttached() Function Function AreTablesAttached () As Integer ' Update connection information in attached tables. ' ' Number of attached tables for progress meter. Const MAXTABLES = 8 Const NONEXISTENT_TABLE = 3011 Const NWIND_NOT_FOUND = 3024 Const ACCESS_DENIED = 3051 Const READ_ONLY_DATABASE = 3027 Dim TableCount As Integer Dim FileName As String, SearchPath As String, Temp As String Dim ReturnValue As Variant, AccDir As String, I As Integer Dim MyTable As TableDef Dim MyDB As Database, MyRecords As Recordset Set MyDB = DBEngine.Workspaces(0).Databases(0) AreTablesAttached = True ' Continue if attachments are broken. On Error Resume Next ' Open attached table to see if connection information is correct. Set MyRecords = MyDB.OpenRecordset("Products") ' Exit if connection information is correct. If Err = 0 Then MyRecords.Close Exit Function End If ' Initialize progress meter. ReturnValue = SysCmd(SYSCMD_INITMETER, "Attaching tables", MAXTABLES) ' Get name of directory where MSACCESS.EXE is located. AccDir = SysCmd(SYSCMD_ACCESSDIR) ' See if NWIND.MDB is in default location, <Access directory>\sampapps. ' If not, use <Access directory> as starting place for OpenFile dialog. Temp = Dir$(AccDir & "sampapps\.") SearchPath = AccDir & IIf(Temp = "", "", "sampapps\") If (Dir$(SearchPath & "nwind.mdb") = "") Then FileName = GetMDBName() ' Display Open File dialog. FileName = Trim(FileName) If FileName = "" GoTo Exit_Failed ' User pressed Cancel. Else FileName = SearchPath & "nwind.mdb" End If ' Loop through all tables, reattaching those with nonzero-length Connect strings. TableCount = 1 ' Initialize TableCount for status meter. For I = 0 To MyDB.TableDefs.Count - 1 Set MyTable = MyDB.TableDefs(I) If MyTable.Connect <> "" Then MyTable.Connect = ";DATABASE=" & FileName Err = 0 MyTable.RefreshLink If Err <> 0 Then If Err = NONEXISTENT_TABLE Then MsgBox "File '" & filename & "' does not contain required table '" & MyTable.SourceTableName & "'", 16, "Can't Run Solutions" ElseIf Err = NWIND_NOT_FOUND Then MsgBox "You can't run Solutions until you locate NWIND.MDB", 16, "Can't Run Solutions" ElseIf Err = ACCESS_DENIED Then MsgBox "Couldn't open " & filename & " because it is read-only or it is located on a read-only share.", 16, "Can't Run Solutions" ElseIf Err = READ_ONLY_DATABASE Then MsgBox "Can't reattach tables because SOLUTION.MDB is read-only or is located on a read-only share.", 16, "Can't Run Solutions" Else MsgBox Error, 16, "Can't Run Solutions" End If AreTablesAttached = False GoTo Exit_Final End If TableCount = TableCount + 1 ReturnValue = SysCmd(SYSCMD_UPDATEMETER, TableCount) End If Next I GoTo Exit_Final Exit_Failed: MsgBox "You can't run Solutions until you locate NWIND.MDB", 16, "Can't Run Solutions" AreTablesAttached = False Exit_Final: ReturnValue = SysCmd(SYSCMD_REMOVEMETER) End Function
|
$#
GetMDBName2() Function
Private Function GetMDBName2 (gfni As wlib_GetFileNameInfo, ByVal fOpen As Integer) As Long
' This function acts as a cover to MSAU_GetFileName in MSAU200.DLL.
' wlib_GetFileName terminates all strings in gfni structure with nulls and
' then calls DLL version of function. Upon returning from MSAU200.DLL, null
' characters are removed from strings in gfni.
Dim lRet As Long
gfni.szFilter = RTrim$(gfni.szFilter) & Chr$(0)
gfni.szCustomFilter = RTrim$(gfni.szCustomFilter) & Chr$(0)
gfni.szFile = RTrim$(gfni.szFile) & Chr$(0)
gfni.szFileTitle = RTrim$(gfni.szFileTitle) & Chr$(0)
gfni.szInitialDir = RTrim$(gfni.szInitialDir) & Chr$(0)
gfni.szTitle = RTrim$(gfni.szTitle) & Chr$(0)
gfni.szDefExt = RTrim$(gfni.szDefExt) & Chr$(0)
lRet = wlib_MSAU_GetFileName(gfni, fOpen)
gfni.szFilter = StringFromSz(gfni.szFilter)
gfni.szCustomFilter = StringFromSz(gfni.szCustomFilter)
gfni.szFile = StringFromSz(gfni.szFile)
gfni.szFileTitle = StringFromSz(gfni.szFileTitle)
gfni.szInitialDir = StringFromSz(gfni.szInitialDir)
gfni.szTitle = StringFromSz(gfni.szTitle)
gfni.szDefExt = StringFromSz(gfni.szDefExt)
GetMDBName2 = lRet
End Function
$#
$#
StringFromSz() Function
Private Function StringFromSz (szTmp As String) As String
' If string terminates with nulls, return a truncated string.
Dim ich As Integer
ich = InStr(szTmp, Chr$(0))
If ich Then
StringFromSz = Left$(szTmp, ich - 1)
Else
StringFromSz = szTmp
End If
End Function
The Solutions Intro form shows how to create a form that users can turn off when they don't need to use it any more. This technique makes the Solutions Intro form work:
Creating a form that users can turn off.
$#Create a Form That Users Can Turn Off
You can display a form that provides introductory information about your application at startup and which users can choose not to display again. For example, the Solutions Intro form is displayed after the Startup form and before the Solutions main menu form every time you start the Solutions sample application. If you decide you don't want it displayed any more, you can select the check box at the bottom of the form.
Overview
Create a table with one Yes/No field. Create a form that has the following controls: labels that display information about your application, a check box bound to the Yes/No field, and a command button that closes the form. Create a main menu form. Create a startup form. In the OnTimer property of the startup form, modify the event procedure so that it uses the value in the Yes/No field to determine if the Solutions Intro form or the main menu form should be opened.
Objects Used in This Example
Object | Names |
Table | Options |
Form | Solutions, Solutions Intro, Startup |
Step by Step 1 Create the Options table. a) Create a field named Hide Solutions Intro, and set its data type to Yes/No. (You don't need to create a primary key for the table.) b) Switch to Datasheet view, and type No in the Hide Solutions Intro field. 2 Create the Solutions Intro form. a) Set the following form properties. Property |
Setting |
RecordSource | Options |
Caption | Solutions Intro |
DefaultView | Single Form |
ViewsAllowed | Form |
DefaultEditing | Can't Add Records |
ShortcutMenu | No |
ScrollBars | Neither |
RecordSelectors | No |
NavigationButtons | No |
AutoCenter | Yes |
BorderStyle | Thin |
MinButton | No |
MaxButton | No |
OnClose | [Event Procedure] |
b) Create labels to display information about the application. c) Create a check box. In the Caption property of its attached label, type: Don't display this form again. Then set the following properties for the check box. Property |
Setting |
Name | Hide Solutions Intro |
ControlSource | Hide Solutions Intro |
DefaultValue | 0 |
d) Create a command button, and set the following properties. Property |
Setting |
Name | OK |
Caption | OK |
Default | Yes |
OnClick | [Event Procedure] |
3 Create the Solutions form. 4 Create the Startup form. 5 Modify the event procedure of the OnTimer property of the Startup form. Cross References For information on |
See |
Creating event procedures | Building Applications, Chapter 3, "Introducing Access Basic," and Chapter 5, "Access Basic Fundamentals" |
Creating forms | User's Guide, Chapter 15, "Designing Forms," and Chapter 16, "Customizing Forms" |
|
$#
$#
$#
Highlights of the Sales Averages Report$#
Create a Multiple-Fact Crosstab ReportObject | Names | ||
Table | Employees, Orders | ||
Query | Order Subtotals, Sales Averages | ||
Report | Sales Averages | ||
Step by Step 1 Import the Order Subtotals query from NWIND.MDB, if you haven't already done so. 2 Create the Sales Averages query. Table/Query |
Field/Expression | Total | Criteria |
Employees | Last Name First Name |
Group By Group By |
-- -- |
Orders | Order ID Shipped Date Year: Format([Shipped Date], "yyyy") |
Group By Group By Expression |
-- -- "1993" |
Order Subtotals | Subtotal | Group By | -- |
3 Design the Sales Averages report. a) Set the following sorting and grouping properties. Field/Expression |
Sort Order | Group Properties | |
Last Name | Ascending | Group Header: Yes Group Footer: Yes Group On: Each Value Group Interval: 1 Keep Together: No |
|
Shipped Date | Ascending | Group Header: Yes Group Footer: No Group On: Month Group Interval: 1 Keep Together: No |
|
b) Set the Width property of the report to 9.7813 inches. c) Set the following Print Setup options. Option |
Setting | ||
Orientation | Landscape | ||
Margins | Left: 0.4000 in Right: 0.4000 in Top: 1 in Bottom: 1 in |
||
Items Across: | 14 | ||
Column Spacing | 0.1 in | ||
Item Size | Width: 0.6 in Height: 0 in |
||
Item Layout | Horizontal | ||
d) In the page header, paste the labels for the type of currency and the names of the months. e) In the Last Name header, create the following text boxes, one below the other. Name |
ControlSource | ||
Salesperson Last Name | =[Last Name]&"," | ||
First Name | First Name | ||
f) In the Last Name header, create the following labels on top of the text boxes you created in Step 3e. Name |
Caption | ||
L - Minimum | Minimum: | ||
L - Average | Average: | ||
L - Maximum | Maximum: | ||
g) In the Last Name header, create the following text boxes. Name |
Other Properties | ||
Print What | ControlSource: -- Visible: No |
||
Column Width | ControlSource: =(0.6+0.1)*1440 Format: General Number Visible: No |
||
Left Margin | ControlSource: =0.4*1440 Format: General Number Visible: No |
||
h) In the Shipped Date header, create the following text boxes, one below the other. For all three, set the Format property to Standard, and the DecimalPlaces property to 0. Name |
ControlSource | ||
Month Minimum | =Min([Subtotal]) | ||
Month Average | =Avg([Subtotal]) | ||
Month Maximum | =Max([Subtotal]) | ||
i) Set the following section properties. Section |
Property | Setting | |
Page header | OnFormat | [Event Procedure] | |
Last Name header | KeepTogether OnFormat |
Yes [Event Procedure] |
|
Shipped Date header | KeepTogether OnFormat |
Yes [Event Procedure] |
|
Detail section | Height | 0 in | |
Last Name footer | NewRowOrCol Height |
After Section 0 in |
|
Report footer | Height | 0 in | |
j) In the Declarations section of the Report.Sales Averages module, type: Option Explicit Cross References For information on |
See | ||
Creating event procedures | Building Applications, Chapter 3, "Introducing Access Basic," and Chapter 5, "Access Basic Fundamentals" | ||
Creating queries | User's Guide, Chapter 10, "Query Basics," and Chapter 11, "Designing Select Queries" | ||
Creating reports | User's Guide, Chapter 20, "Report Basics," and Chapter 21, "Designing Reports" | ||
Setting sorting and grouping properties | User's Guide, Chapter 22, "Sorting and Grouping Data" | ||
$# Format Event Procedure for the Page Header Sub PageHeader1_Format (Cancel As Integer, FormatCount As Integer) ' Set Print What text box to 0 at top of page. Me![Print What] = 0 End Sub
|
$#
$#
Highlights of the Customer Phone List
$#Print the First and Last Entries on a Page in the Page Header
Just as dictionaries make words easy to find, your reports can make data easy to find by identifying the first and last entries on a page. On the Customer Phone List, the first company name on a page is printed on the left side of the page header, and the last company name is printed on the right.
Overview
Create a report that sorts data by company name. In the page footer, create a text box for page numbers that uses the Pages property in its expression (to force two-pass reporting). Create an event procedure for the OnFormat property of the page footer that, on the first pass, puts the customer name in the last record on the page into a global array. Create an event procedure for the OnFormat property of the report footer that, on the first pass, puts the last record in the recordset into the array. Create an event procedure for the OnFormat property of the page header that, on the second pass, fills in the First Entry and Last Entry text boxes.
Objects Used in this Example
Object | Name | |
Table | Customers | |
Report | Customer Phone List | |
Step by Step a) Set the report's RecordSource property to Customers. b) Add a report header and footer. Set the Height properties of both sections to 0. c) In the Sorting And Grouping box, select Company name in the Field/Expression column, and set Sort Order to Ascending. d) In the detail section, create the Company Name, Contact Name, and Phone text boxes. Cut their attached labels, and paste them in the page header. e) Set the KeepTogether property of the detail section to Yes. f) Create text boxes in the following sections and delete their attached labels. Section |
Name | ControlSource |
Page header | First Entry | -- |
Last Entry | -- | |
Page footer | Page Number | =Page & " of " & Pages & " Pages" |
g) Make the Company Name, First Entry, and Last Entry text boxes the same width. h) In the Declarations section of the Report.Customer Phone List module, declare variables. i) Create event procedures for the following sections. Section |
Property | Setting |
Page header | OnFormat | [Event Procedure] |
Page footer | OnFormat | [Event Procedure] |
Report footer | OnFormat | [Event Procedure] |
Cross References For information on |
See | |
Creating event procedures | Building Applications, Chapter 3, "Introducing Access Basic," and Chapter 5, "Access Basic Fundamentals" | |
Creating reports | User's Guide, Chapter 20, "Report Basics," and Chapter 21, "Designing Reports" | |
Setting sorting and grouping properties | User's Guide, Chapter 22, "Sorting and Grouping Data" | |
|
$
#$#
$#
$#
Highlights of the Edit Products Form
$#Prevent Users from Automatically Moving to Another Record
On an edit-only form, you might want to prevent users from automatically advancing to another record. On the Edit Products form, for example, you can't advance to another record in the usual ways, such as by pressing tab or enter in the last field, by using the navigation buttons, or by using menu commands or toolbar buttons.
Overview
Remove the navigation buttons from the form. Create a transparent command button, and use an event procedure in its OnEnter property that uses the SetFocus method to move the focus to the Supplier ID combo box. Place the command button at the end of the tab order.
Objects Used in this Example
Type | Name |
Table | Products |
Form | Edit Products |
Step by Step 1 Design the Edit Products form. a) Set the following form properties. Property |
Value |
RecordSource | Products |
Caption | Edit Products |
DefaultView | Single Form |
ViewsAllowed | Form |
DefaultEditing | Can't Add Records |
ShortcutMenu | No |
ScrollBars | Neither |
RecordSelectors | No |
NavigationButtons | No |
AutoCenter | Yes |
b) In the detail section, add the fields from the Products table. Use combo boxes for the Supplier ID and Category ID fields and a check box for the Discontinued field. Use text boxes for all the other fields. c) Set the Enabled property for all controls in the detail section to No. To prevent the labels from flashing when the controls are enabled, separate the attached labels from the controls by cutting the labels and then pasting them. d) Because it's a Counter field, make the Product ID text box display-only by setting the following properties. Property |
Value |
BackColor | (same color as Detail section's BackColor) |
BorderStyle | Clear |
FontWeight | Bold |
2 In the detail section, create the Go To Supplier ID command button. a) Set its properties and create an event procedure as follows. Property |
Value |
Name | Go To Supplier ID |
Transparent | Yes |
Enabled | No |
OnEnter | [Event Procedure] |
b) To prevent users from inadvertently clicking the command button, hide it behind the Supplier ID combo box. (Make the command button the same height as the combo box, put it on top of the combo box, and then choose Send To Back from the Format menu.) 3 In the Declarations section of the Form.Edit Products module, type: Option Explicit 4 Edit the tab order for the form. Make sure that Supplier ID is the first field in the list and that Go To Supplier ID and Product ID are the last two fields in the list. Tip To move from record to record on this form, you enter search criteria. Cross References For information on |
See |
Creating event procedures | Building Applications, Chapter 3, "Introducing Access Basic," and Chapter 5, "Access Basic Fundamentals" |
Creating forms | User's Guide, Chapter 15, "Designing Forms," and Chapter 16, "Customizing Forms" |
|
$#
Create a Toolbar and Menu That Include Only Editing TasksObject | Name | |||
Form | Edit Products | |||
Macro | Edit Products Menu Bar, Edit Products Menu Bar_File, Edit Products Menu Bar_File | |||
Step by Step 1 Create the Edit Products form. 2 Create a custom toolbar. a) Create a toolbar, and name it Edit Products Toolbar. b) Include these built-in buttons on the toolbar: Cut |
Copy | Paste | Undo | Undo Current Record |
Note The custom toolbars for the forms in Solutions also include the Design View and Form View buttons so that you can easily switch between views while you're looking at the examples. In most cases, you won't include these buttons on custom toolbars you create for your applications. c) To display the custom toolbar and hide the built-in toolbar, create event procedures for the following form properties. Property |
Setting | |||
OnActivate | [Event Procedure] | |||
OnDeactivate | [Event Procedure] | |||
3 Create a custom menu. a) Use the Menu Builder to create a menu bar named Edit Products Menu Bar. Use the Form menu bar as a template, and remove all menus and commands except the ones listed in the following table. Menu |
Command | |||
File | Close | |||
Edit | Undo Undo Current Field Cut Copy Paste |
|||
Note The custom menu bar in the Edit Products form also includes the View, Window, and Help menus so that you can switch easily between views and use the regular features of the Form View menu. b) Set the form's MenuBar property to the macro group, Edit Products Menu Bar. Cross References For information on |
See | |||
Creating event procedures | Building Applications, Chapter 3, "Introducing Access Basic," and Chapter 5, "Access Basic Fundamentals" | |||
Creating menus | Building Applications, Chapter 2, "Structuring a User Interface" | |||
Creating toolbars | User's Guide, Appendix B, "Working with Toolbars" | |||
|
$#
$#
Limit the Contents of One List Based on the Value Selected in AnotherType | Names | |
Table | Categories, Products | |
Query | Category List, Limit Product List | |
Form | Edit Products | |
Module | Enable Controls | |
Step by Step 1 Create the Category List query. Table |
Field (Sort) | |
Categories | Category Name (Ascending) | |
Category ID | ||
2 Create the Limit Product List query. Table |
Field (Sort) | Criteria |
Products | Product Name (Ascending) | -- |
Product ID | -- | |
Category ID | Forms![Edit Products]![Select Category] | |
3 Create the Enable Controls module, if you haven't already done so. a) In the Declarations section, type: Option Explicit b) Create the EnableControls function. 4 Modify the Edit Products form. a) Add a form header and footer. Set the Height property of the form footer to 0. b) Create the Select Category combo box in the form header. Set its properties and create an event procedure as follows. Property |
Value | |
Name | Select Category | |
RowSourceType | Table/Query | |
RowSource | Category List | |
ColumnCount | 2 | |
ColumnWidths | ;0 in | |
BoundColumn | 2 | |
LimitToList | Yes | |
AfterUpdate | [Event Procedure] | |
c) Create the Select Product combo box in the form header. Separate the attached label by cutting and pasting it (so that it doesn't flash when Select Product is enabled and disabled). Set the properties of the Select Product combo box as follows. Property |
Value | |
Name | Select Product | |
RowSourceType | Table/Query | |
RowSource | Limit Product List | |
ColumnCount | 2 | |
ColumnWidths | ;0 in | |
BoundColumn | 2 | |
LimitToList | Yes | |
Enabled | No | |
Tip On an edit-only form, you can prevent users from inadvertently making changes to the wrong record by disabling the controls in the detail section while they enter search criteria and enabling the controls after they find the record. Cross References For information on |
See | |
Creating event procedures | Building Applications, Chapter 3, "Introducing Access Basic," and Chapter 5, "Access Basic Fundamentals" | |
Creating forms | User's Guide, Chapter 16, "Customizing Forms" | |
Creating queries | User's Guide, Chapter 11, "Designing Select Queries" | |
|
$#
$#
AfterUpdate Event Procedure for the Select Category Combo Box
Sub Select_Category_AfterUpdate ()
' Enable and requery Select Product combo box.
' Disable controls in detail section.
Dim Tmp As Variant
Me![Select Product].Enabled = True
Me![Select Product].Requery
Tmp = EnableControls("Detail", False)
End Sub
A fast way to find a record is to select a value from a list. For example, the Edit Products form finds the record for the product you select in the Select Product combo box.
Tip On an edit-only form, you can prevent users from inadvertently making changes to the wrong record by disabling the controls in the detail section while they enter search criteria and enabling the controls after they enter it.
Overview
Create an event procedure for the AfterUpdate property of the Select Product combo box that uses the ApplyFilter command to find the record and a custom function to enable each control in the detail section.
Objects Used in this Example
Type | Name |
Table | Products |
Form | Edit Products |
Module | Enable Controls |
Step by Step 1 Create the Select Category and Select Product combo boxes. 2 Create the Enable Controls module, if haven't already done so. a) In the Declarations section, type: Option Explicit b) Create the EnableControls function. 3 Create an event procedure for the AfterUpdate property of the Select Product combo box. Cross References For information on |
See |
Creating event procedures | Building Applications, Chapter 3, "Introducing Access Basic," and Chapter 5, "Access Basic Fundamentals" |
|
$#
Requery a List After You Move to Another RecordType | Name |
Form | Edit Products |
Step by Step Cross References For information on |
See |
Creating event procedures | Building Applications, Chapter 3, "Introducing Access Basic," and Chapter 5, "Access Basic Fundamentals" |
|
$
#Change the Color of a Control and Display a Message Based on the Control's ValueType | Name | |
Form | Edit Products | |
Step by Step 1 Create a label beside the Units In Stock text box. Set its properties as follows. Property |
Setting | |
Name | L - Low Stock | |
Caption | Units in stock are below the reorder level. | |
Visible | No | |
ForeColor | 128 | |
2 Create event procedures for the form and controls as follows. Form/Control |
Property | Setting |
Edit Products form | OnCurrent | [Event Procedure] |
Units In Stock text box | AfterUpdate | [Event Procedure] |
Units On Order text box | AfterUpdate | [Event Procedure] |
Reorder Level text box | AfterUpdate | [Event Procedure] |
Cross References For information on |
See | |
Creating event procedures | Building Applications, Chapter 3, "Introducing Access Basic," and Chapter 5, "Access Basic Fundamentals" | |
Creating forms | User's Guide, Chapter 15, "Designing Forms" | |
|
$#
$#
$#
$#
Highlights of the Employee Sales Report
$#Create a Crosstab Report with Dynamic Column Headings
You can vary the number of columns you print in a crosstab report depending on criteria you enter when you print it. For example, in the Employee Sales report, not every employee has sales each week, so the report prints columns for only those employees who had sales for the specified time period. This report also calculates totals for each row and column.
Overview
Create a crosstab query to use as the record source for the report. In the report design, create unbound text boxes in the page header (for column headings), in the detail section (for the row heading and crosstab values), and in the report footer (for column totals). In the OnFormat properties of the page header and detail section, create event procedures that fill the text boxes with the column headings and crosstab values and hide the unused text boxes. In the OnPrint property of the detail section, create an event procedure that calculates the row total, and adds it to the column-total array. In the OnPrint property of the report footer, create an event procedure that fills the text boxes with the column totals.
Objects Used in this Example
Object | Names | |
Table | Employees, Orders, Products | |
Query | Employee Sales, Order Details Extended | |
Report | Employee Sales | |
Step by Step 1 Import the Order Details Extended query from NWIND.MDB, if you haven't already done so. 2 Create Employee Sales, a crosstab query. Table/Query |
Field | Properties |
Employees table | Last Name | Total: Group By Crosstab: Column Heading |
Products table | Product Name | Total: Group By Crosstab: Row Heading |
Order Details Extended query | Order Amount: Extended Price | Total: Sum Crosstab: Value |
Orders table | Shipped Date | Total: Where Criteria: Between #1/2/93# And #1/16/93# |
Note If you look at the criteria for the Shipped Date field in the Employee Sales query, you'll see that it references controls on the Employee Sales Dialog Box. The steps to modify the query are covered in Set Report Criteria in a Dialog Box. 4 Design the Employee Sales report. a) Set the report's RecordSource property to Employee Sales. b) Add a report header and footer. Create a title for the report in the report header. c) Create 11 unbound text boxes in the page header. These text boxes will display the column headings. Set the Name property of the leftmost text box to Head1, the Name property of the next text box to Head2, and so on through Head11. d) Create 11 unbound text boxes in the detail section. (Create one text box for the row heading, nine text boxes for the maximum number of salespeople at Northwind Traders, and one text box for the row total.) Set the Name property of the leftmost text box to Col1, the Name property of the next text box to Col2, and so on through Col11. Set the Format property of text boxes Col2 through Col11 to Standard. e) Create 11 text boxes in the report footer. Set the Name property of the leftmost text box to Tot1 and its ControlSource property to: ="Totals:" Set the Name property of the remaining text boxes, which display the column totals, to Tot2 through Tot11. Set the Format property of text boxes Tot2 through Tot11 to Standard. f) In the General section of the Report.Employee Sales module, declare the variables,and create the InitVars sub and xtabCNulls function. g) Set properties and create event procedures as described in the following table. Report or Section |
Property | Setting |
Report | OnOpen | [Event Procedure] |
OnClose | [Event Procedure] | |
Report header | OnFormat | [Event Procedure] |
Page header | OnFormat | [Event Procedure] |
Detail section | KeepTogether | Yes |
OnFormat | [Event Procedure] | |
OnPrint | [Event Procedure] | |
OnRetreat | [Event Procedure] | |
Report footer | OnPrint | [Event Procedure] |
Cross References For information on |
See | |
Creating crosstab queries | User's Guide, Chapter 12, "Advanced Queries" | |
Creating event procedures | Building Applications, Chapter 3, "Introducing Access Basic," and Chapter 5, "Access Basic Fundamentals" | |
Creating reports | User's Guide, Chapter 20, "Report Basics," and Chapter 21, "Designing Reports" | |
|
$#
$#
$#
$#
$#
$#
$#
$#
$#
$#
$#
Highlights of the Employee Sales Dialog Box
$#Set Report Criteria in a Dialog Box
Instead of setting the criteria for a report in its underlying query, you can display a dialog box that prompts the person using the report to enter the criteria just before printing. A report is more flexible when its criteria is set at run time because users have control over the range of data they print. In the Employee Sales Dialog Box form, a user specifies the beginning and ending dates as the selection criteria.
Overview
Create a dialog box that contains the controls for entering the report's criteria. Add the Preview, Print, and Cancel buttons. In the report's underlying query, set the query parameters and the criteria for the date field using an expression that includes the Forms object, the name of the form, and the names of the controls. In the event procedure for the report's OnOpen property, specify the parameters for the QueryDef .
Objects Used in this Example
Object | Name |
Form | Employee Sales Dialog Box |
Report | Employee Sales |
Module | Is Loaded |
Step by Step 1 Create the Employee Sales report. 2 Create the Employee Sales Dialog Box form. a) Create an unbound form, and set the following form properties. Property |
Setting |
Caption | Employee Sales |
DefaultView | Single Form |
ViewsAllowed | Form |
ShortCutMenu | No |
ScrollBars | Neither |
RecordSelectors | No |
NavigationButtons | No |
AutoCenter | Yes |
Modal | Yes |
BorderStyle | Thin |
MinButton | No |
MaxButton | No |
b) Create two unbound text boxes. Set their Format properties to Medium Date, and set the Caption properties for the labels and the Name properties for the text boxes as follows. Caption for label |
Name for text box |
Enter a beginning date: | Beginning Date |
Enter an ending date: | Ending Date |
c) Create the following command buttons, setting their Name, Caption, and OnClick properties as follows. Also, set the Default property of the Preview button to Yes. Name and Caption |
OnClick |
Preview | [Event Procedure] |
[Event Procedure] | |
Cancel | [Event Procedure] |
d) In the Declarations section of the Form.Employee Sales Dialog Box module, type: Option Explicit Const ERR_REP_CANCELED = 2501 If you use the Control Wizards to create the command buttons, the Wizard automatically adds the Option Explicit statement. 3 Modify the Employee Sales query. a) In the Criteria box for the Shipped Date field, type: Between [Forms]![Employee Sales Dialog Box]![Beginning Date] And [Forms]![Employee Sales Dialog Box]![Ending Date] b) Set query parameters as follows. Parameter |
Data Type |
Forms!Employee Sales Dialog Box!Beginning Date | Date/Time |
Forms!Employee Sales Dialog Box!Ending Date | Date/Time |
4 Create the Is Loaded module, if you haven't already done so. a) In the Declarations section of the module, type: Option Explicit b) Create the IsLoaded function. 5 Modify the Open event procedure of the Employee Sales report. Cross References For information on |
See |
Creating event procedures | Building Applications, Chapter 3, "Introducing Access Basic," and Chapter 5, "Access Basic Fundamentals" |
Creating forms | User's Guide, Chapter 14, "Form Basics," and Chapter 15, "Designing Forms" |
Creating queries | User's Guide, Chapter 12, "Advanced Queries" |
|
$#
$#
$#
$#
$#Print Criteria from a Report's Criteria Dialog Box on the Report
To improve the functionality of a report, you can print the criteria you typed in the report's print dialog box on the report. For example, the values you enter in the Beginning Date and Ending Date text boxes on the Employee Sales Dialog Box appear in the report header of the Employee Sales report.
Overview
Create an unbound text box on the report. Set its ControlSource property to an expression that returns the values entered in the controls in the report's criteria dialog box.
Objects Used in this Example
Object | Name |
Form | Employee Sales Dialog Box |
Report | Employee Sales |
Step by Step 1 Create the Employee Sales report. 2 Create the Employee Sales Dialog Box. 3 Modify the Employee Sales report. Create an unbound text box in the report header. Set its Name property to Range and its ControlSource property to: ="From " & Format(Forms![Employee Sales Dialog Box]![Beginning Date], "dd-mmm-yy") & " through " & Format(Forms![Employee Sales Dialog Box]![Ending Date], "dd-mmm-yy") Cross References For information on |
See |
Creating expressions | Users's Guide, Chapter 23, "Using Expressions in Reports" |
Creating reports | User's Guide, Chapter 20, "Report Basics," and Chapter 21, "Designing Reports" |
|
$#Create a Form in Which You Can Enter or Edit Data
You can have easy access to data by creating a form in which you can switch quickly between entering new records and editing existing ones. For example, the Enter Or Edit Products form opens in data entry mode. To edit records, click the Edit Data button on the toolbar. To switch back to entering records, click the Enter Data button on the toolbar.
Overview
Design the form so that it opens in data entry mode. In the form header, add the combo boxes in which you enter search data, and disable them. Create functions for two custom toolbar buttons, Edit Data and Enter Data. In the functions, set the AllowEditing property of the form so that, in the Activate and Deactivate events of the form, you can use the value of the property as a flag to hide and show the correct toolbar. Create macros to run the functions. Create two toolbars, one that includes the Edit Data button and is displayed when the form is in data entry mode, and another that includes the Enter Data button and is displayed when the form is in edit mode.
Objects Used in this Example
Object | Names | ||||
Table | Products | ||||
Form | Enter Or Edit Products | ||||
Macro | Enter Or Edit Products | ||||
Module | Enable Controls, Enter Or Edit Products Buttons | ||||
Step by Step 1 Design the Enter Or Edit Products form. a) Set the following form properties. Property |
Setting | ||||
RecordSource | Products | ||||
Caption | Enter Or Edit Products | ||||
DefaultView | Single Form | ||||
ViewsAllowed | Form | ||||
DefaultEditing | Data Entry | ||||
ShortcutMenu | No | ||||
Scrollbars | Neither | ||||
RecordSelectors | No | ||||
b) In the detail section, add the fields from the Products table. Use a check box for the Discontinued field and combo boxes for the Supplier ID and Category ID fields. Use text boxes for all the other fields. c) To prevent the labels from flashing when the controls in the detail section are enabled and disabled, separate the attached labels from the controls by cutting the labels and then pasting them. d) Because it's a Counter field, make the Product ID text box display-only by setting the following properties: Property |
Value | ||||
Enabled | No | ||||
BackColor | (same color as detail section's BackColor) | ||||
BorderStyle | Clear | ||||
FontWeight | Bold | ||||
2 Add a form header, and create the Select Category and Select Product combo boxes. The procedure for creating the combo boxes was written for the Edit Products form, so you will need to make the following changes. a) Set the RowSource property of the Select Product combo box to: SELECT DISTINCTROW Products.[Product Name], Products.[Product ID], Products.[Category ID] FROM Products WHERE (Products.[Category ID]=[Forms]![Enter Or Edit Products]![Select Category]) ORDER BY Products.[Product Name]; b) Create an event procedure in the AfterUpdate property of the Select Product combo box. c) Set the Enabled property of both combo boxes to No. d) To prevent flashing when the combo boxes are enabled and disabled, separate the attached labels from both combo boxes by cutting and pasting the labels. 3 Create the Enable Controls module, if you haven't already done so. a) In the Declarations section of the module, type: Option Explicit b) Create the EnablesControls function. 4 Create the Enter Or Edit Products Buttons module. a) In the Declarations section of the module, type: Option Explicit b) Create the EditData function. c) Create the EnterData function. 5 Create the following macros in a macro group named Enter Or Edit Products. Macro Name |
Action | Argument | |||
Enter Data | RunCode | EnterData() | |||
Edit Data | RunCode | EditData() | |||
6 Create custom toolbars. a) Create a toolbar, and name it Enter Or Edit Products 1. b) Include these built-in buttons on the toolbar: New |
Cut | Copy | Paste | Undo | Undo Current Record |
c) Add a custom button to the toolbar. Macro |
Button Text | Button Description | |||
Enter Or Edit Products.Edit Data | Edit Data | Edit Data | |||
d) Create a toolbar and name it Enter Or Edit Products 2. e) Include these built-in buttons on the toolbar: Cut, Copy, Paste, Undo, and Undo Current Record. f) Add a custom button to the toolbar. Macro |
Button Text | Button Description | |||
Enter Or Edit Products.Enter Data | Enter Data | Enter Data | |||
Note The custom toolbars for the forms in Solutions also include the Design View and Form View buttons so that you can easily switch between views while you're looking at the examples. In most cases, you won't include these buttons on the toolbars you create for your applications. 7 Create event procedures for the following form properties. Property |
Setting | ||||
OnOpen | [Event Procedure] | ||||
OnActivate | [Event Procedure] | ||||
OnDeactivate | [Event Procedure] | ||||
8 In the Declarations section of the Form.Enter Or Edit Products module, type: Option Explicit Cross References For information on |
See | ||||
Creating event procedures | Building Applications, Chapter 3, "Introducing Access Basic," and Chapter 5, "Access Basic Fundamentals" | ||||
Creating forms | User's Guide, Chapter 15, "Designing Forms," and Chapter 16, "Customizing Forms" | ||||
Creating macros | User's Guide, Chapter 24, "Macro Basics" | ||||
Creating toolbars | User's Guide, Appendix B, "Working with Toolbars" | ||||
|
$#
$#
$#
$#
$#
$#
Control When You Go to a New RecordType | Name |
Table | Products |
Form | Enter Or Edit Products |
Step by Step 1 Set the form's NavigationButtons property to No. 2 In the detail section, create the Go To Supplier ID command button. a) Set its properties and create an event procedure as follows. Property |
Setting |
Name | Go To Supplier ID |
Transparent | Yes |
OnEnter | [Event Procedure] |
b) To prevent users from inadvertantly clicking the command button, hide it behind the Supplier ID combo box. (Make the command button the same height as the combo box, put it on top of the combo box, and then select Send To Back from the Format menu.) 3 Edit the tab order for the form. Make sure Supplier ID is the first field in the list and Go To Supplier ID and Product ID are the last two fields in the list. Cross References For information on |
See |
Creating event procedures | Building Applications, Chapter 3, "Introducing Access Basic," and Chapter 5, "Access Basic Fundamentals" |
Creating forms | User's Guide, Chapter 15, "Designing Forms" |
Creating toolbars | User's Guide, Appendix B, "Working with Toolbars" |
|
$#Add a New Record to a List
As you enter or edit records, you might need to add a new item to the list in a combo box. For example, on the Enter Or Edit Products form, you can add a new category to the list by typing the name in the combo box. The form displays a message box asking if you want to add a new record. If you choose Yes, it displays a form in which you enter the information for the new category.
Overview
Create an event procedure for the OnNotInList property of the Category ID combo box on the Enter Or Edit Products form. (This event procedure displays a dialog box in which you can confirm that you want to add a new category; if you choose Yes, it opens the Add Category form.) Create an event procedure for the OnClose event of the Add Category form that uses the Requery method to requery the Category ID combo box on the Enter Or Edit Products form.
Objects Used in this Example
Type | Names |
Table | Products, Categories |
Form | Enter Or Edit Products, Add Category |
Module | Is Loaded |
Step by Step 1 On the Enter or Edit Products form, create an event procedure in the OnNotInList property of the Category ID combo box. 2 Create the Is Loaded module, if you haven't already done so. a) In the Declarations section of the module, type: Option Explicit b) Create the IsLoaded function. 3 Design the Add Category form. a) Set form properties and create event procedures as follows. Property |
Setting |
RecordSource | Categories |
Caption | Add Category |
DefaultView | Single Form |
ViewsAllowed | Form |
DefaultEditing | Data Entry |
ScrollBars | Neither |
RecordSelectors | No |
NavigationButtons | No |
AutoResize | No |
AutoCenter | Yes |
OnUnload | [Event Procedure] |
OnActivate | [Event Procedure] |
OnDeactivate | [Event Procedure] |
b) In the detail section, add the Category Name, Description, and Picture fields from the Categories table. 4 In the Declarations section of the Form.Add Category module, type: Option Explicit Cross References For information on |
See |
Creating event procedures | Building Applications, Chapter 3, "Introducing Access Basic," and Chapter 5, "Access Basic Fundamentals" |
Creating forms | User's Guide, Chapter 15, "Designing Forms," and Chapter 19, "Using Pictures, Graphs, and Other Objects" |
|
$#
$#
$#
$#
UnLoad Event Procedure for the Add Category Form
Sub Form_Unload (Cancel As Integer)
' If Enter Or Edit Products form is loaded,
' select it, requery Category ID combo box,
' and set value of Category ID combo box.
Dim MyControl As Control
If IsLoaded("Enter Or Edit Products") Then
Set MyControl = Forms![Enter Or Edit Products]![Category ID]
DoCmd SelectObject A_FORM, "Enter Or Edit Products"
MyControl.Requery
MyControl = Me![Category ID]
End If
End Sub
Activate Event Procedure for the Add Category Form
Sub Form_Activate ()
' Show toolbar for Add Category form.
'
' Hide Form View toolbar.
' Show Custom Form View toolbar.
DoCmd ShowToolbar "Form View", A_TOOLBAR_NO
DoCmd ShowToolbar "Custom Form View", A_TOOLBAR_YES
' If Enter Or Edit Products form is loaded, hide its current toolbar.
If IsLoaded("Enter Or Edit Products") Then
If Forms![Enter Or Edit Products].AllowEditing = True Then
DoCmd ShowToolbar "Enter Or Edit Products 1", A_TOOLBAR_NO
Else
DoCmd ShowToolbar "Enter Or Edit Products 2", A_TOOLBAR_NO
End If
End If
End Sub
Deactivate Event Procedure for the Add Category Form
Sub Form_Deactivate ()
' Hide toolbar for Add Category form.
'
' Hide Custom Form View toolbar.
' Show built-in Form View toolbar.
DoCmd ShowToolbar "Custom Form View", A_TOOLBAR_NO
DoCmd ShowToolbar "Form View", A_TOOLBAR_WHERE_APPROP
End Sub
You can make forms more readable and reports look more professional if the data in the fields has a consistent appearance. For example, the product names in the Northwind Traders database should all begin with a capital letter. Because a product name may include several words, some of which you don't want capitalized, you can define a function that capitalizes only the first letter of the first word and leaves the rest of the letters in the case in which you entered them.
Overview
Create a function that capitalizes the first letter of the value in a field. Use that function in an event procedure in the AfterUpdate property of the control.
Objects Used in this Example
Type | Name |
Table | Products |
Form | Enter Or Edit Products |
Module | Proper |
Step by Step 1 Create the Proper module. a) In the Declarations section, type: Option Explicit b) Create the CapitalizeFirst function. 2 On the Enter Or Edit Products form, create an event procedure in the AfterUpdate property of the Product Name text box. 3 In the Declarations section of the Form.Enter Or Edit Products module, type: Option Explicit Tip You can also create a function that capitalizes the first letter of every word in the field. Cross References For information on |
See |
Creating event procedures | Building Applications, Chapter 3, "Introducing Access Basic," and Chapter 5, "Access Basic Fundamentals" |
$# CapitalizeFirst Function Function CapitalizeFirst (X) ' Make first letter in field uppercase; leave other ' letters as typed. Dim Temp Temp = Trim(X) CapitalizeFirst = UCase(Left(Temp, 1)) & Mid(Temp, 2) End Function
|
$#
$#
Change the Color of a Section and Its Controls at Run TimeType | Name |
Table | Products |
Form | Enter Or Edit Products |
Module | Enter Or Edit Products Buttons |
Step by Step 1 On the Enter Or Edit Products form, set the BackColor property of the form header, and the Select Category and the Select Product combo boxes to 8421376. (Teal on the palette.) 2 Create the Enter Data and Edit Data buttons, if you haven't already done so. 3 Modify the Enter Or Edit Products Buttons module. a) Modify the EditData function. b) Modify the EnterData function. 4 Create an event procedure that resets the color of the form header and its controls in the OnClose property of the form. Cross References For information on |
See |
Creating event procedures | Building Applications, Chapter 3, "Introducing Access Basic," and Chapter 5, "Access Basic Fundamentals" |
Creating forms | User's Guide, Chapter 15, "Designing Forms" |
Creating macros | User's Guide, Chapter 24, "Macro Basics" |
Creating toolbars | User's Guide, Appendix B, "Working with Toolbars" |
|
$#
EnterData () Function
The comment and code you need to add to the function to set the color of the form header and its controls are displayed in red.
Function EnterData () As Integer
' Code for Enter Data button on Enter Or Edit Products 2 toolbar.
'
On Error GoTo EnterData_Err
Dim MyForm As Form
Dim ComboBox1 As Control
Dim ComboBox2 As Control
Dim EnterColor As Long
Dim Tmp As Variant
Set MyForm = Forms![Enter Or Edit Products]
Set ComboBox1 = Forms![Enter Or Edit Products]![Select Category]
Set ComboBox2 = Forms![Enter Or Edit Products]![Select Product]
EnterColor = 8421376
' Set AllowEditing property to true to indicate data entry mode.
' Set DefaultEditing property to Data Entry (1).
MyForm.AllowEditing = True
MyForm.DefaultEditing = 1
' Enable controls in detail section and disable Product ID text box.
Tmp = EnableControls("Detail", True)
MyForm![Product ID].Enabled = False
' Go to a new record and move to Supplier ID combo box.
DoCmd GoToRecord , , A_NEWREC
MyForm![Supplier ID].SetFocus
' Switch toolbars.
DoCmd ShowToolbar "Enter Or Edit Products 2", A_TOOLBAR_NO
DoCmd ShowToolbar "Enter Or Edit Products 1", A_TOOLBAR_WHERE_APPROP
' Change color of form header and combo boxes.
MyForm.Section(1).BackColor = EnterColor
ComboBox1.BackColor = EnterColor
ComboBox2.BackColor = EnterColor
' Disable combo boxes in form header.
ComboBox1.Enabled = False
ComboBox2.Enabled = False
Exit Function
EnterData_Err:
MsgBox Error$
Exit Function
End Function
$#
$#
Close Event Procedure of the Enter Or Edit Products Form
Sub Form_Close ()
' Reset background color of form header and its
' controls to original color.
Dim StartColor As Long
' Set StartColor to teal.
StartColor = 8421376
Me.Section(1).BackColor = StartColor
Me![Select Category].BackColor = StartColor
Me![Select Category].BackColor = StartColor
End Sub
The About Solutions form shows how to display an application's name, copyright, and logo in a dialog box. This technique makes the About Solutions form work:
Creating an About dialog box.
$#KCreate an About Dialog Box
You can display information about your application in a dialog box that's available from a custom menu bar. The About Solutions dialog box is available from the Help menu of the Edit Products form.
Overview
Create an unbound form that displays information about your application. Create a macro that adds a command to display the form in the Help menu macro group.
Objects Used in this Example
Object | Name | |
Form | About Solutions | |
Macro | Edit Products Menu Bar_Help | |
Step by Step 1 Design the About Solutions form. a) Set the following form properties. Property |
Setting | |
Caption | About Solutions | |
DefaultView | Single Form | |
ViewsAllowed | Form | |
DefaultEditing | Read Only | |
ShortcutMenu | No | |
ScrollBars | Neither | |
RecordSelectors | No | |
NavigationButtons | No | |
AutoCenter | Yes | |
PopUp | Yes | |
Modal | Yes | |
MinButton | No | |
MaxButton | No | |
BorderStyle | Dialog | |
Note The Modal and PopUp properties in the About Solutions form in Solutions are set to No so that you can have access to menu bar commands and toolbar buttons when you look at the form. When Modal and PopUp are set to Yes, the form will have a dialog box border. b) In the detail section, create labels that display your application's name, version number, and copyright. Also, create an object frame that displays your application's logo. c) Create a command button. Set its properties and create an event procedure as follows. Property |
Setting | |
Name | OK | |
Caption | OK | |
OnClick | [Event Procedure] | |
2 In the Declarations section of the Form.About Solutions module, type: Option Explicit 3 Add a macro to the end of the Edit Products Menu Bar_Help macro group. See Create a Toolbar and Menu That Include Only Editing Tasks for information on creating a custom menu. Macro Name |
Action | Arguments |
A&bout Solutions | OpenForm | Form Name: About Solutions View: Form Data Mode: Read Only Window Mode: Normal |
Tip The Solutions application uses a custom menu only for the Edit Products form, so About Solutions is available from the Help menu only when the Edit Products form is open in Form view. If you use custom menu bars throughout your application, add the A&bout Solutions macro to every Help menu macro group. Cross References For information on |
See | |
Creating event procedures | Building Applications, Chapter 3, "Introducing Access Basic," and Chapter 5, "Access Basic Fundamentals" | |
Creating forms | User's Guide, Chapter 15, "Designing Forms," Chapter 16, "Customizing Forms," and Chapter 19, "Using Pictures, Graphs, and Other Objects" | |
Creating macros | User's Guide, Chapter 24, "Macro Basics" | |
|
$#
Highlights of the Startup Form
$#Create a Startup Form
When your application starts, you might want to display its name, copyright, and logo for a brief time. In Solutions, the Startup form is displayed for several seconds when you first open the database.
Overview
Create an unbound form that displays information about your application. Set the form's TimerInterval property to an interval of time (expressed in milliseconds). Create an event procedure for the OnTimer property to close the form. Create an AutoExec macro to open the form.
Objects Used in this Example
Object | Name |
Form | Startup |
Macro | AutoExec |
Step by Step 1 Design the Startup form. a) Set the following form properties. Property |
Setting |
DefaultView | Single Form |
ViewsAllowed | Form |
ShortcutMenu | No |
ScrollBars | Neither |
RecordSelectors | No |
NavigationButtons | No |
AutoCenter | Yes |
BorderStyle | None |
OnTimer | [Event Procedure] |
TimerInterval | 7000 |
Note So that you can open the Startup form and look at it in Solutions, the TimerInterval property is set to 7000 when you run the AutoExec macro and reset to 0 in the event procedure of the OnClose property. b) In the detail section, create labels that display your application's name, copyright, and other pertinent information. Also, create an object frame that displays your application's logo. 2 In the Declarations section of the Form.Startup module, type: Option Explicit 3 Create a macro group named AutoExec to automatically display the Startup form. Action |
Argument |
OpenForm | Form Name: Startup View: Form Data Mode: Read Only Window Mode: Normal |
Cross References For information on |
See |
Creating event procedures | Building Applications, Chapter 3, "Introducing Access Basic," and Chapter 5, "Access Basic Fundamentals" |
Creating forms | User's Guide, Chapter 15, "Designing Forms," Chapter 16, "Customizing Forms," and Chapter 19, "Using Pictures, Graphs, and Other Objects" |
Creating macros | User's Guide, Chapter 24, "Macro Basics" |
$# Timer Event Procedure for the Startup Form Sub Form_Timer ' Close Startup form. DoCmd Close A_FORM, "Startup" End Sub
|
$#
Create a Report That Prints on a Preprinted FormType | Names | |
Table | Customers, Employees, Orders | |
Query | Invoice, Order Details Extended | |
Report | Invoice | |
Step by Step 1 Import the Order Details Extended query from NWIND.MDB, if you haven't already done so. 2 Create the Invoice query. a) Use the following tables and fields. Table/Query |
Fields | |
Customers table | Company Name, Address, City, Region, Postal Code, Country | |
Employees table | Last Name, First Name | |
Orders table | Order ID, Customer ID, Ship Name, Ship Address, Ship City, Ship Region, Ship Postal Code, Ship Country, Ship Via, Order Date, Required Date, Shipped Date, Freight | |
Order Details Extended query | Product Name, Product ID, Unit Price, Quantity, Discount, Extended Price | |
b) Set the Criteria for the Order Date field to: Between #2/1/94# and #3/30/94#. Tip In your application, you'll probably want to prompt users for date criteria when they preview or print the report rather than specifying the dates in the query. For an example of setting criteria at run time, see the Employee Sales report. 3 Design the Invoice report. a) Set the following sorting and grouping properties. Field/Expression |
Sort Order | Group Properties |
Order ID | Descending | Group Header: Yes Group Footer: Yes Group On: Each Value Group Interval: 1 Keep Together: No |
Product ID | Ascending | (Default settings) |
Note Order ID is sorted in descending order in the Invoice report so that the invoice requiring multiple pages appears first. b) In the Print Setup dialog box, set the Left, Right, Top, and Bottom margins to 1.000 in. c) Set the Height property for the following sections. Section |
Height | |
Page header | 3.6875 in | |
Detail section | 0.25 in | |
Page footer | 1 in | |
The Order ID header and footer are not visible on the printed report, so you don't need to set their Height properties. d) In the page header, create text boxes for the customer's name, address, and any other information that you want to appear on every page. e) In the detail section, create text boxes with their properties set as follows. Name |
ControlSource | |
Product ID | Product ID | |
Product Name | Product Name | |
Quantity | Quantity | |
Unit Price | Unit Price | |
Discount | Discount | |
Extended Price | Extended Price | |
f) In the Order ID footer, create an unbound text box. Set its Name property to Print Page Footer. g) In the Order ID footer, create a text box that calculates the invoice subtotal. Set its Name property to Subtotal and its ControlSource property to: = Sum([Extended Price]) h) Create text boxes in the page footer with their properties set as follows. Name |
ControlSource | |
Invoice Subtotal | =[Subtotal] | |
Freight | Freight | |
Invoice Total | =[Invoice Subtotal]+ [Freight] | |
i) Set properties and create event procedures for the following sections. Section |
Property | Setting |
Order ID header | Visible | No |
Order ID footer | ForceNewPage | After Section |
Visible | No | |
OnFormat | [Event Procedure] | |
Page footer | OnFormat | [Event Procedure] |
j) In the Declarations section of the Report.Invoice module, type: Option Explicit Cross References For information on |
See | |
Creating event procedures | Building Applications, Chapter 3, "Introducing Access Basic," and Chapter 5, "Access Basic Fundamentals" | |
Creating queries | User's Guide, Chapter 10, "Query Basics," and Chapter 11, "Designing Select Queries" | |
Creating reports | User's Guide, Chapter 20, "Report Basics," and Chapter 21, "Designing Reports" | |
Setting sorting and grouping properties | User's Guide, Chapter 22, "Sorting and Grouping Data" | |
$# Format Event Procedure for the Order ID Footer Sub GroupFooter3_Format (Cancel As Integer, FormatCount As Integer) ' Reset Print Page Footer text box to show that all detail ' records have been printed for current order. Report will print ' page footer when value of Print Page Footer is True. Me![Print Page Footer] = True End Sub
|
$#
Print "Continued" at the Bottom of a Preprinted FormType | Names | |
Table | Customers, Employees, Orders | |
Query | Invoice, Order Details Extended | |
Report | Invoice | |
Step by Step 1 Create a report for a preprinted form. 2 In the page header, create an unbound text box. Set its Name property to Rows Per Page, its ControlSource property to: = 17, and its Visible property to No. 3 In the Order ID header, create two unbound text boxes. a) Set the Name property of one text box to Row Count, and set its ControlSource property to: =Count([Product ID]) b) Set the Name property of the other text box to Order Page, and leave its ControlSource property blank. Tip On the Invoice report, the Order ID header isn't visible. If you create a report where it is visible, be sure to set the Visible property for these controls to No. 4 Make the following changes in the detail section. a) Create an unbound text box behind the other controls in the center of the detail section. Set its Name property to Continued, its FontItalic property to Yes, and its ControlSource property to: ="Continued on next page..." b) Create an unbound text box at the right edge of the detail section. Set its Name property to Row and its Visible property to No. c) Create a page break control at the bottom of the section. Set its Name property to Detail Page Break. 5 Create event procedures for the following sections. Section |
Property | Setting |
Page header | OnFormat | [Event Procedure] |
Order ID header | OnFormat | [Event Procedure] |
Detail section | OnFormat | [Event Procedure] |
Cross References For information on |
See | |
Creating controls | User's Guide, Chapter 21, "Designing Reports" | |
Creating event procedures | Building Applications, Chapter 3, "Introducing Access Basic," and Chapter 5, "Access Basic Fundamentals" | |
Creating expressions | User's Guide, Chapter 23, "Using Expressions in Reports" | |
|
$#
$#
$#
Highlights of the Shipping Log Report$#
Create a Multiple-Column ReportObject | Names | |
Table | Orders | |
Query | Order Subtotals, Shipping Log | |
Report | Shipping Log | |
Step by Step 1 Import the Order Subtotals query from NWIND.MDB, if you haven't already done so. 2 Create the Shipping Log query. Table |
Field | Criteria |
Orders table | Shipped Date | Between #2/1/92# And #3/31/92# |
Order ID | -- | |
Order Subtotals query | Subtotal | -- |
Tip In your application, you'll probably want to prompt users for date criteria when they preview or print the report rather than specifying the dates in the query. For an example of setting criteria at run time, see the Employee Sales report. 3 Design the Shipping Log report. a) Set the following sorting and grouping properties. Field/Expression |
Sort Order | Group Properties |
Shipped Date | Descending | Group Header: Yes Group Footer: Yes Group On: Each Value Group Interval: 1 Keep Together: With first detail |
Order ID | Ascending | (Default settings) |
b) Set the Width property of the report to 9 inches. c) Set the following Print Setup options. Option |
Setting | |
Orientation | Landscape | |
Margins | Left: 1.000 in Right: 1.000 in Top: 1.000 in Bottom: 1.000 in |
|
Items Across | 2 | |
Column Spacing | 0.75 in | |
Item Size | Width: 3.25 in Height: 0.2299 in |
|
Item Layout | Vertical | |
d) Place labels with the following captions in the page header: Shipped Date, Order ID, and Order Amount. e) Place the following text boxes in the following sections of the report, setting their Name and Control Source properties as follows. Section |
Name | ControlSource |
Shipped Date header | Shipped Date | Shipped Date |
Detail | Order ID Subtotal |
Order ID Subtotal |
f) Copy the Shipped Date, Order ID, and Order Amount labels in the page header, and paste them to the right of the first set. (The Left property of the second Shipped Date label should be set to 4 in.) Cross References For information on |
See | |
Creating queries | User's Guide, Chapter 10, "Query Basics," and Chapter 11, "Designing Select Queries" | |
Creating reports | User's Guide, Chapter 20, "Report Basics," and Chapter 21, "Designing Reports" | |
Setting sorting and grouping properties | User's Guide, Chapter 22, "Sorting and Grouping Data" | |
|
Object | Names | |
Table | Orders | |
Query | Order Subtotals, Shipping Log | |
Report | Shipping Log | |
Step by Step 1 Create the Shipping Log report. 2 Add the following controls to the report. a) In the Shipped Date header, create an unbound text box. Set its Name property to Top1 and its Visible property to No. b) In the detail section, create an unbound text box. Set its Name property to Continued, its FontItalic property to Yes, and its ControlSource property to: =Format([Shipped Date], "dd-mmm-yy") & " continued" 3 Create event procedures for the following sections. Section |
Property | Setting |
Page header | OnFormat | [Event Procedure] |
Shipped Date header | OnFormat | [Event Procedure] |
Detail | OnFormat | [Event Procedure] |
4 In the Declarations section of the Report.Shipping Log module, type: Option Explicit Cross References For information on |
See | |
Creating event procedures | Building Applications, Chapter 3, "Introducing Access Basic," and Chapter 5, "Access Basic Fundamentals" | |
Creating reports | User's Guide, Chapter 20, "Report Basics," and Chapter 21, "Designing Reports" | |
|
$#
$#
$#
Hide a Section When It Prints at the Top of a Page or ColumnObject | Names |
Table | Orders |
Query | Order Subtotals, Shipping Log |
Report | Shipping Log |
Step by Step 1 Create a multiple-column report. 2 Add the Top1 text box to the report, and create the event procedures for the page header and Shipped Date header as described in Repeat a Group Name at the Top of a Column or Page. 3 Create an event procedure for the OnFormat property of the Shipped Date footer. Cross References For information on |
See |
Creating event procedures | Building Applications, Chapter 3, "Introducing Access Basic," and Chapter 5, "Access Basic Fundamentals" |
$# Format Event Procedure for the Shipped Date Footer Sub GroupFooter4_Format (Cancel As Integer, FormatCount As Integer) ' Determine if group footer is first section formatted ' at top of page or column. ' ' Don't print group footer if either statement is true. If Me![Top1] = 0 Or Me.Top = Me![Top1] Then Cancel = True End Sub
|
$#
Create a Form LetterObject | Names | |
Table | Employees, Orders, Sales Goals | |
Query | Order Subtotals, Sales Letter 1, Sales Letter 2 | |
Report | Sales Letter, Sales Letter Subreport | |
Step by Step 1 Import the Order Subtotals query from NWIND.MDB, if you haven't already done so. 2 Create Sales Letter 1, a crosstab query. a) Use the following tables and fields. Table/Query |
Field/Expression | Properties |
Employees table | Employee ID |
Total: Group By Crosstab: Row Heading |
Name: [Last Name] & ", " & [First Name] | Total: Group By Crosstab: Row Heading |
|
Orders table | Year: DatePart("yyyy", [Shipped Date]) | Total: Group By Crosstab: Row Heading |
Expr1: "Qtr" & DatePart("q", [Shipped Date]) | Total: Group By Crosstab: Column Heading |
|
DatePart("yyyy", [Shipped Date]) | Total: Where Criteria: 1993 |
|
Order Subtotals query | Subtotal | Total: Sum Crosstab: Value |
Tip In your application, you'll probably want to prompt users for date criteria when they preview or print the report rather than specifying the dates in the query. For an example of setting criteria at run time, see the Employee Sales report. b) Set the query's ColumnHeadings property to the following value: "Qtr1", "Qtr2","Qtr3","Qtr4" 3 Create the Sales Letter 2 query. Table/Query |
Field/Expression | Properties |
Employees table | Last Name | Sort: Ascending |
Salesperson: [First Name] & " " & [Last Name] | -- | |
Employee ID, Address, City, Region, Postal Code, Country | -- | |
Sales Goals query | Goal | -- |
Goal Year: DatePart("yyyy", [Sales Goals].[Year]) | Criteria: [Sales Letter 1].[Year] | |
Sales Letter 1 query | Qtr1, Qtr2, Qtr3, Qtr4 | -- |
4 Create the Sales Letter report. a) Set the report's RecordSource property to Sales Letter 2. b) Remove the page header and footer. (You need only the detail section for this report.) c) In the Sorting and Grouping box, select Last Name in the Field/Expression column and set Sort Order to Ascending. d) Set the ForceNewPage property of the detail section to After Section. e) Create text boxes for the name, address, salutation, and the text of the letter. For the salutation, use an expression such as the following: ="Dear " & [Salesperson] & ":" To concatenate text and data in a paragraph, use an expression such as: ="Congratulations! You met or exceeded your quarterly sales goal of " & Format([Goal], "$#,##0") & " in " & {Goal Year] & ". In the following summary of your sales, each quarter that is within 90% of your goal is circled." If a text box contains one or more paragraphs, make the text box the height of one line of text and set its CanGrow property to Yes. 5 Create the Sales Letter Subreport. a) Set the report's RecordSource property to Sales Letter 2. b) Remove the page header and footer. (You need only the detail section.) c) Create the text boxes for the quarter totals and the goal. Make the text boxes the same width and height, and space them evenly across the detail section. For example, for each text box, set the Width property to 1 inch and the Height property to 0.1979 inch. Leave 0.125 inch between each text box. d) Save the report. 6 Add the subreport to the main report. a) Select the Sales Letter Subreport in the Database window, and drag it to the detail section of the Sales Letter report. b) Delete the label for the subreport control and adjust the size of the subreport control. For example, make the subreport the height of one line of text. c) Verify that the LinkChildFields and LinkMasterFields properties of the Sales Letter Subreport control are set to Employee ID. 7 Adjust the placement of the controls on the main report. For example, make sure that the parts of the letter Cross References For information on |
See | |
Creating event procedures | Building Applications, Chapter 3, "Introducing Access Basic," and Chapter 5, "Access Basic Fundamentals" | |
Creating queries | User's Guide, Chapter 11, "Designing Select Queries" and Chapter 12, "Advanced Queries" | |
Creating reports | User's Guide, Chapter 20, "Report Basics," and Chapter 21, "Designing Reports" | |
Creating subreports | User's Guide, Chapter 22, "Sorting and Grouping Data" | |
Creating expressions | User's Guide, Chapter 23, "Using Expressions in Reports" | |
Setting sorting and grouping properties | User's Guide, Chapter 22, "Sorting and Grouping Data" | |
|
Object | Names |
Table | Employees, Orders, Sales Goals |
Query | Order Subtotals, Sales Letter 1, Sales Letter 2 |
Report | Sales Letter, Sales Letter Subreport |
Step by Step 1 Create the Sales Letter report. 2 In the Sales Letter Subreport, create an event procedure for the OnPrint property of the detail section. 3 In the Declarations section of the Report.Sales Letter Subreport module type: Option Explicit Cross References For information on |
See |
Creating event procedures | Building Applications, Chapter 3, "Introducing Access Basic," and Chapter 5, "Access Basic Fundamentals" |
|
$#Highlights of the Find Customers Form
The Find Customers form shows how to enter search criteria in one or more fields. This technique makes the Find Customers form work:
Finding records based on criteria you enter in the Company, Contact Name, City, and Country fields.
This technique enhances the Find Customers form:
Clearing the search fields and the subform with the Clear button.
$#KKFind Records Based on Criteria You Enter in One or More Fields
When you search for records, the search criteria might vary each time you search. On the Find Customers form, for example, you can find a customer record by entering a value in any one or more of the fields in the form header: Company, Contact Name, City, or Country. Also, you may have only part of the search criteria you needfor example, you may remember only that the name of the company starts with "B." On the Find Customers form, you can enter "B" in the Company field and then look through the records for all the companies that start with "B."
Overview
Create a subform that contains the fields from the Customers table. Create an unbound main form that contains the search fields and a command button. In the OnClick property of the command button, create an event procedure that creates a SELECT statement based on the criteria entered in the search fields and then sets the RecordSource property of the subform to the SELECT statement.
Objects Used in this Example
Object | Names |
Table | Customers |
Form | Find Customers, Find Customers Subform |
Module | Enable Controls |
Step by Step 1 Create the Enable Controls module, if you haven't already done so. a) In the Declarations section of the module, type: Option Explicit b) Create the EnableControls function. 2 Create the Find Customers form. a) Create an unbound form, and set its properties as follows. Property |
Setting |
Caption | Find Customers |
DefaultView | Single Form |
ViewsAllowed | Form |
ScrollBars | Neither |
RecordSelectors | No |
NavigationButtons | No |
AutoCenter | Yes |
OnOpen | [Event Procedure] |
b) Add a form header and footer. Set the Height property of the form footer to 0. c) In the Declarations section of the Form.Find Customers module, type: Option Explicit d) In the General section of the Form.Find Customers module, create the AddToWhere sub and the DisableControl sub. e) In the form header, create four unbound text boxes. Set their properties as follows. Name |
AfterUpdate |
Look For Company | [Event Procedure] |
Look For Contact | [Event Procedure] |
Look For City | [Event Procedure] |
Look For Country | [Event Procedure] |
f) In the form header, create a command button, and set its properties as follows. Property |
Value |
Name | Show Customers |
Caption | Show Customers |
Default | Yes |
OnClick | [Event Procedure] |
3 Create the Find Customers Subform. a) Set the following form properties. Property |
Setting |
RecordSource | SELECT * FROM Customers WHERE False |
DefaultView | Datasheet |
b) In the detail section, create text boxes for all the fields in the field list. Tip To adjust the width of the columns in Datasheet view, cut the WHERE False statement from the RecordSource property, switch to Datasheet view, adjust column widths, and then save the form. Return to Design view, paste the WHERE False statement, and save the form again. 4 Add the Find Customers Subform to the Find Customers form. a) Select the Find Customers Subform in the Database window, and drag it to the detail section of the Find Customers form. b) Delete the label attached to the subform control. Cross References For information on |
See |
Creating event procedures | Building Applications, Chapter 3, "Introducing Access Basic," and Chapter 5, "Access Basic Fundamentals" |
Creating forms | User's Guide, Chapter 15, "Designing Forms," Chapter 16, "Customizing Forms," and Chapter 17, "Creating Forms Based on More Than One Table" |
|
$#
$#
$#
$#
$#
$#
$#
$#
KClear the Values in Unbound Text Boxes and a SubformObject | Name |
Table | Customers |
Form | Find Customers, Find Customers Subform |
Step by Step 1 Create the Find Customers form. 2 In the form header of the Find Customers form, create a command button, and set its properties as follows. Property |
Value |
Name | Clear |
Caption | Clear |
OnClick | [Event Procedure] |
3 Modify the event procedure in the OnClick property of the Show Customers command button. Cross References For information on |
See |
Creating event procedures | Building Applications, Chapter 3, "Introducing Access Basic," and Chapter 5, "Access Basic Fundamentals" |
Creating forms | User's Guide, Chapter 15, "Designing Forms," Chapter 16, "Customizing Forms" |
$# Click Event Procedure for the Clear Command Button Sub Clear_Click () ' Clear controls in form header and remove records from subform. ' Dim MySQL As Sting Dim Tmp As Variant MySQL = "SELECT & FROM Customers WHERE False" ' Clear search text boxes. Me![Look For Company] = Null Me![Look For Contact] = Null Me![Look For City] = Null Me![Look For Country] = Null ' Reset subform's RecordSource property to remove records. Me![Find Customers Subform].Form.Recordsource = MySQL ' Move insertion point to Look For Company text box. Me![Look For Company].SetFocus End Sub
|
$#
Highlights of the Solutions Form
$#Open a Form or Report from a Main Form
If there are many forms and reports in an application, you might want to provide a main form that organizes them by task or job function. The Solutions form is the main form of the Solutions sample application. It groups the examples by category of application development.
Overview
Create an unbound form. Create one list box that lists the categories. Create a second list box that lists the examples that pertain to the selected category. Create a command button. In its OnClick property, create an event procedure that uses the Column property to open the sample form or report selected in the examples list box.
Objects Used in this Example
Object | Names | ||
Table | Example Objects, Example Topics, Examples | ||
Query | Example List, Example Topic List | ||
Form | Solutions | ||
Step by Step 1 Create the Example Topic List query. Table |
Field (Ascending) | Show | |
Example Topics | Topic ID Description Sort Order (Ascending) |
Yes Yes No |
|
2 Create the Example List query. Table |
Field (Ascending) | Show | Criteria |
Examples | Description (Ascending) | Yes | -- |
Example Objects | Object Name | Yes | -- |
Object Type | Yes | -- | |
Topic ID | No | Forms![Solutions]![Select Topic] | |
3 Design the Solutions form. a) Create an unbound form, and set its properties as follows. Property |
Setting | ||
Caption | Solutions | ||
DefaultView | Single Form | ||
ViewsAllowed | Form | ||
ShortcutMenu | No | ||
ScrollBars | Neither | ||
RecordSelectors | No | ||
NavigationButtons | No | ||
AutoCenter | Yes | ||
b) Create an unbound list box, and set its properties as follows. Property |
Setting | ||
Name | Select Topic | ||
RowSourceType | Table/Query | ||
RowSource | Example Topic List | ||
ColumnCount | 2 | ||
ColumnWidths | 0 in;5 in | ||
BoundColumn | 1 | ||
StatusBarText | Select a topic. | ||
AfterUpdate | [Event Procedure] | ||
c) Create another unbound list box, and set its properties as follows. Property |
Setting | ||
Name | Select Example | ||
RowSourceType | Table/Query | ||
RowSource | Example List | ||
ColumnCount | 3 | ||
ColumnWidths | 5 in;0 in;0 in | ||
BoundColumn | 2 | ||
StatusBarText | Select an example. | ||
OnDblClick | [Event Procedure] | ||
d) Create a command button, and set its properties as follows. Property |
Setting | ||
Name | OK | ||
Caption | OK | ||
Default | Yes | ||
OnClick | [Event Procedure] | ||
Cross References For information on |
See | ||
Creating event procedures | Building Applications, Chapter 3, "Introducing Access Basic," and Chapter 5, "Access Basic Fundamentals" | ||
Creating forms | User's Guide, Chapter 15, "Designing Forms," and Chapter 16, "Customizing Forms" | ||
Creating queries | User's Guide, Chapter 11, "Designing Select Queries" | ||
|
$#
$#
$#
KClose All Database Objects When You Exit an ApplicationObject | Name |
Form | Solutions |
Step by Step 1 In the General section of the Form.Solutions module, create the CloseObject function. 2 In the Declarations section of the Form.Solutions module, type: Option Explicit 3 Add a command button to the Solutions form and set its properties as follows. Property |
Setting |
Name | Exit |
Caption | Exit |
OnClick | [Event Procedure] |
Note The Click event procedure for the Exit command button does not work when you discard changes to a module. When you choose not to save changes to a module, Microsoft Access closes the module window but halts the Exit command button's Click event procedure, and you must close the database yourself. Cross References For information on |
See |
Creating event procedures | Building Applications, Chapter 3, "Introducing Access Basic," and Chapter 5, "Access Basic Fundamentals" |
Creating forms | Chapter 16, "Customizing Forms" |
$# CloseObject() Function Function CloseObject (ContainerName As String, ContainerType As Integer) ' Close open database objects. Dim MyDatabase As Database, MyContainer As Container Dim I As Integer Set MyDatabase = DBEngine.Workspaces(0).Databases(0) Set MyContainer = MyDatabase.Containers(ContainerName) For I = 0 To MyContainer.Documents.Count - 1 DoCmd Close ContainerType, MyContainer.Documents(I).Name Next I End Function
|
$#
Highlights of the Sales Totals Form$#
KKKCreate a One-to-Many-to-Many FormObject | Names | ||
Table | Employees, Orders, Order Details | ||
Query | Sales Totals (Qtr), Sales Totals (Year), Salespeople | ||
Form | Sales Totals, Sales Totals Subform 1, Sales Totals Subform 2 | ||
Module | Enable Controls | ||
Step by Step 1 Create the Salespeople query. Table |
Field/Expression | Sort | |
Employees | Salesperson: [Last Name] &", " & [First Name] | Ascending | |
Employee ID | -- | ||
Orders | -- | -- | |
Note You don't use any fields from the Orders table in this query; you use the Orders table only to limit the employee names returned to those with records in the Orders table. 2 Create the Sales Totals (Year) query. Table |
Field/Expression | Total | Criteria |
Orders | Employee ID | Group By | -- |
T-Year: Year([Shipped Date]) | Group By | Is Not Null | |
Order Details | Total: CCur(Sum(CLng([Unit Price]*[Quantity]*(1-[Discount])*100)/100)) | Expression | -- |
3 Create the Sales Totals (Qtr) query. Table |
Field/Expression | Total | Criteria |
Orders | Employee ID | Group By | -- |
T-Year: Year([Shipped Date]) | Group By | Is Not Null | |
Quarter: DatePart("q",[Shipped Date]) | Group By | Is Not Null | |
Order Details | Subtotal: CCur(Sum(CLng([Unit Price]*[Quantity]*(1-[Discount])*100)/100)) | Expression | -- |
4 Design Sales Totals Subform 1. a) Set the following form properties. Property |
Setting | ||
RecordSource | Sales Totals (Year) | ||
DefaultView | Datasheet | ||
b) Create two text boxes in the detail section, and set their Name and ControlSource properties as follows. Name |
ControlSource | ||
T-Year | T-Year | ||
Total | Total | ||
c) Switch to Datasheet view. Adjust the width of the columns, and save the form. 5 Design Sales Totals Subform 2. a) Set the following form properties. Property |
Setting | ||
RecordSource | Sales Totals (Qtr) | ||
DefaultView | Datasheet | ||
b) Create two text boxes in the detail section, and set their Name and ControlSource properties as follows. Name |
ControlSource | ||
Quarter | Quarter | ||
Subtotal | Subtotal | ||
c) Switch to Datasheet view. Adjust the width of the columns, and save the form. 6 Create the Enable Controls module, if you haven't already done so. a) In the Declarations section, type: Option Explicit b) Create the EnableControls function. 7 Design the Sales Totals form. a) Set the following form properties. Property |
Setting | ||
RecordSource | Employees | ||
DefaultView | Single Form | ||
ViewsAllowed | Form | ||
ShortcutMenu | No | ||
ScrollBars | Neither | ||
RecordSelectors | No | ||
NavigationButtons | No | ||
AutoCenter | Yes | ||
b) Add a form header and footer. Set the Height property of the form footer to 0. c) In the form header, create an unbound combo box, and set its properties as follows. Property |
Setting | ||
Name | Select Salesperson | ||
RowSourceType | Table/Query | ||
RowSource | Salespeople | ||
ColumnCount | 2 | ||
ColumnWidths | 2 in;0 in | ||
BoundColumn | 2 | ||
LimitToList | Yes | ||
AfterUpdate | [Event Procedure] | ||
d) In the Declarations section of the Form.Sales Totals module, type: Option Explicit e) In the detail section, create a text box, and detach its label. Then set the properties of the text box as follows. Property |
Setting | ||
Name | Salesperson | ||
ControlSource | =[Last Name] &", "&[First Name] | ||
Enabled | No | ||
TabStop | No | ||
BackColor | 8388608 (dark blue) | ||
BorderStyle | Clear | ||
ForeColor | 16777215 (white) | ||
f) In the detail section, create a hidden text box with its properties set as follows. Property |
Setting | ||
Name | T-Year | ||
ControlSource | =[Sales Totals Subform 1].Form![T-Year] | ||
Visible | No | ||
TabStop | No | ||
Note Microsoft Access uses the value in this text box to link the values in Sales Totals Subform 2 to the selected row in Sales Totals Subform 1. 8 Add the Sales Totals Subform 1 to the Sales Totals form. a) Select the Sales Totals Subform 1 in the Database window, and drag it to the detail section of the Sales Totals form. b) Change the label for the subform control to Select Year: and adjust the size of the subform control. c) Verify that the LinkChildFields and LinkMasterFields properties of the Sales Totals Subform 1 control are both set to Employee ID. d) Set the Enabled property of the Sales Totals Subform 1 control to No. 9 Add the Sales Totals Subform 2 to the Sales Totals form. a) Select the Sales Totals Subform 2 in the Database window, and drag it to the detail section of the Sales Totals form. b) Change the label for the subform control to Quarter Totals for Selected Year: and adjust the size of the subform control. c) Set both the LinkChildFields and LinkMasterFields properties of the Sales Totals Subform 2 control to: T-Year;Employee ID d) Set the Enabled property of the Sales Totals Subform 2 control to No. Cross References For information on |
See | ||
Creating event procedures | Building Applications, Chapter 3, "Introducing Access Basic," and Chapter 5, "Access Basic Fundamentals" | ||
Creating forms | User's Guide, Chapter 15, "Designing Forms," Chapter 16, "Customizing Forms," and Chapter 17, "Creating Forms Based on More Than One Table" | ||
Creating queries | User's Guide, Chapter 11, "Designing Select Queries" | ||
|
$#
Highlights of the Show Sales Form
$#KKKKKKKKKKKKKKUse a Custom Function in a Form's SELECT Statement (SQL)
On a query by form, you might want users to choose one option from a specific set of criteria. On the Show Sales form, you can specify a date range and whether you want to see all records in the date range or records in the date range where the sales are less than or greater than 1000.
Overview
Create a query by form where you enter criteria in text boxes and an option group in the form header of the main form, and display records in a subform in the detail section. Create a function that returns the value selected in the option group. Create a command button in the form header of the main form. In the OnClick property of the command button, create an event procedure that creates an SQL SELECT statement that uses the custom function to return the user's criteria and sets the RecordSource property of the subform to the SELECT statement.
Objects Used in this Example
Object | Names | |
Table | Customers, Orders | |
Query | Order Subtotals, Show Sales | |
Form | Show Sales, Show Sales Subform | |
Module | Enable Controls | |
Step by Step 1 Import the Order Subtotals query from NWIND.MDB, if you haven't already done so. 2 Create the Show Sales query. Table/Query |
Fields | Sort |
Customers | Company Name | -- |
Orders | Shipped Date | -- |
Order Subtotals | Order ID | -- |
Subtotal | Descending | |
3 Create the Show Sales Subform. a) Set the following form properties. Property |
Setting | |
RecordSource | Show Sales | |
DefaultView | Datasheet | |
ViewsAllowed | Datasheet | |
ShortcutMenu | No | |
b) Create text boxes for the following fields in the detail section: Company Name, Order ID, and Subtotal. c) Switch to Datasheet view. Adjust the width of the columns, and save the form. d) Reset the form's RecordSource property to the following SQL statement: SELECT [Company Name] FROM Customers WHERE False Note Setting the RecordSource property to this SQL statement will display the subform as an empty datasheet when the Show Sales form is opened. 4 Create the Show Sales form. a) Set the following form properties. Property |
Setting | |
RecordSource | -- | |
Caption | Show Sales | |
DefaultView | Single Form | |
ViewsAllowed | Form | |
ShortcutMenu | No | |
ScrollBars | Neither | |
RecordSelectors | No | |
NavigationButtons | No | |
AutoCenter | Yes | |
OnOpen | [Event Procedure] | |
b) Select the Show Sales Subform in the Database window, and drag it to the detail section of the Show Sales form. Adjust the size and location of the subform control, and remove its attached label. (Leave the LinkChildFields and LinkMasterFields properties blank.) c) Add a form header and footer. Set the Height property of the form footer to 0. d) In the form header, create two unbound text boxes, and set their Name and Format properties as follows. Name |
Format | |
Beginning Date | Medium Date | |
Ending Date | Medium Date | |
e) Type the name of each text box in the Caption property of its attached label. f) In the form header, create an unbound option group, and set its Name property to Sales and its DefaultValue property to 1. g) Type Sales in the Caption property of the option group's attached label. h) Create three option buttons in the Sales option group, and set their Name and OptionValue properties as follows. Name |
OptionValue | |
Less Than 1000 | 1 | |
Greater Than 1000 | 2 | |
All | 3 | |
i) Type the name of each option button in the Caption property of its attached label. 5 Modify the Show Sales query. a) Specify the following criteria for the Shipped Date field: Between Forms![Show Sales]![Beginning Date] And Forms![Show Sales]![Ending Date] b) For the Subtotal field, specify Descending sort order. Tip You can copy the SQL statement from the query's SQL window to use in the event procedure for the command button you create in step 8. 6 In the General section of the Form.Show Sales module, create the ShowSales function. 7 In the Declarations section of the Form.Show Sales module, type: Option Explicit 8 Create the Enable Controls module, if you haven't already done so. a) In the Declarations section of the module, type: Option Explicit b) Create the EnablesControls function. 9 In the form header of the Show Sales form, create a command button, and set its properties as follows. Property |
Setting | |
Name | Show Sales | |
Caption | Show Sales | |
Default | Yes | |
OnClick | [Event Procedure] | |
Cross References For information on |
See | |
Creating event procedures | Building Applications, Chapter 3, "Introducing Access Basic," and Chapter 5, "Access Basic Fundamentals" | |
Creating forms | User's Guide, Chapter 15, "Designing Forms," and Chapter 16, "Customizing Forms" | |
Creating queries | User's Guide, Chapter 10, "Query Basics," and Chapter 11, Designing Select Queries" | |
|
$#
$#