tblContact | ||
ContactID | Long | Primary key |
LastName | T 20 | Contact's last name |
FirstName | T 15 | Contact's first name |
Company | T 50 | Contact's company |
Street | T 35 | Company billing street address |
City | T 35 | Company billing city |
State | T 2 | Company billing state |
Zip | T 11 | Company billing zip |
WorkPhone | T 18 | Contact's work phone |
Sel | Y/N | Yes=Select this record for processing |
Comment | T 255 | Note about this record |
Usr | T 10 | User who last updated this record |
UpdDate | Date | Date this record was last updated |
tblInvoice | ||
InvoiceID | Long | Primary key |
ContactID | Long | Relates to tblContact |
InvoiceDate | Date | Date invoice was finalized and sent |
PaidDate | Date | Date invoice was paid in full |
Sel | Y/N | Yes=Select this record for processing |
PrivateNote | T 255 | Private note about this record |
Comment | T 255 | Note about this record |
Usr | T 10 | User who last updated this record |
UpdDate | Date | Date this record was last updated |
tblInvoiceDetail | ||
InvoiceDetailID | Long | Primary key |
InvoiceID | Long | Relates to tblInvoice |
ContactID | Long | Relates to tblContact for performance (opt) |
StartDate | Date | Date record was added to the invoice |
PartNo | T 10 | Relates to tblPart |
Part | T 255 | Short description of part or service |
Qty | Long | Number of items |
Price | Single | Price per item |
ExtPrice | Single | Price * Qty |
Sel | Y/N | Yes=Select this record for processing |
Comment | T 255 | Note about this record |
Usr | T 10 | User who last updated this record |
UpdDate | Date | Date this record was last updated |
tblPart | ||
PartNo | T 50 | Primary key |
Part | T 50 | Short description of part |
Price | Single | Price for a single part |
Category | Long | Relates to tblCategory |
Sel | Y/N | Yes=Select this record for processing |
Comment | T 255 | Note about this record |
Usr | T 10 | User who last updated this record |
UpdDate | Date | Date this record was last updated |
Property | Value |
Caption | Contact |
Default View | Single Form |
Views Allowed | Form |
Scroll Bars | Neither |
Record Selectors | No |
Dividing Lines | No |
Auto Center | Yes |
Key Preview | Yes |
Cycle | Current Record |
Shortcut Menu | No |
Allow Design Changes | Design View Only |
Control | Prefix |
Combo Box | cbo |
Command Button | cmd |
Image | img |
List Box | lst |
Label | lbl |
Line | lin |
Option Group Frame | fra |
Option Button | opt |
Subform | sfrm |
Tab Control | tab |
Text Box | txt |
Combo Box | cbo |
Property | Value |
Column Count | 2 |
Column Widths | 0;2.5" |
List Width | 2.5" |
RowSource | qryCboContact |
Name | cboContact |
Tab Stop | No |
For this, we'll need some VBA! Open frmContact in the design view, right-click cboContact and select Properties to see the properties window. Click the Event tab. Click the white line to the right of After Update and select [Event Procedure] in the combo box. Click the ... to the right of the combo to see the Visual Basic window. This is where the code behind the form is located. It has a General Declarations area and lines that say: Private Sub cboContact_AfterUpdate() End Sub The code we will add to this subroutine will run After we Update the combo box named cboContact. Private means the code can only used by this form. Any memory variables it changes will not affect other objects in the database. End Sub is the last line of the code for this event. Between the lines above, type: Me.RecordsetClone.FindFirst "ContactID=" & Me!cboContact Me.Recordset.Bookmark = Me.RecordsetClone.Bookmark Save the form and open it to test your new combo box. If you typed the code correctly, the form data should now change to match the contact you select in the Contact combo box. Those two lines above are about as complicated as VBA gets. Let's look at some more simple VBA commands first. GENERAL VBA TIPS If you type DoCmd. in a code module, you'll see a menu of arguments for the DoCmd command. Here are some useful ones:
After you type a DoCmd command, press the F1 key to see Access help on that command. Another important part of VBA is knowing how to refer to objects. You are already familiar with quite a few objects. Controls such as textboxes, combo boxes, checkboxes, lines, images and option groups are objects. They can be contained in forms or reports which are also objects. Tables, queries and macros are also objects. Think of objects as nouns in the English language. They are things. Objects have properties such as Width, Height or BackColor that describe them. Properties are like adjectives that describe nouns. Running VBA code is like the verb of a sentence. It causes the objects to act (OpenForm, RunSQL, Close, etc). Access has several collections of objects. All forms are in the Forms collection. queries in the QueryDefs collection, tables in the TableDefs collection and reports in the Reports collection. Each form has a Controls collection that contains the textboxes, labels, combo boxes and other controls for that form. Each table has a Fields collection. Most objects have a Properties collection. You generally specify the name of the collection and the name of the object within that collection. Specify from the general collection to the more specific collection and finally to the property. To refer to the value in a textbox named txtContactID in the form frmContact use Forms("frmContact").Controls("txtContactID").Value. The word Forms tells us that frmContact is in the Forms collection. We are further referring to the txtContactID control in frmContact's Controls collection and we have specified the property .Value which appears in the textbox. To see if the field txtUsr is visible on the frmContact form you would refer to: Forms("frmContact").Controls("txtUsr").Visible To set it to True Forms("frmContact").Controls("txtUsr").Visible=True You can also shorten the syntax of forms to Forms!frmContact!txtUsr.visible=True Note that we use the ! when separating collections such as the Forms collection and its Controls collection. We use the . to separate Properties from collections. When code in frmContact is running, you can refer to the form as Me instead of having to use the longer Forms!frmContact or Forms("frmContact"). Me!txtZip is the same as Forms("frmContact").Controls("txtZip").Value. |
Property | Value |
Caption | Invoices |
Default View | Continuous Forms |
Views Allowed | Form |
Scroll Bars | Vertical |
Auto Center | Yes |
Allow Additions | No |
Allow Deletions | No |
Shortcut Menu | No |
Allow Design Changes | Design View Only |
VBA Tips
If you want to change a textbox SpecialEffect from the default Sunken to Etched in code, Press Ctrl-G to go to the
debug window and ? forms!frmInvoices!txtPaidDate.SpecialEffect If txtPaidDate is Etched, the line will return 3. If it is Sunken the line will return 2. Knowing that constant, you could set txtPaidDate to Etched or Sunken. Using SQL, you could delete the current record in frmInvoices by: Docmd.RunSQL "DELETE FROM tblInvoice WHERE InvoiceID=" & Forms!frmInvoice!txtInvoiceID The form must be open (it can be hidden or in the design view) to refer to it using Forms! InvoiceID above is a numeric value so it needs no delimiter. If you refer to a text field, enclose the value in single or double quotes. DoCmd.RunSQL "Update tblInvoice SET Sel=True WHERE Usr='" & CurrentUser() & "'" The line above would select all records that you last updated. If you refer to a Date, enclose it in #'s. DoCmd.RunSQL "Update tblInvoice SET UpdDate=#" & Now*() & "#" The shortest way to refer to objects WITHIN the open form is to use Me. Me!txtInvoiceID refers to the value in the textbox txtInvoiceID on the currently running form. |