Está en la página 1de 66

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

1/22/2003 (Last Updated) A ---------------------------------A Application object prop/methods Automating compaction (by User)
Automation of Excel Automation of Word Automation of Word (simple ex.) B ---------------------------------B
Bookmarks C ---------------------------------C Calculated controls--ControlSource Capitalize (capitalize certain field)
Clones (RecordsetClone) Closing a database connection Closing a form Collection (ways of referencing)
Column (listing single one) Combo Box properties/methods Combobox Load with SQL Query Combobox Update & Item Selection
Compaction (automating for User) Concatenation function (simple) Containers (& docs within) listing Control object properties/methods
Control Source properties (Access) Controls (moving to specific ones) ControlSource data (separate form) D ---------------------------------D
Data Conversion functions Database (creating new one) Database Connection (example) Database properties (determining)
Date calc (diff between 2 dates) Declaring a Recordset Object Declaring Database Connection DLookup function (data f tbl or q)
DoCmd object Domain Aggregate functions E ---------------------------------E Error Table creation
Excel (link spreadsheet as a table) Excel chart creation via automation F ---------------------------------F Field (i.e., Column) Constraints
Field (listing single col in recordset) File Open dialog (for choosing file) Filename & Path Info (of dbase) Finding record (simple form)
Finding record (via seek method) Finding records (walking recordset) Finding recs (FindFirst & FindNext) Finding recs (seek against table)
Finding recs (SQL search -- pref.) Finding recs (SQL string - simple) Finding recs (using no-match prop) Focus (giving a control focus)
Form activation (from another) Form object properties/methods Form Open? (true or false) Form open? check (IsLoaded funct.)
FoxPro (creating FoxPro table) FoxPro (linking to a FoxPro db) Functions (rules for calling/using) G ---------------------------------G
H ---------------------------------H Hourglass (turning it on) HTML Output (example) I ---------------------------------I
IsLoaded utility (Form open?) J ---------------------------------J K ---------------------------------K L ---------------------------------L
List box properties/methods ListBox Item Selection Listbox: rowsource from SQL query M ---------------------------------M
Me property (when within object) Module object propeties/methods N ---------------------------------N Null strings in database fields
O ---------------------------------O Opening a database connection Opening a Form (simple method) Opening a Recordset
Opening and Closing a Form Opening form (hiding & returning) Opening form (in data entry mode) Outlook (sending invoice reminder)
P ---------------------------------P Password (how to change it) Printing reports example Progress meter in status bar
Properties of a database (finding) Properties/Methods: Application Properties/methods: Combo & List Properties/methods: Controls
Properties/Methods: Form object Properties/methods: Report object Q --------------------------------- QueryDef (constructing w VBA)
QueryDef (editing with VBA) R ---------------------------------R Record locking types & method Record navigation (in database/rst)
Records Found with SQL to Array Recordset (rows & cols) into Array Recordset (Working With) Recordset Open Options
Refresh table links Report (after Query mod by listbox) Report object properties/methods Rowsource of listbox from SQL
S ---------------------------------S Security (adding a Group Account) Security (adding a User Account) Security (adding User to Group)
Security (granting read only) Security (list groups & users) Security (list Groups in workspace) Security (listing permissions)
Security (show "no delete" perms) Show/Hide Controls Spreadsheet (linking an Excel sht) SQL (changing display of NULL)
SQL (conditional CASE logic) SQL (converting datatypes) SQL (handling dates) SQL (handling upper- & lowercase)
SQL (list of commands/keywords) SQL (pattern-matching) SQL (string concatenation) SQL (substring SUBSTR function)
SQL Aggregate functions SQL cmd to delete a table SQL cmds to append data to table SQL cmds to change table data
SQL cmds to create a table SQL cmds to delete rows f table SQL Query (Basic Form) SQL Query (joining tables on field)
SQL Statement (Full Syntax expl.) Sub Procedures (rules for using) Synchronizing forms SysCmd function (object states, etc.)
T ---------------------------------T Table Creation (using SQL) Table creation using VBA Tag property (available for use)
Timer function (timing a process) Transaction process (example) U ---------------------------------U Unhiding previously hidden object
Userform_Initialize equivalent Users (list all Users in workspace) Users (list users & groups o) V ---------------------------------V
Variables Variables & Procedures VBE Shortcuts View Creation (SQL)
W ---------------------------------W Walking recordset (faster!) Walking the recordset Word automation (creating letter)
Word automation (simple example) X --------------------------------- Y ---------------------------------Y Z ---------------------------------

******************************************************************************************************
******************************************************************************************************
******************************************************************************************************
******************************************************************************************************
******************************************************************************************************
******************************************************************************************************
******************************************************************************************************
******************************************************************************************************
******************************************************************************************************
******************************************************************************************************
Walking recordset (faster!)
******************************************************************************************************
Here is a trick to loop through a recordset faster. Often when looping through a recordset people will use the
following code:
Do while not records.eof
combo1.additem records![Full Name]
records.movenext
Loop

The problem is that everytime the database moves to the next record it must make a check to see if it has reached the
end of the file. This slows the looping down a great deal. When moving or searching throuch a large record set this
can make a major difference. Here is a better way to do it.
records.movelast
lngRecCount=records.RecordCount
records.movefirst
For lngCounter=1 to lngRecCount
combo1.additem records![Full Name]
records.movenext
Next lngCounter

You should see about a 33% speed increase

******************************************************************************************************
Null strings in database fields
******************************************************************************************************
Dealing with Null strings in Access database fields
By default Access string fields contain NULL values unless a string value (including a blank string like "") has been assigned. When you read
these fields using recordsets into VB string variables, you get a runtime type-mismatch error. The best way to deal with this problem is to use
the built-in & operator to concatenate a blank string to each field as you read it. For example:
Dim DB As Database
Dim RS As Recordset
Dim sYear As String
Set DB = OpenDatabase("Biblio.mdb")
Set RS = DB.OpenRecordset("Authors")
sYear = "" & RS![Year Born]

******************************************************************************************************
Password (how to change it)
******************************************************************************************************
Private Sub ChangeAccessPassword()
Dim Db As Database

Set Db = OpenDatabase("C:\My Documents\northwind.mdb", True, _


False, ";pwd=OriginalPassword")
Db.NewPassword "OriginalPassword", "NewPassword"
Db.Close
End Sub
******************************************************************************************************
SQL (list of basic commands & keywords)
******************************************************************************************************
Basic Commands
ALTER DATABASE DROP INDEX
ALTER TABLE DROP TABLE
BEGIN TRANsaction DROP VIEW
COMMIT TRANsaction GRANT
CREATE DATABASE INSERT
CREATE INDEX REVOKE
CREATE TABLE ROLLBACK TRANsaction
CREATE VIEW SELECT
DELETE UPDATE
DROP DATABASE UPDATE STATISTICS

Keywords
ALL ALTER COLUMN ALTER SESSION SET EVENTS ANALYZE
ANY AS ASC AUTOTRACE ON AVG
BEGIN BETWEEN CASE CAST CHAR
CHECK COALESCE COLLATE COLLATION FROM COMMIT
CONNECT CONSTRAINT CONVERT COUNT CREATE CHARACTER SET
CREATE RULE CREATE SCHEMA CROSS JOIN CURRENT DATE DATABASE
DATEADD DATEDIFF DATEFORMAT DATEPART DDL
DECIMAL DECODE DEFAULT DESC DISTINCT
DML DOUBLE DOUBLE PRECISION DROP COLLATION ESCAPE
EXISTS EXPLAIN PLAN FLOAT FOREIGN KEY FROM
FULL OUTER GO GROUP BY HAVING IN
INNER INSERT INTO INT INTEGER INTERSECT
INTERVAL IS NULL ISNULL JOIN LEFT OUTER
LENGTH LIKE LONG LONG VARCHAR LOWER
MAX MIN MIN MINUS MODIFY
NATURAL JOIN NESTING NOT NOT BETWEEN NOT EXISTS
NOT IN NOT LIKE NOT NULL NULL AND
NUMBER NUMERIC NVL OR ORDER BY
PRIMARY KEY PUBLIC REAL REFERENCES RIGHT OUTER
ROLLBACK SMALLFLOAT SMALLINT SP_HELPSORT SUBSTRING
SUM SUM TEXT TIME TIMESTAMP
TINYINT TO TO_CHAR TO_NUMBER UNIQUE
UPPER USE VALUES WHERE WITH CHECK OPTION
******************************************************************************************************
SQL (handling dates)
*****************************************************************************************************
One simple way for finding all rows entered on one day is to spell out the full minimum and maximum time values for the day and use
BETWEEN to find everything within those parameters:
SELECT price, pubdate
FROM titles
WHERE pubdate between 'Oct 21 1998 00:00' and 'Oct 21 1998 23:59'

Another idea is to use the keyword LIKE to find everything that matches the known part of the date. Here the % wildcard (similar to an
asterisk) stands for anything following the character string.
SELECT price, pubdate
FROM titles
WHERE pubdate like 'Oct 21 1998%'

You could also use convert functions to change the date into a shorter character string and then search for the string:
SELECT price, pubdate
FROM titles
WHERE cast (pubdate as char(11)) = 'Oct 21 1998'

A particular date function available on some systems is DATEPART which works like this:
SELECT price, pubdate
FROM titles
WHERE datepart(mm, pubdate) = 10 and
datepart(dd, pubdate) = 21 and
datepart(yy, pubdate) = 1998

A similar function is DATEADD which is set up to add a number of time units to a known date -- and you can then retrieve records
between the paramaters without worrying about the time.
SELECT price, pubdate
FROM titles
WHERE pubdate between 'Oct 21 1998' and dateadd(day, 1, 'Oct 21 1998')

There are also situations where you'll want to group data by time units. For example, what's the distribution of books published during the
year (by month)?
SELECT datepart(month, pubdate), count(title_id)
FROM titles
GROUP BY datepart(month, pubdate)
******************************************************************************************************
Rowsource of Listbox from Instream SQL
******************************************************************************************************
MySQL = "SELECT DISTINCT qryPubReviewInfo.AuthorName " & vbCrLf
MySQL = MySQL & "FROM tblComments RIGHT JOIN qryPubReviewInfo ON tblComments.ReviewID = qryPubReviewInfo.ReviewID "
& vbCrLf
MySQL = MySQL & "ORDER by 1;"
lblNames.Caption = "Select Author from list below:"
lblNames.Visible = True
With lstNames ' And sets a few other options for the listbox
.ColumnCount = 1 ' Two columns
.ColumnHeads = False ' With column headings
.RowSource = MySQL ' Providing it the SQL string
.Visible = True ' Turning it on
.Requery ' Requerying it -- just in case we are coming back after being elsewhere on the form
End With
******************************************************************************************************
SQL (handling upper- & lowercase chars)
******************************************************************************************************
Sometimes data is stored in uppercase letters, sometimes in lowercase letters, and sometimes in mixed case. This might reflect the lack of
control over the data as it was entered into the system. This can be handled by:
(1) Using character-classes with the like keyword -- for each character. Example:
SELECT title
FROM titles
WHERE title LIKE '%[Ww][Ii][Tt][Hh][Oo][Uu][Tt]%'
(2) Converting the case to UPPER or LOWER, then performing the comparison:
SELECT title
FROM titles
WHERE UPPER(title) LIKE '%WITHOUT%'
******************************************************************************************************
SQL (converting datatypes)
******************************************************************************************************
You can also use SQL to convert data from one type to another. For example, if you have a number which might be expressed as a decimal
(85.45) but need to express it as a percentage -- you can CAST multiple by 100, then cast it as an integer. Example:
CAST (royaltyshare * 100 as int) || '%' as percent
******************************************************************************************************
SQL (string concatenation ||)
******************************************************************************************************
The double-pipe || allows you to concatenate expressions. The syntax is: char_expr || char_expr
NOTE: Some systems use the plus (+) or period (.) for concatenation.
Several examples:
SELECT au_fname || au_lname as Writer
FROM authors
WHERE state > 'CA'
Output might look like:
Writer
AlbertRinger
AnnRinger
MichaelFrance
SylviaPanteley

We can be creative and use a combination of the SUBSTR and concatenate command to provide the first initial and last name as with:
SELECT SUBSTR(au_fname, 1, 1) || '.' || ' ' || au_lname as Writer
FROM authors
WHERE state > 'CA'
Output would now look like:
Writer
A. Ringer
A. Ringer
M. France
S. Panteley
******************************************************************************************************
SQL (substring SUBSTR function)
******************************************************************************************************
A single field (i.e., column) can be broken into multiple columns -- with the substring SUBSTR command (much as with VBA or PERL).
The syntax is: SUBSTR( expression, start, length) though length is optional -- will simply take rest of expression if left blank.
For example:
SELECT SUBSTR(title_id, 1, 2) as alpha,
SUBSTR(title_id, 3, 4) as num,
SUBSTR(title_id, 3) as no3rd,
SUBSTR(title_id, -4) as countback
FROM titles
WHERE price > 29.99

Output might look like:


alpha num no3rd countback
PC 8888 8888 8888
PC 1035 1035 1035
TC 3218 3218 3218
PS 1372 1372 1372
******************************************************************************************************
SQL (changing how NULL is displayed)
******************************************************************************************************
You can prevent confusion on the part of your audience by changing NULL to some other value using the COALESCE (with two
arguments) or ISNULL.
The syntax is: COALESCE (expr, value-to-substitute-if-null)
For example:
SELECT title_id, COALESCE (type, 'UNDECIDED') as type
FROM titles

Our output might look like this:


title_id type
PC8888 popular_comp
BU1032 business
MC3026 UNDECIDED
BU7832 business
PC9999 popular_comp
******************************************************************************************************
SQL (conditional CASE example)
******************************************************************************************************
Case function allows you to use conditional logic pretty much anyplace you can use an expression. It comes in two forms:
CASE expr
WHEN value1 THEN result1
[WHEN value2 THEN result2 . . . [ELSE resultn]
END

CASE
WHEN condition1 THEN result1
[WHEN condition2 THEN result2]. . . [ELSE resultn]
END

Example:
SELECT title_id, substr(title, 1, 25) as book,
CASE contract
WHEN 1 THEN 'contract on file'
WHEN 0 THEN 'no contract'
END as contract
FROM titles
ORDER BY title_id
***** A more complex example ********
In this example, you produce a report that compares sales date and ship date and prints notes depending on the difference. (In this
particular implementation the DATEDIFF command takes 3 arguments, though this can vary from system to system).

SELECT sales.sonum, salesdetails.sonum, sdate, date_shipped,


CASE
WHEN DATEDIFF(dd, sdate, date_shipped) BETWEEN 0 and 0 THEN 'on time'
WHEN DATEDIFF(dd. sdate, date_shipped) < 0 THEN 'ERROR-- Check Dates!'
WHEN DATEDIFF(dd, sdate, date_shipped) > 2 then 'Late!'
WHEN date_shipped is NULL then 'Not yet shipped'
END as note
FROM sales, salesdetails
WHERE sales.sonum = salesdetails.sonum

NOTE: In both the above examples, the CASE statement could be compressed into an element of the SELECT clause.
******************************************************************************************************
Report (after Query modified by Listbox)
******************************************************************************************************
Private Sub cmdProceed_Click()
If Me.Tag = "Individual" Then
For i = 0 To lstNames.ListCount - 1
If lstNames.Selected(i) = True Then
MySelectedEditor = lstNames.Column(1, i) ' Provides the Editor number of selected row into Global Variable
GoTo SKIP_ERROR
End If
Next
MsgBox "You didn't select anyone!" & vbCrLf & vbCrLf & _
"Please TRY AGAIN!", vbCritical + vbOKOnly, _
"INDIVIDUAL SELECTION ERROR"
SKIP_ERROR:
IndividualStats MySelectedEditor ' SUBROUTINE to Modify the Query Definition & Open the Report
ElseIf Me.Tag = "Teams" Then
For i = 0 To lstNames.ListCount - 1
If lstNames.Selected(i) = True Then
MySelectedTeam = lstNames.ItemData(i)
GoTo SKIP_ERROR2
End If
Next
MsgBox "You didn't select a team!" & vbCrLf & vbCrLf & _
"Please TRY AGAIN!", vbCritical + vbOKOnly, _
"TEAM SELECTION ERROR!"
SKIP_ERROR2:
TeamStats MySelectedTeam ' SUBROUTINE to Modify the Query Definition & Open the Report
End If
End Sub

Private Sub IndividualStats(ByVal MySelectedEditor As String)


' *******************************************************************************************
' Modifies Query Definition for Individual Report then runs it with value passed from the listbox
' *******************************************************************************************
Dim qdf As QueryDef
Dim MySQL As String
MySQL = "SELECT tblEmployees.Team, [Last] & "", "" & [First] AS [Reviewer Name], tblAssignment.PubName AS [Pub Name], " & vbCrLf
MySQL = MySQL & "tblAssignment.ProjectNumber AS [Proj Number], tblPubReview.R1Units AS Units, tblPubReview.R1Hours " & vbCrLf
MySQL = MySQL & "AS Hours" & vbCrLf
MySQL = MySQL & "FROM (tblAssignment INNER JOIN (tblPubReview INNER JOIN tblEmployees ON " & vbCrLf
MySQL = MySQL & "tblPubReview.Reviewer1 = tblEmployees.[Editor ID]) ON tblAssignment.AssignID = tblPubReview.AssignID)" & vbCrLf
MySQL = MySQL & "WHERE (((tblPubReview.Reviewer1) = " & MySelectedEditor & "))" & vbCrLf
MySQL = MySQL & "UNION" & vbCrLf
MySQL = MySQL & "SELECT tblEmployees.Team, [Last] & "", "" & [First] AS [Reviewer Name], tblAssignment.PubName AS [Pub Name], " &
vbCrLf
MySQL = MySQL & "tblAssignment.ProjectNumber AS [Proj Number], tblPubReview.R2Units, tblPubReview.R2Hours" & vbCrLf
MySQL = MySQL & "FROM (tblAssignment INNER JOIN (tblPubReview INNER JOIN tblEmployees ON " & vbCrLf
MySQL = MySQL & "tblPubReview.Reviewer2 = tblEmployees.[Editor ID]) ON tblAssignment.AssignID = tblPubReview.AssignID)" & vbCrLf
MySQL = MySQL & "WHERE (((tblPubReview.Reviewer2) = " & MySelectedEditor & "))" & vbCrLf
MySQL = MySQL & "UNION" & vbCrLf
MySQL = MySQL & "SELECT tblEmployees.Team, [Last] & "", "" & [First] AS [Reviewer Name], tblAssignment.PubName AS [Pub Name], " &
vbCrLf
MySQL = MySQL & "tblAssignment.ProjectNumber AS [Proj Number], tblPubReview.R3Units, tblPubReview.R3Hours" & vbCrLf
MySQL = MySQL & "FROM (tblAssignment INNER JOIN (tblPubReview INNER JOIN tblEmployees ON " & vbCrLf
MySQL = MySQL & "tblPubReview.Reviewer3 = tblEmployees.[Editor ID]) ON tblAssignment.AssignID = tblPubReview.AssignID)" & vbCrLf
MySQL = MySQL & "WHERE (((tblPubReview.Reviewer3) = " & MySelectedEditor & "))" & vbCrLf
MySQL = MySQL & "ORDER BY 2;"
Set qdf = CurrentDb.QueryDefs("qryIndividualReviewerStats")
qdf.SQL = MySQL
qdf.Close
DoCmd.OpenReport "rptIndividualStats", acViewPreview
End Sub
******************************************************************************************************
Listbox (Rowsource from SQL Query)
******************************************************************************************************
Private Sub cmdStatsByPerson_Click() ' Statistics for grouped by Person and selected for particular Person
Dim qdf As QueryDef
Set qdf = CurrentDb.QueryDefs("qryReviewerNames")
NewSQLString = qdf.SQL ' Obtaining the SQL string from a saved query definition!
lblNames.Visible = True ' alternatively, we could have created the SQL string right here.
lstNames.RowSource = NewSQLString
lstNames.Visible = True
lstNames.Requery
cmdProceed.Visible = True
Me.Tag = "Individual" ' To show which list is being displayed -- so when we click proceed we'll know what to do
End Sub
******************************************************************************************************
Record navigation (in database/recordset)
******************************************************************************************************
Below are navigation commands with respect to records in a database (shown as functions -- as if tied to buttons):
Public Function FirstRecord()
DoCmd.GoToRecord Record:=acFirst ' The first record
End Function

Public Function PreviousRecord()


DoCmd.GoToRecord Record:=acPrevious ' The previous record (check to ensure that you're not at the BOF mark)
End Function

Public Function NextRec()


DoCmd.GoToRecord Record:=acNext ' The next record (check to ensure that you're not at the EOF mark)
End Function

Public Function LastRec()


DoCmd.GoToRecord Record:=acLast ' The last record
End Function

Public Function NewRec()


DoCmd.GoToRecord Record:=acNew ' A new record is added
End Function
Note: The above functions should be navigation buttons on any record browsing feature created for customers!!
******************************************************************************************************
Controls (moving to particular ones)
******************************************************************************************************
You can move the focus to a particular control on a form in two ways:

(a) Use the GoToControl method of the DoCmd object:


DoCmd.GoToControl controlname

Example of a sub procedure:


Private Sub cmdToControl_Click()
Dim strControl as String
strControl = "OrderDate"
DoCmd.GoToControl strControl
End Sub

Note: Moving the focus to a specific control on a subform requires using the GoToControl method twice -- you move the focus to the
subform control with the first method and then to the control on the subform with the second method. For Example:
Private Sub cmdToControlOnSubform_Click()
Dim strControl as String, strSubformControl as String
strControl = "Discount"
strSubformControl = "Orders Subform"
DoCmd.GoToControl strSubformControl
DoCmd.GoToControl strControl
End Sub

Note: Moving the focus to a specific control on an open form that isn't active also takes two steps, the first step uses the SelectObject
method of the DoCmd object to activate the open form, and the second step uses the GoToControl method to move to the control. Example:
Private Sub cmdToOtherFormControl_Click()
Dim strControl as String, strOtherForm as String
strOtherForm = "Employees"
strControl = "Title"
DoCmd.SelectObject acForm, strOtherForm
DoCmd.GoToControl strControl
End Sub
[Caution! Anticipate the possibility that a form might be closed by using the IsLoaded() function to check, because attempting to
move focus to a control on a closed form will generate a rather nasty Access error!]

(b) The second method of moving the focus to a specific control on the active form uses the object's SetFocus method.
Example:
object.SetFocus ' rather simple
The SetFocus method of the Control object moves the focus to the specified control on the active form or the specified field on the
active datasheet. When the object is the Form object, the result depends on whether the form has any controls that can receive
the focus: Enabled property must be set to True; otherwise, the SetFocus method moves the focus to the form, itself. Examples:
Private Sub cmdSetControl_CLick()
Dim cbo as ComboBox
Set cbo = Forms!Orders!EmployeeID ' Initializing an object variable to hold our combobox -- speeds execution!
cbo.SetFocus ' Moves focus to the Combobox on the active form.
End Sub

Private Sub cmdSetControlOnSubform_Click()


Dim sfr as Subform, txt as Textbox ' Declaring our object variables
Set sfr = Forms!Orders![Orders Subform] ' Assigning the Subform variable its value
Set txt = Forms!Orders![Orders Subform]!Quantity ' Assigning the Textbox variable its value
sfr.SetFocus ' Moving to the subform
txt.SetFocus ' Moving to the control on the subform
End Sub

Private Sub cmdSetOtherFormControl_Click() ' Basically the same except that we going to an inactive form rather
Dim frm as Form, txt as TextBox ' than to a subform (as above)
Set frm = Forms![Employees]
Set txt = Forms![Employees]!Extension
frm.SetFocus
txt.SetFocus
End Sub

******************************************************************************************************
Synchronizing Forms
******************************************************************************************************
Private Sub cmdViewCustomer_Click() ' A command button on the Orders form to view the record for this customer
Dim strForm as String
Dim strWhere as String
strForm = "Customers" ' The form we want to open
strWhere = "CustomerID = " & Chr(39) & Forms!Orders!CustomerID & Chr(39) ' Referencing the field on which to sync
DoCmd.OpenForm FormName:=strForm, WhereCondition:=strWhere ' Passing the particular value in the field to same field
End Sub
<< See also pages 473 et seq. in Access 2002 VBA Handbook for more in depth discussion. Such as keeping the forms synchronized,
closing one form from another form, etc. >>
******************************************************************************************************
Opening a Form (hiding & returning)
******************************************************************************************************
Public Function OpenHide (strName as String) ' A generic function to place in the standard module
Dim strHide as String
strHide = Screen.ActiveForm.Name ' Stores name of active form in the variable
Screen.ActiveForm.Visible = False ' Hides the active form
DoCmd.OpenForm strName ' Opens the form which name was passed to the function
Screen.ActiveForm.Tag = strHide ' Sets the Tag property available on all forms to the variable name of originating form
End Function
Public Function CloseUnhide()
Dim strUnhide as String
If IsNull(Screen.ActiveForm.Tag) Then ' The form could have been opened directly, so the property would be empty
DoCmd.Close
Else
strUnhide = Screen.ActiveForm.Tag ' Reading the property
DoCmd.Close ' Closing the Active form
DoCmd.SelectObject acForm, strUnhide ' Unhiding the previously hidden form -- note: it was already open!
End If
End Function
******************************************************************************************************
SysCmd function
******************************************************************************************************
The SysCmd function is actually 3 functions in one. You can use it to:
Return the state of a Database window object
A common use of the SysCmd function is to determine the state of a database object -- whether the object (such as a table,
query, form, report, data access page) is open, whether the object is new, or whether the design of the object has been changed
but not yet saved. The syntax is:
returnvalue = SysCmd(acSysCmdGetObjectState, objecttype, objectname)

Object types are:


a) acTable
b) acQuery
c) acForm
d) acReport
e) acMacro
f) acModule
g) acDataAccessPage
h) acDefault
i) acDiagram
j) acServerView
k) acFunction
l) acStoredProcedure

Return values can be any of the following:


a) 0 ' Not open or does not exist
b) 1 ' Open
c) 2 ' Changed but not saved
d) 4 ' New
Return Access system information
You can also use the SysCmd function to tell you various information about the Access system. The syntax is:
returnvalue = SysCmd(action)
Possible actions are:
SysCmd(acSysCmdAccessVer) ' The version number of Access
SysCmd(acSysCmdGetWorkgroupFile) ' The path to the workgroup file.
SysCmd(acSysCmdRuntime) ' Whether you are running a run-time version of Access (as created by
someone with the Access Developer's toolkit).
SysCmd(acSysCmdAccessDir) ' Name of the directory that contains Msaccess.exe
SysCmd(acSysCmdProfile) ' The /profile setting specified when starting Access from command line.

Display a progress meter


You can use the SysCmd function to display a progress meter with text or to display a text message in the status bar to
indicate the progress of an operation. The syntax is:
returnvalue = SysCmd(action[, text][, value])
[Note: Also see link "Progress Meter in Status Bar"]
******************************************************************************************************
Timer function (timing a process)
******************************************************************************************************
Public Function QueryRunTime(strQueryName As String) as Single ' Query you want to run is taken in as an argument
Dim sngBegin as Single, sngEnd as Single
sngBegin = Timer
DoCmd.OpenQuery strQueryName ' The query is executed here with the DoCmd.OpenQuery command
sngEnd = Timer
QueryRunTime = sngEnd - sngBegin
MsgBox strQueryName & " run time is " & QueryRunTime ' Timer results in message box
End Function
******************************************************************************************************
Opening a Form (simple ex)
******************************************************************************************************
Public Function OpenAForm(strFormName as String) as Integer
Screen.ActiveForm.Visible = False ' Hiding the current form
DoCmd.OpenForm strFormName
End Function
<< also see Access 2002 VBA Handbook pgs. 468 et seq. >>
******************************************************************************************************
Sub Procedures (rules for calling/using)
******************************************************************************************************
The following rules apply to the use of sub procedures:
Because a sub procedure doesn't return a value, you can't use a sub procedure in an expression. If you want to run a sub
procedure as part of an expression, you can create a function that calls the subroutine and then use the function in an
expression.
You also can't call a sub procedure directly from a macro; however, you can create a function procedure that calls the sub
procedure and run the function using the RunCode macro action.
You run a sub procedure in the Immediate Window using the same syntax that you use to run a function when the return value
is discarded. The syntax you use depends on whether the sub procedure is stored in a standard module or a form or report
module. When a sub procedure is stored in a standard module, use either of these:
Call subroutine (argumentlist)
subroutine argumentlist
If the sub procedure is stored in a form or report module, you must use the fully qualified reference for the function in these
expressions; for example:
Form_formname.subroutine argumentlist
You can use the Run method to run a sub procedure from another application (other than Access) through Automation:
Application.Run subroutine [, argument1, argument2, . . . , argumentN]
******************************************************************************************************
Functions (rules for calling/using)
******************************************************************************************************
The following rules apply to the use of functions:
If you use the Call keyword to call a function that requires arguments, you must enclose the argument list in parentheses.
Call functionname (argumentlist)
If you don't use the Call keyword, you must omit the parentheses around the argument list.
funcitonname argumentlist
You can call a public but not a private function stored in another module. [If so, you may need to include the module name in
the reference. For example Form_frm1.Function1(3)
You can use an event to trigger a function. To run a function when an event occurs, use the following syntax in the event's
property box: = functionname (argumentlist) [NOTE: The syntax for the event property setting uses the function name
without a qualifying module name; in fact, you cannot use the fully qualified reference for a procedure as the event property
setting.]
You can use an event to trigger a public function stored in a standard module, as long as the function's name is unique and
doesn't require a fully qualified reference, but you can't trigger a public function stored in another form or report module
because using a function in another form or report module requires the fully qualified reference!
Because a sub procedure doesn't return a value, you can't use a sub procedure in an expression. If you want to run a sub
procedure as part of an expression, you can create a function that calls the subroutine and then use the function in an
expression.
******************************************************************************************************
Concatenation Function
******************************************************************************************************
Public Function Concatenate (ByVal A as String, ByVal B as String)
Concatenate = A & ", " & B
End Function
******************************************************************************************************
VBE Shortcuts
******************************************************************************************************
VBE Shortcuts:
Ctl + F ' Displays Find dialog
F3 ' Find Next (repeats previous Find)
Ctl + H ' Displays Replace dialog (in addition to Find)
Ctl + J ' Lists properties and methods for the code element or statement containing the insertion point
Ctl + I ' Displays the syntax information for the variable, constant, or procedure containing the insertion point.
Ctl + Shft + I ' Displays all parameters for the statement containing the insertion point.
Shft + F2 ' Displays the procedure code for the procedure name at the insertion point.
Ctl + Shft + F2 (or Alt-W + 2) ' Returns to the line position you were viewing in the previous procedure.
Shft + F9 ' Allows you to see the value of a variable while a procedure is in break mode.
F9 ' Toggles the breakpoint on or off on the current line.
Ctl + Shft + F9 ' Clears all breakpoints.
Ctl + F9 ' Sets the next statement in the procedure to be executed (while in break mode).
******************************************************************************************************
Finding records ("walking the recordset")
******************************************************************************************************
Private Sub cmdRecordset_Click() ' Actual example!
Dim rst As Recordset ' Establishing a recordset variable (for the cloned recordset)
DoCmd.OpenForm "frmPubReviewInfo" ' Opening the form -- which creates a recordset (the controls are bound)
Form_frmPubReviewInfo.Visible = False ' Hides the form
Set rst = Form_frmPubReviewInfo.RecordsetClone ' Clones the recordset
rst.MoveFirst ' Moves to the first record
Do ' Loops through all the records until the EOF point is reached
If Len(rst!Reviewer1) > 0 Then
Debug.Print rst!Reviewer1 & Chr(9) & rst!Reviewer2 & Chr(9) & rst!Reviewer3
End If
rst.MoveNext
Loop Until rst.EOF ' Not the most efficient method -- SEE NOTE BELOW
End Sub

Private Sub cmdRecordset_Click()


Dim rst as ADODB.Recordset
Set rst = Me.RecordsetClone
rst.MoveFirst
Do
Debug.Print rst!LastName
rst.MoveNext
Loop Until rst.EOF
End Sub
' ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/ //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
How to speed up database access
Here is a trick to loop through a recordset faster. Often when looping through a recordset people will use the
following code:
Do while not records.eof
combo1.additem records![Full Name]
records.movenext
Loop

The problem is that everytime the database moves to the next record it must make a check to see if it has
reached the end of the file. This slows the looping down a great deal. When moving or searching throuch a
large record set this can make a major difference. Here is a better way to do it.
records.movelast
lngRecCount=records.RecordCount
records.movefirst
For lngCounter=1 to lngRecCount
combo1.additem records![Full Name]
records.movenext
Next lngCounter

You should see about a 33% speed increase


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
** alternatively -- can use a function to which an object is passed (associated with recordset) **

Public Function FormRecordset(frm as Form)


Dim rst as ADODB.Recordset
Set rst = frm.RecordsetClone
rst.MoveFirst
Do
Debug.Print rst(0)
rst.MoveNext
Loop Until rst.EOF
End Function
******************************************************************************************************
DoCmd object
******************************************************************************************************
You can use the DoCmd object to run 49 of the 56 macro actions as corresponding VBA methods. The 7 macro actions without
corresponding DoCmd equivalents are:
MsgBox ' Use the MsgBox() function
RunApp ' Use the Shell() function.
SendKeys ' Use the SendKeys statement.
SetValue ' Use the assignment statement with the equal (=) sign as an assignment operator.
StopAllMacros ' There is no way to stop all running macros from within a VBA procedure. Use a Stop statement to suspend execution or
an End statement to termination execution.
StopMacro ' There is no way to stop a macro from within a VBA procedure there are various alternatives Exit Whatever, etc.
<< see Access 2002 VBA Handbook pgs. 228 et seq. >>
******************************************************************************************************
Properties and methods of Combo and List Box objects
******************************************************************************************************
Properties of Combo Box and List Box controls:
ItemData (Read-only; Variant) ' Returns the bound column for the specified row in a combo box or list box.
ListCount (Read/write; Long integer) ' Determines the number of rows in a list box or in the list box portion of a combo box.
ListIndex (Read/write; Long integer) ' Determines which item is selected in a list box or in the list box portion of a combo box.
Selected (Read/write; Long integer) ' Selects an item or determines if an item is selcted in a list box (only).
Methods of Comb Box and List Box controls:
AddItem ' Adds a new item to the list displayed by the combo box or list box.
Dropdown ' Forces a combo box to drop down.
RemoveItem ' Removes an item from the list displayed by the combo box or list box.
******************************************************************************************************
Properties and methods of Control object
******************************************************************************************************
VBA-only (note there are many regular properties also accessible via VBA) properties of Control objects are:
ControlType (Read/write; Byte) ' Determines or changes the type of control on a form. The property is set in form Design view.
Hyperlink (Read-only; hyperlink object) ' Access the properties and methods of a Hyperlink object associated with a command
button, label, or image.
Methods of Control objects:
Move (Applies to visible controls) ' Relocates a control to the location you specify in twips from the upper and left edges of the
program space.
Requery (Data controls) ' Updates a control on a form. The syntax is object.Requery where object refers to the control.
If you omit object, the method requeries the source of the active control on the active form.
Example: Forms!Customers!Country.Requery
SetFocus (Controls that can receive focus) ' Moves the focus to the specified control on the active form. You can move the focus only to
a visible, enabled control. Some control properties can be set only when a control doesn't
have the focus, such as the control's Visible, Locked, or Enabled properties.
Example: Forms!Customers!Country.SetFocus
SizeToFit (Controls that have a size) ' Sizes a control to fit the text or the image it contains.
Undo (Controls that have a value) ' Resets a control when its value has changed. Has the same effect as pressing the Escape key.
******************************************************************************************************
Properties and Methods of Module object
******************************************************************************************************
<< see pages 219 & 220 of Access 2002 VBA Handbook >>
******************************************************************************************************
Properties and Methods of Report object
******************************************************************************************************
Some additional properties of the Report object are:
DefaultControl (Read-only; control) ' Sets the default properties for a particular type of control on a report.
Me (Read-only; report object) ' Refers to the report itself in a VBA procedure stored in the report's module.
Module (Read-only; object) ' Refers to a report's module. If you refer to the Module property of a report with the HasModule
property set to False, an error occurs check it first.
PrintCount (Read/write; integer) ' Returns or sets the number of times the OnPrint property has been evaluated for the current
section of a report.
Methods of the Report object are:
Circle ' Draws a circle, an ellipse, or an arc on a report when the Print event occurs.
Line ' Draws lines and rectangles on a report when the Print event occurs.
Move ' Relocates a report to the location you specify, in twips from the upper and left edges of the program space.
Print ' Prints the text on a report.
PSet ' Sets a point on a report to a specified color when the Print event occurs.
Scale ' Defines the coordinate system for a Report object.
TextHeight ' Returns the height of a text string as it would be printed in the current font.
TextWidth ' Returns the width of a text string as it would be printed in the current font.
******************************************************************************************************
Properties and Methods of the Form object
******************************************************************************************************
Some additional properties of the Form object are:
AllowDesignChanges (Read/write; boolean) ' Specifies whether changes can be made to a form in all views or only in Design view.
Bookmark (Read/write; variant) ' Stores the value of the current record's bookmark as a unique binary string expression created
by Access for each record each time you open the form. Available only for the form's current
record.
DefaultControl (Read-only; control) ' Sets the default properties for a particular type of control on a form.
Me (Read-only; form object) ' Refers to the form itself when used in the Me property in a VBA procedure stored in a form's
module.
Module (Read-only; object) ' Refers to a form's module. If you refer to the Module property of a form with the HasModule
property set to False, an error occurs so you might wish to check it first.
OpenArgs (Read/write; variant) ' Determines the string expression specified as the OpenArgs argument of the OpenForm
method.
UniqueTable (Read/write; string) ' Specifies or determines a table to be updatable when a form is bound to a multiple table view or
stored procedure in an Access project.
Methods of the Form object are:
GoToPage ' Moves the focus to the first control on a specified page of the active form (which can contain multiple pages).
Move ' Relocates a form to the location you specify, in twips from the upper and left edges of the program space.
Recalc ' Updates all calculated controls on a form. This method has the same effect as pressing the F9 function key when the
form has the focus Use Recalc to updated calculated controls that have ControlSource expressions based on other controls
or fields in the form's data source. When the ControlSource property setting includes domain or SQL aggregate
functions, use the Requery method instead.
Refresh ' Updates the records in the current set with changes to the existing data made by you or by others in a multiuser
environment. This method does not change the recordset to include records that were added or exclude records that were
deleted since the current set was last requeried and does not exclude changed records that may no longer satisfy the
query or filter criteria that are specified in the form's data source.
Repaint ' Updates the screen and completes any pending recalculation of the form's controls. Use Repaint to update the screen
when repainting has been delayed while Access carries out other tasks.
Requery ' Updates the data source of the specified form. The syntax is object.Requery, where object refers to the form; if you omit
object, the method requeries the source of the active form.
SetFocus ' Moves the focus to the control that last had the focus on the specified form. If the specified form has no enabled controls,
this method moves the focus to the form itself. You can move the focus only to a visible form.
Undo ' Resets a form that has changed. All changes to the form are discarded. This method is equivalent of pressing the
Escape key.

******************************************************************************************************
Properties and Methods of Application object
******************************************************************************************************
Some additional properties of the Application object are:
BrokenReference (Read-only; boolean) ' Checks to see if the project contains any broken references to databases or type libraries.
CodeContextObject (Read-only; object) ' Determines the object in which a VBA procedure is running.
CurrentObjectName (Read-only; string) ' Use to determine which database object has the focus, or in which object is running code or
has the focus.
CurrentObjectType (Read-only; integer) ' Returns the object type of the object that currently has the focus.
FeatureInstall (Read/write; enum const) ' Determines how to handle situations in which calls are made to methods and properties that
require features not yet installed.
IsCompiled (Read-only; boolean) ' Determines whether all the modules in the project are in a compiled state. Returns True if all
the modules are in a compiled state.
VBE (Read-only; VBE object) ' Represents the VBA editor and can be used to return a reference to the VBA editor object and
its related properties.
Visible (Read/write; boolean) ' Shows or hides the Access application. If the Visible property is True, the application is visible;
otherwise, the application isn't visible. When the user starts Access, the Visible property is True
and can't be changed. When another application using Automation starts Access, the Visible
property is False by default. You can set this property in a VBA procedure only when the
application is started by another application using Automation (and when the UserControl
property value is False).

Methods of the Application object are:


Addto Favorites ' Adds the name of the current database to the Favorites folder.
BuildCriteria ' Returns a parsed criteria string as it would appear in a Criteria cell in query design grid or as a Filter property
setting. Use this method to construct criteria for a query of filter based on user input.
CloseCurrentDatabase ' Closes the current database or project (the one that is open in the Access window) from another application that is
controlling Access through Automation. After you close the database that is open in the current instance of
Access, you can create a new database or open another existing database in the same instance of Access.
CodeDb ' Returns the name of the database in which code is running.
CompactRepair ' Compacts and repairs the database project.
ConvertAccessProject ' Converts a database from one version of Access to another.
CreateAccessProject ' Creates a new project from within Access, or from within another application using OLE Automation.
CreateControl ' Creates a control on an open form.
CreateForm ' Creates a new form in minimized, Design view.
CreateGroupLevel ' Specifies groupings within a form or report.
CreateNewWorkgroupFile ' Creates a new workgroup file for a specified user.
CreateReport ' Creates a new report in minimized, Design view.
CreateReportControl ' Creates a control on an open report.
Echo ' Turns screen painting off. If you turn screen painting off in a VBA procedure, you must turn it back on or it
will remain off, even if you press Ctl-Break or if the procedure encounters a breakpoint.
Eval ' Evaluates a text-string or numeric-value expression.
ExportXML ' Exports data fro the specified Access object as XML files.
FollowHyperlink ' Opens the document or Web page specified in the hyperlink address that you supply as an argument. Use this
method to follow a hyperlink supplied by you or by the user.
GetHiddenAttribute ' Returns the value (Boolean) of a hidden attribute of an object.
GetOption ' Returns the current value of an option in the Options dialog.
HyperlinkPart ' Returns information about data stored as a hyperlink.
ImportXML ' Imports data about a specified Access object from XML files.
LoadPicture ' Loads a graphic into an ActiveX control.
NewAccessProject ' Creates and opens a new Access project.
NewCurrentDatabase ' Creates a new Database object in the Access window from another application that is controlling Access through
Automation. This method adds the database to the Databases collection automatically. After you create an
instance of Access from another application, you must either create a new database or open an existing database.
Nz ' Returns a zero-length string when a variant's value is Null.
OpenAccessProject ' Opens an existing Access project.
OpenCurrentDatabase ' Opens an existing Database object as the current database in the Access window from another application that is
controlling Access through Automation.
Quit ' Quits Access. This method has the same effect as selecting FILE | EXIT. Use this method's option argument to
specify treatment for any unsaved objects.
RefreshDatabaseWindow ' Updates the Database window after a Database window object has been added, deleted, or renamed.
RefreshTitleBar ' Updates the Access title bar after you set the AppTitle or AppIcon startup properties in VBA.
Run ' Runs a procedure you have defined in an Access database from another application through Automation or from
another Access database.
RunCommand ' Runs a built-in menu or toolbar command.
SetHiddenAttribute ' Sets an object's hidden attribute.
SetOption ' Sets the current value of an option in the Options dialog. Use SetOption together with GetOption to observe
and change the environmental options in VBA. You can get and set any option in the Options dialog. Changes
you make are permanent; if you want to restore the original values when you close a database, hold them in public
variables.
SysCmd ' Displays text or a progress meter in the status bar. Also returns information about Access and associated program
files, or returns the state of a specified database object.
*** and others ***
DAvg DCount DDEExecute DDEInitiate DDEPoke DDERequest DDETerminate
DDETerminateAll DefaultWorkspaceClone DFirst DLast DLookup
DMax DMin DStDev DstDevP DSum DVar DVarP
EuroConvert GUIDFromString hWndAccessApp SetDefaultWorkgroupFile
StringFromGUID
******************************************************************************************************
Collection (ways of referencing)
******************************************************************************************************
There are four ways to refer to an object in a collection:
collectionname!objectname ' Use the exclamation point reference to refer to an object in a collection explicitly by its name. This has
the advantage of allowing you to refer to the object using a string expression that evaluates to the object's
name.
collectionname("objectname") ' Use the index by name reference to refer to an object in a collection explicitly by its name. Similar to
the former method, in that, you can create a string variable and use that for the object reference.
collectionname(index) ' Use the index by number reference to refer to an object in a collection by the index number assigned to
it. [When you know the position number of an object in its collection, you can refer to the object using
just the number. Note: Using a For Each. . . Next you can learn the position and/or name of an object in
its collection! Remember the collection is zero-based] Looping through the collection is especially
useful if turning a property (such as "Lock") on or off for all of the objects.
collectionname(objectvariable) ' Use the index by variable reference to refer to an object in a collection by using a string variable to refer
to the object.
******************************************************************************************************
FoxPro (creating a FoxPro table)
******************************************************************************************************
Sub CreateFoxProTable()
Dim db As Database
Dim tdf As TableDef

Dim fldCustID As Field


Dim fldCustName As Field
Dim fldCustAddress As Field
Dim fldCustCity As Field
Dim fldCustState As Field
Dim fldCustZip As Field

Dim idxCustID As Index


Dim fldCustIDIX As Field

Set db = DBEngine(0).OpenDatabase("C:\LABS\LAB02\SOLUTION", False, False, "FoxPro 2.5;")

Set tdf = db.CreateTableDef("DBCUST")

Set fldCustID = tdf.CreateField("CustID", dbText, 10)


Set fldCustName = tdf.CreateField("Name", dbText, 30)
Set fldCustAddress = tdf.CreateField("Address", dbText, 30)
Set fldCustCity = tdf.CreateField("City", dbText, 30)
Set fldCustState = tdf.CreateField("State", dbText, 10)
Set fldCustZip = tdf.CreateField("Zip", dbText, 10)

tdf.Fields.Append fldCustID
tdf.Fields.Append fldCustName
tdf.Fields.Append fldCustAddress
tdf.Fields.Append fldCustCity
tdf.Fields.Append fldCustState
tdf.Fields.Append fldCustZip

Set idxCustID = tdf.CreateIndex("CustID")


Set fldCustIDIX = idxCustID.CreateField("CUSTID")

idxCustID.Fields.Append fldCustIDIX
idxCustID.Unique = True
tdf.Indexes.Append idxCustID

db.TableDefs.Append tdf

End Sub
******************************************************************************************************
FoxPro (linking to a FoxPro database)
******************************************************************************************************
Sub LinkTable()

Dim dbsJet As Database


Dim FoxTable As TableDef

' Open database


Set dbsJet = CurrentDb()

' Create TableDef object.


Set FoxTable = dbsJet.CreateTableDef("LinkedFoxProTable")

' Set connection information.


FoxTable.Connect = "FoxPro 3.0;DATABASE=C:\LABS\LAB02\SOLUTION"
FoxTable.SourceTableName = "Categori"

' Append TableDef object to create link.


dbsJet.TableDefs.Append FoxTable
' Display confirmation message.

MsgBox "Finished linking " & FoxTable.SourceTableName & ".", 0

End Sub
******************************************************************************************************
Finding records (SQL string -- simple ex.)
******************************************************************************************************
NOTE: This example searches the "CategoryID" field within the table "Categories" for any CategoryID's greater than or equal to 1.
Then lists the result in the immediate window.

Private Sub CheckSQL() ' A made up name for my subroutine to run the function
Call BuildSQL("1") ' Passing the function the criteria -- could make this more flexible!
End Sub

Function BuildSQL(Criteria As String)


Dim MyDB As Database
Dim MyRecSet As Recordset
Dim SQLString As String

SQLString = "SELECT * FROM Categories WHERE CategoryID >= " & Criteria ' The SQL search string
Set MyDB = CurrentDb
Set MyRecSet = MyDB.OpenRecordset(SQLString) ' Creating a recordset using the SQL string
While Not MyRecSet.EOF
Debug.Print MyRecSet!CategoryID & ": " & MyRecSet!CategoryName ' Looping through the created recordset and outputing value
MyRecSet.MoveNext
Wend
MyRecSet.Close ' Closing the recordset
Set MyRecSet = Nothing ' Eliminating the object variable
Set MyDB = Nothing ' Eliminating the other object variable
End Function
******************************************************************************************************
Transaction process (example)
******************************************************************************************************
Sub ChangeTitle()
Dim strName As String, strMessage As String, strPrompt As String
Dim wspDefault As Workspace, dbsNorthwind As Database
Dim rstEmployees As Recordset
strPrompt = "Change title to Account Executive?"
Set wspDefault = DBEngine.Workspaces(0) ' Get default Workspace.
Set dbsNorthwind = wspDefault.Databases(0) ' Get current database.
Set rstEmployees = dbsNorthwind.OpenRecordset("Employees", dbOpenTable) ' Open table.

wspDefault.BeginTrans ' Start of transaction.


rstEmployees.MoveFirst
Do Until rstEmployees.EOF
If rstEmployees![Title] = "Sales Representative" Then
strName = rstEmployees![LastName] & _
", " & rstEmployees![FirstName]
strMessage = "Employee: " & strName & vbCrLf & vbCrLf
If MsgBox(strMessage & strPrompt, vbQuestion + vbYesNo, _
"Change Job Title") = vbYes Then
rstEmployees.Edit ' Enable editing.
rstEmployees![Title] = "Account Executive"
rstEmployees.UPDATE ' Save changes.
End If
End If
rstEmployees.MoveNext ' Move to next record.
Loop
If MsgBox("Save all changes?", vbQuestion + vbYesNo, _
" Save Changes") = vbYes Then
wspDefault.CommitTrans ' Commit changes.
Else
wspDefault.Rollback ' Undo changes.
End If

rstEmployees.Close ' Close table.


dbsNorthwind.Close
End Sub
******************************************************************************************************
Finding a record (via seek method)
******************************************************************************************************
Function SeekRecord()
Dim MyDB As Database
Dim MyRec As Recordset

Set MyDB = CurrentDb()


Set MyRec = MyDB.OpenRecordset("Categories", dbOpenTable)
MyRec.Index = "PrimaryKey" ' Define current index.
MyRec.Seek "=", 3 ' Seek record.
If MyRec.NoMatch Then
MsgBox "Record Does Not Exist"
Else
MsgBox "Category ID: " & MyRec![CategoryID] & vbCrLf & "Category Name: " & MyRec![CategoryName]
End If
End Function
******************************************************************************************************
Finding a record (simple form)
******************************************************************************************************
Function FindRecord()

Dim MyDB As Database


Dim MyRec As Recordset
Dim Criteria As String

Set MyDB = CurrentDb()


Set MyRec = MyDB.OpenRecordset("Categories", dbOpenDynaset)
Criteria = "CategoryName Like 'C*'"
MyRec.FindFirst Criteria
Do Until MyRec.NoMatch
Debug.Print MyRec![CategoryID]
Debug.Print MyRec![CategoryName]
MyRec.FindNext Criteria
Loop
End Function
******************************************************************************************************
Word Automation (simple example)
******************************************************************************************************
Private Sub cmdModifyDocument_Click()
Dim wrdApp As New Word.Application
Dim wrdDoc As Word.Document

wrdApp.Visible = True

Set wrdDoc = wrdApp.Documents.Add

With wrdDoc
.Range.InsertBefore "Hello there everyone!"
.SaveAs "C:\Greetings.doc"
End With

Set wrdApp = Nothing


End Sub
******************************************************************************************************
Filename & Path information
******************************************************************************************************
Function GetDBPath() As String

Dim MyDB As DATABASE


Dim tempPath As String
Dim FileLen As Integer

' Set MyDB to the current database.


Set MyDB = CurrentDb()

' get the length of the db file name


FileLen = Len(Dir(MyDB.Name, vbDirectory))

' trims off the db filename so the path is all that's left.
tempPath = Left(MyDB.Name, (Len(MyDB.Name) - FileLen))
' Return the path name
GetDBPath = tempPath

End Function
******************************************************************************************************
Security (granting read permission to User)
******************************************************************************************************
'Grant read permissions only to a table for a specific user
Sub GrantReadPermissions()
Dim Db As Database
Dim doc As Document

Set Db = CurrentDb()

Set doc = Db.Containers("Tables").Documents("Categories")


With doc
.userName = "A_Manager"
.Permissions = .Permissions Or dbSecRetrieveData
End With
End Sub
******************************************************************************************************
Groups (list groups & users)
******************************************************************************************************
'Lists all groups and the users that belong to that group
Sub ShowGroupsAndUsers()
Dim ws As Workspace
Dim usr As User
Dim grp As Group

Set ws = DBEngine.Workspaces(0)

With ws
.Groups.Refresh
.Users.Refresh
End With

For Each grp In ws.Groups


Debug.Print "**********************"
Debug.Print UCase(grp.Name) & " Group has the following users:"
For Each usr In grp.Users
Debug.Print " " & usr.Name
Next usr
Next grp
End Sub
******************************************************************************************************
Users (list all users and groups they belong to)
******************************************************************************************************
'Lists all users and the groups they belong to
Sub ShowUsersAndGroups()
Dim ws As Workspace
Dim usr As User
Dim grp As Group

Set ws = DBEngine.Workspaces(0)

With ws
.Groups.Refresh
.Users.Refresh
End With

For Each usr In ws.Users


Debug.Print "**********************"
Debug.Print UCase(usr.Name) & " is in the following groups:"
For Each grp In usr.Groups
Debug.Print " " & grp.Name
Next grp
Next usr

End Sub
******************************************************************************************************
Users (list all Users in workspace)
******************************************************************************************************
Option Compare Database
Option Explicit

'Lists all users in a workspace


Sub ShowAllUsers()
Dim ws As Workspace
Dim usr As User

Set ws = DBEngine.Workspaces(0)
ws.Users.Refresh
For Each usr In ws.Users
Debug.Print "User: " & usr.Name
Next usr
End Sub

'Lists all Groups in a Workspace


Sub ShowAllGroups()
Dim ws As Workspace
Dim grp As Group

Set ws = DBEngine.Workspaces(0)
ws.Groups.Refresh
For Each grp In ws.Groups
Debug.Print "Group: " & grp.Name
Next grp
End Sub
******************************************************************************************************
IsLoaded Utility (to check if Form is open)
******************************************************************************************************+
Option Compare Database
Option Explicit

Function IsLoaded(ByVal strFormName As String) As Integer


' Returns True if the specified form is open in Form view or Datasheet view.

Const conObjStateClosed = 0
Const conDesignView = 0

If SysCmd(acSysCmdGetObjectState, acForm, strFormName) <> conObjStateClosed Then


If Forms(strFormName).CurrentView <> conDesignView Then
IsLoaded = True
End If
End If

End Function
******************************************************************************************************
Word automation (creating a credit letter)
******************************************************************************************************
Option Compare Database
Option Explicit

' default template


Private Const gconTemplate As String = "C:\BegVBA\Credit.DOT"

Sub CreditLetter(lngPersonID As Long)


'
' Purpose: Create a credit information letter
' Arguments: lngPersonID The id of the person at the company
'

Dim dbC As Database ' current database


Dim qryContact As QueryDef ' querydef of contact
Dim recContact As Recordset ' recordset of contact details
Dim objWord As Word.Application ' word object
Dim objDoc As Object ' document object

' find the company and contact details


Set dbC = CurrentDb()
Set qryContact = dbC.QueryDefs("qryCompanyContacts")
qryContact.Parameters("PID") = lngPersonID
Set recContact = qryContact.OpenRecordset()
If recContact.EOF Then
MsgBox "The person with an ID of " & lngPersonID & " could not be found.", vbCritical, "Contact Not Found"
Exit Sub
End If

' create word and a letter based on the template


Set objWord = New Word.Application
objWord.Visible = True
Set objDoc = objWord.Documents.Add(gconTemplate)

' fill in the details


InsertTextAtBookmark objWord, objDoc, "Contact", recContact("FullName")
InsertTextAtBookmark objWord, objDoc, "CompanyName", recContact("CompanyName")
InsertTextAtBookmark objWord, objDoc, "Address", recContact("Address")
InsertTextAtBookmark objWord, objDoc, "Town", recContact("Town")
InsertTextAtBookmark objWord, objDoc, "PostCode", recContact("PostCode")
InsertTextAtBookmark objWord, objDoc, "Dear", recContact("FirstName")

' close up
objDoc.SaveAs "C:\BegVBA\CredLet"
objDoc.Close
objWord.Quit
recContact.Close
Set objDoc = Nothing
Set objWord = Nothing
End Sub

Sub InsertTextAtBookmark(objW As Object, objD As Object, strBkmk As String, varText As Variant)


'
' Purpose: Insert text at a bookmark
' Arguments: objW the word object
' objD the word document object
' strBkmk the bookmark name
' varText the text to insert
' Returns: none
'

' select the required bookmark, and set the selection text
objD.Bookmarks(strBkmk).Select
objW.Selection.Text = varText

End Sub
******************************************************************************************************
Outlook (sending an invoice reminder)
******************************************************************************************************
Option Compare Database
Option Explicit

Private Const conDefaultDelay As Integer = 21 ' default delay


Private Const conMaxDelay As Integer = 45 ' maximum delay

Private dbC As Database ' the current database

Sub InvoiceReminder(strOrderNumber As String)


'
' Purpose: Create an invoice reminder in Outlook
' Arguments: strOrderNumber The order number to create the reminder for
' Returns: none
'

Dim objOutlook As Outlook.Application ' the outlook application


Dim objRem As AppointmentItem ' the new reminder
Dim qryI As QueryDef ' the invoice query
Dim recI As Recordset ' the invoice details
Dim datDue As Date ' date payment due

' get the invoice details


Set dbC = CurrentDb()
Set qryI = dbC.QueryDefs("qryInvoiceDetails")
qryI.Parameters("OrderNum") = strOrderNumber
Set recI = qryI.OpenRecordset()
If recI.EOF Then
MsgBox "This order was not found. Please check the number and try again", 0, "Order Not Found"
Exit Sub
End If

' start Outlook and create the reminder


Set objOutlook = New Outlook.Application
Set objRem = objOutlook.CreateItem(olAppointmentItem)
' set the reminder details
' DateDue is the n days from the date sent, where n is
' the maximum delay less the average payment delay. Therefore, the quicker
' they pay, the more time they get next time.
datDue = Format(recI("SentDate") + conMaxDelay - AveragePaymentDelay(recI("LocationID")), "dd-mmm-yyyy 10:00:00")
With objRem
.Start = datDue
.End = datDue
.ReminderSet = True
.ReminderOverrideDefault = True
.Subject = "Payment Due: " & recI("CompanyName") _
& vbCrLf & "Location: " & recI("LocationName") _
& vbCrLf & "Order Number: " & recI("OrderNumber") _
& vbCrLf & "Sent On: " & recI("SentDate")
.Close (olSave)

End With

' close up and exit


recI.Close
Set objRem = Nothing
Set objOutlook = Nothing

End Sub

Function AveragePaymentDelay(lngLocationID As Long) As Integer


'
' Purpose: Find the average payment delay for a location
' Arguments: lngLocationID The id of the location
' Returns: The average delay
'

Dim recDelay As Recordset ' delay recordset

Set recDelay = dbC.OpenRecordset("qryAveragePaymentDelay")


recDelay.FindFirst "LocationID = " & lngLocationID
If recDelay.NoMatch Then
AveragePaymentDelay = conDefaultDelay
Else
AveragePaymentDelay = recDelay("AverageDelay")
End If
recDelay.Close

End Function
******************************************************************************************************
Refresh Table Links
******************************************************************************************************
Public Function RefreshTableLinks(strDB As String) As Integer
'
' Purpose: To relink the tables from a database
' Arguments: strDB The database to link from
' Returns: True if the tables were linked sucessfully,
' False otherwise
'
Dim dbCurrent As Database ' current database
Dim tblLinked As TableDef ' current table in collection

On Error GoTo RefreshTableLinks_Err

' loop through the tables


Set dbCurrent = CurrentDb()
For Each tblLinked In dbCurrent.TableDefs
' if it is a linked table, then reset the connect
' string and refresh the link
If tblLinked.Connect <> "" Then
tblLinked.Connect = ";DATABASE=" & strDB
tblLinked.RefreshLink
End If
Next

RefreshTableLinks = True

RefreshTableLinks_Exit:
Exit Function

RefreshTableLinks_Err:
MsgBox Error$
RefreshTableLinks = False
Resume RefreshTableLinks_Exit

End Function
******************************************************************************************************
Excel (link a spreadsheet as if a table)
******************************************************************************************************
Public Sub LinkSpreadsheet()
'
' Purpose: To link a spreadsheet into Access
' Arguments: none
' Returns: none

' This links the spreadsheet into Access as though it was a table
DoCmd.TransferSpreadsheet acLink, acSpreadsheetTypeExcel97, "WhiskySales", _
"C:\BegVBA\WhiskySales.XLS"
End Sub
******************************************************************************************************
Table creation using VBA
******************************************************************************************************
Sub MakeATable()
'
' Purpose: Demonstrates creating a table
' Arguments: none
' Returns: none
'

Dim db As Database
Dim tbl As TableDef
Dim fld As Field
Dim idx As Index
'Start by opening the database
Set db = CurrentDb()

'Create a tabledef object


Set tbl = db.CreateTableDef("Bottlers")

'Create a field; set its properties; add it to the tabledef


Set fld = tbl.CreateField("BottlerID", dbLong)

fld.OrdinalPosition = 1
fld.Attributes = dbAutoIncrField

tbl.Fields.Append fld

'Create another; set its properties; add it to the tabledef


Set fld = tbl.CreateField("BottlerName", dbText)

fld.OrdinalPosition = 2
fld.Size = 50
fld.Required = True
fld.AllowZeroLength = False

tbl.Fields.Append fld

'Create an index and set its properties


Set idx = tbl.CreateIndex("PrimaryKey")

idx.Primary = True
idx.Required = True
idx.Unique = True

'Add a field to the index


Set fld = idx.CreateField("BottlerID")
idx.Fields.Append fld

'Add the index to the tabledef


tbl.Indexes.Append idx

'Finally add table to the database


db.TableDefs.Append tbl

'And refresh the database window


RefreshDatabaseWindow

'Indicate creation was successful


MsgBox "The " & tbl.Name & " table was successfully created"

End Sub
******************************************************************************************************
QueryDef (editing a query def via VBA)
******************************************************************************************************
Function ChangeQueryDef(strQuery As String, strSQL As String) As Boolean
'
' Purpose: Demonstrates editing a querydef
' Arguments: strSQL The SQL string for the querydef
' Returns: True
If strQuery = "" Or strSQL = "" Then Exit Function

Dim qdf As QueryDef

Set qdf = CurrentDb.QueryDefs(strQuery)


qdf.SQL = strSQL
qdf.Close
RefreshDatabaseWindow

ChangeQueryDef = True

End Function
******************************************************************************************************
QueryDef (constructing using VBA)
******************************************************************************************************
Function MakeQueryDef(strSQL As String) As Boolean
'
' Purpose: Demonstrates creating a querydef
' Arguments: strSQL The SQL string for the querydef
' Returns: True

If strSQL = "" Then Exit Function

Dim qdf As QueryDef

Set qdf = CurrentDb.CreateQueryDef("MyQuery")


qdf.SQL = strSQL
qdf.Close
RefreshDatabaseWindow

MakeQueryDef = True

End Function
******************************************************************************************************
Finding records (SQL search -- preferred method)
******************************************************************************************************
Sub FindBottleByPrice2(curPrice As Currency)
'
' Purpose: Demonstrates preferred way to extract restriction of data
' Arguments: none
' Returns: none
'

Dim db As Database
Dim rec As Recordset
Dim strSQL As String
Dim strMatches As String
Dim intCounter As Integer

strSQL = "SELECT * FROM Bottling WHERE Price = " & curPrice

Set db = CurrentDb()
Set rec = db.OpenRecordset(strSQL, dbOpenSnapshot)

Do Until rec.EOF
strMatches = strMatches & Chr$(10) & rec!BottlingID
rec.MoveNext
Loop

intCounter = rec.RecordCount

Select Case intCounter


Case 0
MsgBox "No bottlings cost " & Format$(curPrice, "Currency")
Case 1
MsgBox "The following bottling cost " & _
Format$(curPrice, "Currency") & " : " & _
Chr$(10) & strMatches
Case Else
MsgBox "The following " & intCounter & " bottlings cost " & _
Format$(curPrice, "Currency") & " : " & _
Chr$(10) & strMatches
End Select

rec.Close

End Sub
******************************************************************************************************
Finding records (FindFirst & FindNext methods)
******************************************************************************************************
Sub FindBottleByPrice(curPrice As Currency)
'
' Purpose: Demonstrates use of FindFirst and FindNext methods
' Arguments: none
' Returns: none
'

Dim db As Database
Dim rec As Recordset
Dim strSQL As String
Dim strMatches As String
Dim intCounter As Integer

strSQL = "Bottling"

Set db = CurrentDb()
Set rec = db.OpenRecordset(strSQL, dbOpenSnapshot)

rec.FindFirst "Price = " & curPrice


Do While rec.NoMatch = False
intCounter = intCounter + 1
strMatches = strMatches & Chr$(10) & rec("BottlingID")
rec.FindNext "Price = " & curPrice
Loop

Select Case intCounter


Case 0
MsgBox "No bottlings cost " & Format$(curPrice, "Currency")
Case 1
MsgBox "The following bottling cost " & _
Format$(curPrice, "Currency") & " : " & _
Chr$(10) & strMatches
Case Else
MsgBox "The following " & intCounter & " bottlings cost " & _
Format$(curPrice, "Currency") & " : " & _
Chr$(10) & strMatches
End Select

rec.Close

End Sub
******************************************************************************************************
Finding recs (using no-match property)
******************************************************************************************************
Sub SeekPrice2(curPrice As Currency)
'
' Purpose: Demonstrates use of NoMatch property
' Arguments: none
' Returns: none
'

Dim db As Database
Dim rec As Recordset
Dim strSQL As String
Dim strMsg As String

strSQL = "Bottling"

Set db = CurrentDb()
Set rec = db.OpenRecordset(strSQL)

rec.Index = "Price"
rec.Seek "=", curPrice

If rec.NoMatch = True Then


strMsg = "No bottlings cost " & Format$(curPrice, "Currency")
Else
strMsg = "Bottling No. " & rec("BottlingID") & " costs " & _
Format$(rec("Price"), "Currency")
End If

MsgBox strMsg

rec.Close

End Sub
******************************************************************************************************
Finding records (seek method against table)
******************************************************************************************************
'
' Purpose: Demonstrates use of Seek method against table-type
' Recordset objects
' Arguments: none
' Returns: none

Dim db As Database
Dim rec As Recordset
Dim strSQL As String
Dim strMsg As String

strSQL = "Bottling"
Set db = CurrentDb()
Set rec = db.OpenRecordset(strSQL)

rec.Index = "Price"
rec.Seek "=", curPrice
strMsg = "Bottling No. " & rec("BottlingID") & " costs " & _
Format$(rec("Price"), "Currency")

MsgBox strMsg
rec.Close
End Sub
******************************************************************************************************
Opening a Recordset
******************************************************************************************************
Sub OpenWhiskyRecordset()
'
' Purpose: Demonstrates use of OpenRecordset method
' Arguments: none
' Returns: none
'
Dim db As Database
Dim rec As Recordset
Set db = CurrentDb()
Set rec = db.OpenRecordset("Whisky")
Stop
rec.Close
End Sub

******* 2nd method shown below -- basically the same with a message box showing the record count ****

Private Sub OpeningARecordset()


'
' Purpose: Demonstrates use of OpenRecordset method
' Arguments: none
' Returns: none
Dim db As Database
Dim rec As Recordset
Dim intRecords As Integer
Set db = CurrentDb()
Set rec = db.OpenRecordset("Whisky")
intRecords = rec.RecordCount
MsgBox "There are " & intRecords & " records in the Whisky table"
rec.Close
End Sub
******************************************************************************************************
Capitalize (converts field's data to uppercase)
******************************************************************************************************
Function Capitalize(strTable As String, strFld As String)
'
' Purpose: Converts data in a given field to upper case
' Arguments: none
' Returns: none

Dim db As Database
Dim rec As Recordset

Set db = CurrentDb()
Set rec = db.OpenRecordset(strTable)

'Start to loop through all records


Do
rec.Edit 'Copy the current record to the copy buffer
rec(strFld) = UCase$(rec(strFld)) 'Make changes to the record in the copy buffer
rec.Update 'Save the contents of the copy buffer to disk
rec.MoveNext 'Make the next record the current record
Loop Until rec.EOF 'Keep on looping until we go beyond the last record
Capitalize = True

End Function
******************************************************************************************************
Containers (and documents within) listing
******************************************************************************************************
Public Sub ListContainers()
'
' Purpose: Demonstrate the use of the Containers collection
' Arguments: none
' Returns: none
'
Dim dbCurrent As Database ' current database
Dim conTest As Container ' Container object
Dim docTest As Document ' Document object
Dim objTest As Control ' Control object

' open the database


Set dbCurrent = DBEngine.Workspaces(0).Databases(0)

' loop through the containers displaying each ones name


For Each conTest In dbCurrent.Containers
Debug.Print "Container: "; conTest.Name

' and the documents for this container


For Each docTest In conTest.Documents
Debug.Print " Document: "; docTest.Name
Next

Next

End Sub
******************************************************************************************************
Date calculation (difference between 2 dates)
******************************************************************************************************
Public Function PaymentOverdue(datDateInvoiced As Date, datDatePaid As Date) As String
'
' Purpose: Demonstrate date handling
' Arguments: none
' Returns: none
'
' DateDiff calculates the difference between two dates
' The "d" says return the number of days difference
If DateDiff("d", datDateInvoiced, datDatePaid) > 30 Then
PaymentOverdue = "Overdue"
Else
PaymentOverdue = "OK"
End If
End Function
******************************************************************************************************
Compaction (automating for the User -- every 5 times used)
******************************************************************************************************
Option Compare Database
Option Explicit
Public booCompactTime As Boolean
Function Autoexec()
'***************************************************************************
'Purpose:
'Comments: 1. Compiles all code in the database if it is uncompiled
' 2. Writes usage info to custom database properties
' 3. Writes usage info to the Registry
' 4. Sets flag to indicate if db is due for compacting
'***************************************************************************

On Error GoTo Autoexec_Err

Dim strModule As String


Dim strProperty As String
Dim dtLastOpened As Date
Dim db As Database
Dim pty As Property
Dim lngDBTimesOpened As Long
Dim lngProfileTimesOpened As Long
Dim intRetVal As Integer

Set db = CurrentDb

'If the db is not compiled then open a module and


'force recompilation
If Not Application.IsCompiled Then
'Display a message indicating we are compiling
DoCmd.OpenForm "frmCompile", , , , acFormReadOnly
'Turn off screen updating
Application.Echo False
'Get the name of any module
strModule = db.Containers("Modules").Documents(0).Name
'Open the module so we can use the Compile Modules menu command
DoCmd.OpenModule strModule
'Compile and save all modules
Application.RunCommand acCmdCompileAndSaveAllModules
'Set a database property to indicate last compile time
MarkCompileTime
'Give audible confirmation
Beep
'Close the module we opened
DoCmd.Close acModule, strModule
'Turn screen updating back on
Application.Echo True
'Remove the warning form
DoCmd.Close acForm, "frmCompile"
End If

'Find out how many times this particular database has been opened
IncrementTimesOpened
lngDBTimesOpened = db.Properties("TimesOpened")
'If this is the first time for this database, then show the greeting form
If lngDBTimesOpened = 1 Then
DoCmd.OpenForm "frmGreeting", , , , , acDialog
Else
'Else open the greeting form unless the user has deselected the re-view check box
If GetSetting("MWSDB", "Preferences", "StartUpDialog", True) Then
DoCmd.OpenForm "frmGreeting", , , , , acDialog
End If
End If

'Write information to the Registry to indicate usage for this user


lngProfileTimesOpened = GetSetting("MWSDB", "Statistics", "UsageCount", 1)
SaveSetting "MWSDB", "Statistics", "UsageCount", lngProfileTimesOpened + 1
SaveSetting "MWSDB", "Statistics", "LastUsed", Format$(Now(), "yyyy.mm.dd hh:nn:ss")

'And finally open the switchboard form


DoCmd.OpenForm "frmSwitchboard" ' This form would contain a cmdCompactDatabase control

Autoexec_Exit:
Exit Function

Autoexec_Err:
'Turn screen updating back on
Application.Echo True

'Now handle the error


Select Case Err.Number
Case Else
Call GlobalErr("Autoexec", Err.Number)
Resume Autoexec_Exit
Resume
End Select

End Function

Sub GlobalErr(strProcName As String, intErr As Integer)


'***************************************************************************
'Purpose: To display a message box providing details of a given error
'Parameters: strProcName - the name of the procedure calling GlobalErr
' intErr - an Error code
'Returns: Nothing
'***************************************************************************

Dim strMsg As String

strMsg = "The following error occurred in the " & strProcName & " procedure"
strMsg = strMsg & Chr$(10) & Chr$(10)
strMsg = strMsg & "Error Number: " & Format$(intErr) & Chr$(10)
strMsg = strMsg & "Error Description: " & Error$

MsgBox strMsg, 48, "Unexpected Error"

GlobalErr_Exit:
Exit Sub

End Sub

Sub MarkCompileTime()
'***************************************************************************
'Purpose: To set a db property indicating when the database was last
' programmatically compiled
'Parameters: None
'Returns: Nothing
'***************************************************************************

On Error GoTo MarkCompileTime_Err

Dim pty As Property

CurrentDb.Properties("LastCompiled") = Now

MarkCompileTime_Exit:
Exit Sub

MarkCompileTime_Err:
Select Case Err.Number
Case 3270 'Error code for "Property not found"
Set pty = CurrentDb.CreateProperty("LastCompiled", dbDate, Now())
CurrentDb.Properties.Append pty
Resume
Case Else
Call GlobalErr("MarkCompileTime", Err.Number)
Resume MarkCompileTime_Exit
Resume
End Select

End Sub

Sub IncrementTimesOpened()
'***************************************************************************
'Purpose: To set a db property indicating the number of times the
' database has been opened
'Parameters: None
'Returns: Nothing
'***************************************************************************

On Error GoTo IncrementTimesOpened_Err

Dim pty As Property


Dim lngDBTimesOpened As Long

lngDBTimesOpened = CurrentDb.Properties("TimesOpened")
CurrentDb.Properties("TimesOpened") = lngDBTimesOpened + 1
'Warn the user to re-compact every five opens
If lngDBTimesOpened Mod 5 = 0 Then
booCompactTime = True
End If

IncrementTimesOpened_Exit:
Exit Sub

IncrementTimesOpened_Err:
Select Case Err.Number
Case 3270 'Error code for "Property not found"
Set pty = CurrentDb.CreateProperty("TimesOpened", dbDate, 0)
CurrentDb.Properties.Append pty
Resume
Case Else
Call GlobalErr("IncrementTimesOpened", Err.Number)
Resume IncrementTimesOpened_Exit
Resume
End Select

End Sub
******************************************************************************************************
Excel chart creation thru automation
******************************************************************************************************
Private Sub cmdCreateSpreadsheet_Click()
Dim varArray as Variant
Dim recSales As Recordset ' Recorset to create chart from
Dim objExcel As New Excel.Application ' Excel object
DoCmd.Hourglass True ' Turn on the Hourglass -- to encourage wait by User
Set recSales = CurrentDb().OpenRecordset("qryMonthlySales") ' Creating recordset from QueryDef
recSales.MoveLast ' Move to the last record to update record count
recSales.MoveFirst ' Move to the first record
varArray = recSales.GetRows(recSales.RecordCount) ' Load the array with the query results
recSales.Close ' Close the recordset
objExcel.Workbooks.Add ' Add a new Excel workbook
' ****************************************************************************
For intFld = 0 To UBound(varArray, 1) ' Pass the values from the array into the Excel sheet
For intRow = 0 To UBound(varArray, 2)
objExcel.Cells(intRow + 2, intFld + 1).Value = varArray(intFld, intRow)
Next
Next
' *****************************************************************************
' Work on the data as desired using regular Excel commands -- simply recede them with
' objExcel as with objExcel.Range("A1:C3").Select
' *****************************************************************************
' As with modifying a Chart -- see this:
Set objChart = objExcel.ActiveChart
With objChart
.ChartType = xl3DArea
.HasTitle = True ' Add some titles
.ChartTitle.Text = "Monthly Sales"
.Axes(xlCategory).HasTitle = True
.Axes(xlCategory).AxisTitle.Caption = "Month"
.Axes(xlValue).HasTitle = True
.Axes(xlValue).AxisTitle.Caption = "Sales"
.Axes(xlSeriesAxis).HasTitle = True
.Axes(xlSeriesAxis).AxisTitle.Caption = "Year"
.HasLegend = False
End With
objExcel.ActiveWorkbook.Close True, "C:\BegVBA\MnthSale.XLS" ' Saving & closing Workbook
' Close Excel and free the memory
Set objChart = Nothing
Set objExcel = Nothing
DoCmd.Hourglass False
objExcel.Quit ' Leave the Excel application
Exit Sub

*** or a more complext example of inserting data and creating a chart therefrom ******
Public Sub CreateChart()
'
' Purpose: To create an Excel chart from a recordset
'
Dim recSales As Recordset ' Recorset to create chart from
Dim varArray As Variant ' Array of entries from above
Dim objExcel As New Excel.Application ' Excel object
Dim objChart As Object ' Excel chart object
Dim intFields As Integer ' Number of fields in recordset
Dim intRows As Integer ' Number of rows in recordset
Dim intFld As Integer ' Loop index for fields
Dim intRow As Integer ' Loop index for rows
Dim strRange As String ' Range of Excel cells for data

On Error GoTo CreateChart_Err

' Turn on the hourglass before opening the recordset


DoCmd.Hourglass True
Set recSales = CurrentDb().OpenRecordset("qryxMonthlySales")

' Move to the last record so we get an accurate record count


recSales.MoveLast

' Copy the whole recordset into an array, and close the recordset
recSales.MoveFirst
varArray = recSales.GetRows(recSales.RecordCount)

' Determine the number of rows and fields in the array


intFields = UBound(varArray, 1)
intRows = UBound(varArray, 2)

' create a new workbook


objExcel.Workbooks.Add

' fill the years and close the recordset


recSales.MoveFirst
For intFld = 1 To intFields
objExcel.Cells(intRow + 1, intFld + 1).Value = recSales.Fields(intFld).Name
Next
recSales.Close

' Pass the values from the array into the Excel sheet
For intFld = 0 To intFields
For intRow = 0 To intRows
objExcel.Cells(intRow + 2, intFld + 1).Value = varArray(intFld, intRow)
Next
Next

' Determine the A1:C2-type reference for the range containing our data
strRange = "A1:" & Chr$(Asc("A") + intFields) & Format$(intRows + 2)

' Select the range in the Excel sheet and make it active
objExcel.Range(strRange).Select
objExcel.Range(Mid(strRange, 4)).Activate

' Insert a chart based on the active selection


objExcel.Application.Charts.Add

' Add some titles


Set objChart = objExcel.ActiveChart
With objChart
.ChartType = xl3DArea
.HasTitle = True
.ChartTitle.Text = "Monthly Sales"
.Axes(xlCategory).HasTitle = True
.Axes(xlCategory).AxisTitle.Caption = "Month"
.Axes(xlValue).HasTitle = True
.Axes(xlValue).AxisTitle.Caption = "Sales"
.Axes(xlSeriesAxis).HasTitle = True
.Axes(xlSeriesAxis).AxisTitle.Caption = "Year"
.HasLegend = False
End With

objExcel.ActiveWorkbook.Close True, "C:\BegVBA\MnthSale.XLS"

' Close Excel and free the memory


Set objChart = Nothing
Set objExcel = Nothing
DoCmd.Hourglass False

CreateChart_Exit:
Exit Sub

CreateChart_Err:
' Tidy up - ensure all objects are cleared
Set objChart = Nothing
objExcel.ActiveWorkbook.Close False
objExcel.Quit
Set objExcel = Nothing
DoCmd.Hourglass False
MsgBox Err.Number & " - " & Err.Description
Resume CreateChart_Exit

End Sub
******************************************************************************************************
Security (show "no delete" permissions)
******************************************************************************************************
Sub ShowNoDelPerms()

Dim objContainer As Container


Dim objDoc As Document

For Each objContainer In CurrentDb.Containers


Debug.Print "--> Container: " & objContainer.Name
For Each objDoc In objContainer.Documents

If objDoc.AllPermissions And dbSecDelete Then


Debug.Print "Can Delete Document: " & _
objDoc.Name & " ";
objDoc.UserName = "Admin"
Debug.Print "Perms: " & objDoc.AllPermissions
Else
Debug.Print "Cannot Delete Document: " & _
objDoc.Name & " ";
objDoc.UserName = "Admin"
Debug.Print "Perms: " & objDoc.AllPermissions
End If

Next
Next

Debug.Print "Done"

End Sub
******************************************************************************************************
Security (listing permissions)
******************************************************************************************************
Sub ShowPerms()

Dim objContainer As Container


Dim objDoc As Document

For Each objContainer In CurrentDb.Containers


Debug.Print "--> Container: " & objContainer.Name
For Each objDoc In objContainer.Documents

Debug.Print "Document: " & objDoc.Name & " ";


objDoc.UserName = "Admin"
Debug.Print "Perms: " & objDoc.AllPermissions

Next
Next

Debug.Print "Done"

End Sub
******************************************************************************************************
Security (adding User to Group)
******************************************************************************************************
Sub AddUserToGroup()

Dim wks As Workspace


Dim usrMark As User
Dim grpRegistrars As Group

Set wks = DBEngine(0)


Set usrMark = wks.CreateUser("Mark Fenton")

wks.Groups("Registrars").Users.Append usrMark

End Sub
**** Alternative example **************

'Creates a new user and appends it to the user collecton.


'Appends that user to the Users and Managers groups
Sub CreateNewUser()
Dim ws As Workspace
Dim newUsr As User

Set ws = DBEngine.Workspaces(0)

Set newUsr = ws.CreateUser("A_Manager", "1234", "myPassword")


ws.Users.Append newUsr

With newUsr.Groups
.Append ws.CreateGroup("Users")
.Append ws.CreateGroup("Managers")
.Refresh
End With
End Sub
******************************************************************************************************
Security (adding a Group Account)
******************************************************************************************************
Sub GroupAdd()
Dim wks As Workspace
Dim grpRegistrars As Group
Dim strGroupPID As String

Set wks = DBEngine(0)


strGroupPID = "5678"

'Start by creating a new Group account


Set grpRegistrars = wks.CreateGroup("Registrars")

'Now set the Group's properties


grpRegistrars.PID = strGroupPID

'Append Group to the Groups collection of this workspace


wks.Groups.Append grpRegistrars

End Sub
******** Alternative example ***********

'Create a new group called Managers


Sub CreateNewGroup()
Dim ws As Workspace
Dim newGrp As Group

Set ws = DBEngine.Workspaces(0)

Set newGrp = ws.CreateGroup("Managers", "1234")


With ws.Groups
.Append newGrp
.Refresh
End With
End Sub
******************************************************************************************************
Security (adding a new User Account)
******************************************************************************************************
Sub UserAdd()

Dim wks As Workspace


Dim usrMark As User
Dim strUsersPID As String

Set wks = DBEngine(0)


strUsersPID = "1234abcd"

'Start by creating a new User account


Set usrMark = wks.CreateUser("Mark Fenton")

'Now set the User's properties


usrMark.Password = "Doctor"
usrMark.PID = strUsersPID

'Append User to the Users collection of this workspace


wks.Users.Append usrMark

End Sub
******************************************************************************************************
Record locking types and method
******************************************************************************************************
NOTE: We're passing the subroutine below the description of the type of locking desired
Sub SetAndGetLocking(strLockType As String)

Dim intLockIndex As Integer


Dim strLockDesc As String

'Convert the string argument into a option index


Select Case strLockType
Case "Optimistic"
intLockIndex = 0
Case "Exclusive"
intLockIndex = 1
Case "Pessimistic"
intLockIndex = 2
Case Else
intLockIndex = -1
End Select

'Set default record locking option


If intLockIndex <> -1 Then
Application.SetOption "Default Record Locking", intLockIndex
End If

'Now determine and display default record locking option


Select Case Application.GetOption("Default Record Locking")
Case 0
MsgBox "The default locking method is optimistic."
Case 1
MsgBox "The default locking method is exclusive."
Case 2
MsgBox "The default locking method is pessimistic."
End Select

End Sub
******************************************************************************************************
Progress meter in status bar
******************************************************************************************************
Sub ShowProgress()
'
' Purpose: To display a progress bar in the status bar
'

Dim varRetVal As Variant


Dim i As Integer
Dim j As Integer
Dim intRnd As Integer

'Initialise Progress Meter and set Maximum to 300


varRetVal = SysCmd(acSysCmdInitMeter, "Testing...", 300)

For i = 0 To 300
'Perform some processing or other...
For j = 0 To 1000
intRnd = Rnd * 10 + 1
Next j

'Update Meter to Show Progress


varRetVal = SysCmd(acSysCmdUpdateMeter, i)

Next i

'Remove Meter from Status Bar


varRetVal = SysCmd(acSysCmdRemoveMeter)

End Sub
******************************************************************************************************
Error Table Creation
******************************************************************************************************
Function AccessAndJetErrorsTable() As Boolean
'
' Purpose: Create a table and populate it with Access errors
'

Dim dbC As Database ' current database


Dim tblErr As TableDef ' table to create
Dim fldErr As Field ' field to create
Dim recErr As Recordset ' recordset of errors
Dim lngCode As Long ' error code
Dim strAccessErr As String ' error description
Const conAppObjectError = "Application-defined or object-defined error"

On Error GoTo AccessAndJetErrorsTable_Err


' Create Errors table with ErrorNumber and ErrorDescription fields.
Set dbC = CurrentDb
Set tblErr = dbC.CreateTableDef("AccessAndJetErrors")
Set fldErr = tblErr.CreateField("ErrorCode", dbLong)
tblErr.Fields.Append fldErr

Set fldErr = tblErr.CreateField("ErrorString", dbMemo)


tblErr.Fields.Append fldErr

dbC.TableDefs.Append tblErr

' Open recordset on Errors table.


Set recErr = dbC.OpenRecordset("AccessAndJetErrors")

' Loop through error codes.


For lngCode = 0 To 3500
On Error Resume Next
' Raise each error.
strAccessErr = AccessError(lngCode)
DoCmd.Hourglass True

' Skip error numbers without associated strings.


If strAccessErr <> "" Then

' Skip codes that generate application or object-defined errors.


If strAccessErr <> conAppObjectError Then
' Add each error code and string to Errors table.
recErr.AddNew
recErr!ErrorCode = lngCode
' Append string to memo field.
recErr!ErrorString.AppendChunk strAccessErr
recErr.Update
End If
End If
Next lngCode

' Close recordset.


recErr.Close
DoCmd.Hourglass False
RefreshDatabaseWindow
MsgBox "Access and Jet errors table created."

AccessAndJetErrorsTable = True

AccessAndJetErrorsTable_Exit:
Exit Function

AccessAndJetErrorsTable_Err:
MsgBox Err & ": " & Err.Description
AccessAndJetErrorsTable = False
Resume AccessAndJetErrorsTable_Exit

End Function
******************************************************************************************************
Field (listing single col wi Recordset)
******************************************************************************************************
Public Sub ErrorHelp(strColumn As String)
'
' Purpose: Demonstrate a naff error message, but useful help
' when searching a recordset for a column
' Arguments: strColumn The column to search for
'

Dim dbCurrent As Database ' The current database


Dim recBottling As Recordset ' Snapshot on bottling table
Dim strSQL As String ' SQL string

' build a SQL string and open the recordset


strSQL = "SELECT " & strColumn & " FROM Bottling"
Set dbCurrent = CurrentDb()
Set recBottling = dbCurrent.OpenRecordset(strSQL, dbOpenSnapshot)

' run through the recordset printing out the column


While Not recBottling.EOF
Debug.Print recBottling(strColumn)
recBottling.MoveNext
Wend
recBottling.Close

End Sub
******************************************************************************************************
Records Found With SQL into Array
******************************************************************************************************
Private Sub GetWhiskies(strRegion As String, Optional bolPrice As Boolean)
'
' Purpose: To show the whiskies for a region
'

Dim dbC As Database ' current database


Dim recR As Recordset ' recordset of whiskies and prices
Dim intC As Integer ' current whisky

Set dbC = CurrentDb


strSQL = "SELECT * FROM qryWhiskyAndPrices WHERE RegionName = """ & strRegion & """"
Set recR = dbC.OpenRecordset(strSQL)

' loop through the whiskies


While Not recR.EOF
' add another element to the array
ReDim Preserve mastrWhiskies(intC) ' We start with the initial value of zero

' now set the values


With mastrWhiskies(intC)
.strName = recR("WhiskyName")
.intAge = Nz(recR("Age"))
If bolPrice = True Then
.dblPrice = Nz(recR("Price"))
End If
End With
recR.MoveNext
intC = UBound(mastrWhiskies) + 1
Wend

recR.Close

End Sub
******** To display the array we just created use ****************

Private Sub DisplayWhiskies()


'
' Purpose: To display the whiskies stored in the global array
'
Dim intW As Integer ' current index

For intW = 0 To UBound(mastrWhiskies)


With mastrWhiskies(intW)
Debug.Print .strName
Debug.Print .intAge
Debug.Print .dblPrice
End With
Next

End Sub

******************************************************************************************************
Properties of a Database (determining)
******************************************************************************************************
Sub EnumDBProperties()
'
' Purpose: Enumerate the database properties
'
Dim pty As Property
On Error Resume Next
For Each pty In CurrentDb.Properties
Debug.Print pty.Name & ": " & pty.Value
Next
End Sub
******** Sample output shown below ** Note the Property name is shown preceding the quote *** highlighted important ones
Name: C:\TEMP\WHISKY2000.MDB
Connect:
Transactions: True
Updatable: True
CollatingOrder: 1033
QueryTimeout: 60
Version: 4.0
RecordsAffected: 0
ReplicaID:
DesignMasterID:
AccessVersion: 08.50
Build: 3822
AllowBypassKey: True
Show Values Limit: 1000
Show Values in Indexed: 1
Show Values in Non-Indexed: 1
Show Values in Remote: 0
AppTitle: The Malt Whisky Store
StartUpShowDBWindow: True
StartUpShowStatusBar: True
AllowShortcutMenus: True
AllowFullMenus: True
AllowBuiltInToolbars: True
AllowToolbarChanges: True
AllowBreakIntoCode: True
AllowSpecialKeys: True
LastCompiled: 8/19/1997 9:05:04 AM
TimesOpened: 1/23/1900
Track Name AutoCorrect Info: 0
Perform Name AutoCorrect: 0
CpgConversion: 1252
******************************************************************************************************
Recordset (Rows & Fields) into Array
******************************************************************************************************
Sub TestGetRows()
'
' Purpose: Demonstrate the use of the GetRows function to read
' values from a recordset into an array
Dim varValues As Variant
Dim rs As Recordset
Dim varRow As Variant
Dim intRowCount As Integer
Dim intFieldCount As Integer
Dim i As Integer
Dim j As Integer

Set rs = CurrentDb().OpenRecordset("Order")
varValues = rs.GetRows(2)
rs.Close

intFieldCount = UBound(varValues, 1)
intRowCount = UBound(varValues, 2)

For j = 0 To intRowCount
For i = 0 To intFieldCount
Debug.Print "Row " & j & ", Field " & i & ": ";
Debug.Print varValues(i, j)
Next
Next

End Sub
******************************************************************************************************
DLookup() function -- pulling data from a table or query
******************************************************************************************************
Using the DLookup() function you can display data obtained directly from a table or query. You can use the DLookup() function as
follows:
In a query In a calculated field expression in a Field cell, to specify criteria in a Criteria cell, or in an expression in the Update
To cell in an update query.
In a VBA procedure In a condition or method argument.
In a form or report In a calculated control.

The DLookup() function takes 3 arguments: DLookup("fieldname", "tablename" or "queryname", "searchcondition")
Note: All three arguments must be expressed as strings.
Syntax for search condition is:
fieldname = Forms!formname!controlname
Where the left side of the search condition is the name of the field in the table or query you're searching and the right side is the fully
qualified reference to the form control with the value you're searching for.

Example:
=DLookup("Description", "Categories", "CategoryID = Forms!Products!CategoryID")
******************************************************************************************************
ControlSource data from Another Form
******************************************************************************************************
You can pull data into an unbound control from controls on another open form or from fields in another form's data source by setting the
control's ControlSource property to an expression (fully-qualified) that refers to these controls or fields.
Example: An unbound control on the Orders form obtaining data from the Employees form:
=Forms!Employees!LastName & ", " & Forms!Employees!FirstName

*** More Examples Of ControlSource Expressions Of Referring To Other Open Forms **

=Forms![Customer Orders]!CustomerID ' Displays the value of the CustomerID field in the data source of the Customer Orders form.
=Forms![Customer Orders]![Customer Orders Subform1]!OrderDate
' Displays the value of the OrderDate control of the current record in the Customer Orders
Subform1 subform of the Customer Orders form.
=[Orders Subform]!UnitPrice ' Displays the value fo the UnitPrice control of the current record in the Orders Subform of the
active form. The calculated control is located on the main form.
=Parent!OrderID ' Displays the value of the OrderID control on the parent form of the current subform. The
calculated control is located on the subform.

*** Can also set the control source referring to two separate open forms in an expression **

="The Customers current record is " & Screen.ActiveForm.CurrentRecord & " and the Employees current record is " & Forms!
Employees.CurrentRecord

******************************************************************************************************
Calculated Controls (some ControlSource expressions) -- get data from same form
******************************************************************************************************
=FirstName & " " & LastName ' Displays the values in the FirstName and LastName controls separated by a space
=Left(CompanyName, 4) ' Displays the first four characters of the value of the CompanyName control.
=Sum(Quantity*Price) ' Displays the sum of the product of the values of the Quantity and Price fields for all the records
displayed on the form.
=IIf(IsNull(Sum(Quantity*Price),0,Sum(Quantity*Price))
' Displays a zero if the sum is Null; otherwise, displays the sum.
=Count(EmployeeID) ' Displays the number of records displayed by the form that have a non-Null value in the EmployeeID
field.
=Count(*) ' Displays the number of records displayed by the form. Use the asterisk(*) to count all of the records.

NOTE: If you are working interactively, you can force the update of calculated controls by pressing F9. Otherwise, you can write a
program to do so keyed to some event (Exit, LostFocus, GotFocus, etc.)

******************************************************************************************************
Variables & Procedures (declarations, scope, passing info examples)
******************************************************************************************************
Option Compare Database
Option Explicit
Private strPrivate As String
Public gstrPublic As String
Public strModule As String
Public Sub LocalVariable()
Dim strLocal As String
strLocal = "'Local variable in LocalVariable procedure'"
MsgBox strLocal
Call GetLocal(strLocal) ' See below
End Sub
Public Sub GetLocal(strA As String)
MsgBox strA & " passed as an argument to GetLocal"
End Sub

Public Sub StaticVariable()


Dim strNonStatic As String
Static sstrStatic As String
strNonStatic = strNonStatic & " nonstatic"
sstrStatic = sstrStatic & " static"
Debug.Print strNonStatic
Debug.Print sstrStatic
End Sub

Public Static Sub StaticProcedure()


Dim strNonStatic As String
Static sstrStatic As String
strNonStatic = strNonStatic & " nonstatic"
sstrStatic = sstrStatic & " static"
Debug.Print strNonStatic
Debug.Print sstrStatic
End Sub

Public Sub PassingLiteral()


MsgBox "Literal"
Call GetData("'Literal'") ' See below
MsgBox "Returning from the GetData procedure"
End Sub
Public Sub GetData(A As String)
MsgBox A & " is passed to the GetData procedure as an argument."
End Sub

Public Sub PassingVariableByRef()


Dim strVariable As String
strVariable = "'Variable to be passed to another procedure'"
MsgBox strVariable
Call GetVariableByRef(strVariable) ' See below
MsgBox strVariable
End Sub
Public Sub GetVariableByRef(ByRef strA As String)
MsgBox strA & " is passed as an argument of the procedure."
strA = "Variable received. Thank you"
End Sub

Public Sub PassingVariableByVal()


Dim strVariable As String
strVariable = "'Variable to be passed by value to another procedure'"
MsgBox strVariable
Call GetVariableByVal(strVariable) ' See below
MsgBox strVariable
End Sub
Public Sub GetVariableByVal(ByVal strA As String)
MsgBox strA & " is passed by value as an argument of the procedure."
strA = "Variable received. Thank you"
End Sub

Public Sub GetMultiply()


Dim intX As Integer, sngY As Single
Dim dtmX As Date
Dim strX As String, strY As String
intX = 2
sngY = 3
dtmX = #5/11/2001#
strX = "2"
strY = "Three"
MsgBox "Numbers of different data type " & Multiply(intX, sngY) ' See below
MsgBox "Number and date " & Multiply(dtmX, sngY)
MsgBox "Number and convertible string " & Multiply(strX, sngY)
MsgBox "Number and non-convertible string " & Multiply(intX, strY)
End Sub
Public Function Multiply(X, Y)
Multiply = X * Y
End Function

Public Function ChangeCaption(objectname As Object)


objectname.Caption = "My Caption"
End Function

Public Function Product(X, Y, Optional Z)


Product = X * Y
If IsMissing(Z) Then Exit Function ' We'd leave at this point instead of proceeding further
Product = Product * Z
End Function

Public Sub PublicScoping()


gstrPublic = "'Public'"
Call SamePublic
Call OtherPublic ' Don't see this one anywhere??
End Sub
Public Sub SamePublic()
MsgBox gstrPublic & " from the same module.", , "SamePublic"
End Sub

Public Sub PrivateScoping()


strPrivate = "'Private'"
Call SamePrivate
Call OtherPrivate
End Sub

Public Sub SamePrivate()


MsgBox strPrivate & " from the same module.", , "SamePrivate"
End Sub

Public Sub LifeTimeModule()


strModule = "'Module-level variable exists until you close the database.'"
MsgBox strModule, , "LifeTimeModule"
End Sub
Public Sub StillThere()
MsgBox strModule, , "StillThere"
End Sub
******************************************************************************************************
HTML Output (example)
******************************************************************************************************
Private Sub OutputToHTML_Click()
On Error GoTo Err_OuputToHTML_Click
' Outputs the Alphabetical List of Products report as an HTML
' document, and opens the document in an Internet browser.
' You must have Nwindtem.htm (template for Northwind) and NWLogo.gif
' (Northwind logo) in your default database folder.

DoCmd.OutputTo acOutputReport, "Alphabetical List of Products", acFormatHTML, "Products.htm", True, "Nwindtem.htm"

Exit_OutputToHTML_Click:
Exit Sub

Err_OuputToHTML_Click:
' If action was cancelled by the user, don't display an error message.
Const conErrDoCmdCancelled = 2501
If (Err = conErrDoCmdCancelled) Then
Resume Exit_OutputToHTML_Click
Else
MsgBox Err.Description
Resume Exit_OutputToHTML_Click
End If

End Sub
******************************************************************************************************
Creating a new database
******************************************************************************************************
Private Sub cmdNewCatalog_Click()
Dim newdb As ADOX.Catalog
Set newdb = New ADOX.Catalog
newdb.Create "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\VBAHandbook\MyNewDB.mdb"
End Sub
******************************************************************************************************
Focus (giving a control focus)
******************************************************************************************************
' Give ProductName control focus.
Forms![Products]!ProductName.SetFocus
******************************************************************************************************
Closing a form
******************************************************************************************************
' Close Product List form.
DoCmd.Close acForm, "Product List"
' Close form (simpler form -- when inside the form)
DoCmd.Close
******************************************************************************************************
Opening form (in data entry mode)
******************************************************************************************************
' Open Products form in data entry mode and store SupplierID in the form's OpenArgs property.
DoCmd.OpenForm strDocName, , , , acAdd, , Me!SupplierID
' Open form (simply to view data)
DoCmd.OpenForm strDocName, , , , , acDialog
******************************************************************************************************
Printing Reports example
******************************************************************************************************
Sub PrintReports(PrintMode As Integer)
On Error GoTo Err_Preview_Click
' This procedure used in Preview_Click and Print_Click Sub procedures.
' Preview or print report selected in the ReportToPrint option group.
' Then close the Print Sales Reports Dialog form.
Dim strWhereCategory As String
strWhereCategory = "CategoryName = Forms![Sales Reports Dialog]!SelectCategory"
Select Case Me!ReportToPrint
Case 1
DoCmd.OpenReport "Employee Sales by Country", PrintMode
Case 2
DoCmd.OpenReport "Sales Totals by Amount", PrintMode
Case 3
If IsNull(Forms![Sales Reports Dialog]!SelectCategory) Then
DoCmd.OpenReport "Sales by Category", PrintMode
Else
DoCmd.OpenReport "Sales by Category", PrintMode, , strWhereCategory
End If
End Select
DoCmd.Close acForm, "Sales Reports Dialog"
Exit_Preview_Click:
Exit Sub
Err_Preview_Click:
Resume Exit_Preview_Click
End Sub
******************************************************************************************************
File Open dialog (for choosing file) -- example
******************************************************************************************************
Sub getFileName()
' Displays the Office File Open dialog to choose a file name
' for the current employee record. If the user selects a file
' display it in the image control.
Dim fileName As String
Dim result As Integer
With Application.FileDialog(msoFileDialogFilePicker)
.Title = "Select Employee Picture"
.Filters.Add "All Files", "*.*"
.Filters.Add "JPEGs", "*.jpg"
.Filters.Add "Bitmaps", "*.bmp"
.FilterIndex = 3
.AllowMultiSelect = False
.InitialFileName = CurrentProject.path
result = .Show
If (result <> 0) Then
fileName = Trim(.SelectedItems.Item(1))
Me![ImagePath].Visible = True
Me![ImagePath].SetFocus
Me![ImagePath].Text = fileName
Me![FirstName].SetFocus
Me![ImagePath].Visible = False
End If
End With
End Sub
******************************************************************************************************
Opening & Closing a Form
******************************************************************************************************
Private Sub OpenChildForm()
DoCmd.OpenForm "OrdersLinked"
If Not Me.[ToggleLink] Then Me![ToggleLink] = True ' See the ToggleLink routine below
End Sub

Private Sub CloseChildForm()


DoCmd.Close acForm, "OrdersLinked"
If Me![ToggleLink] Then Me![ToggleLink] = False' See ToggleLink routine below
End Sub

Sub ToggleLink_Click()
On Error GoTo ToggleLink_Click_Err
If ChildFormIsOpen() Then
CloseChildForm
Else
OpenChildForm
FilterChildForm
End If
ToggleLink_Click_Exit:
Exit Sub
ToggleLink_Click_Err:
MsgBox Error$
Resume ToggleLink_Click_Exit
End Sub
******************************************************************************************************
Form Open? (True or False)
******************************************************************************************************
Private Function ChildFormIsOpen()
ChildFormIsOpen = (SysCmd(acSysCmdGetObjectState, acForm, "OrdersLinked") And acObjStateOpen) <> False
End Function
******************************************************************************************************
Me object
******************************************************************************************************
In procedures stored in a form (or report) module use the "Me" property to refer to the form or report itself.
From within the form you could execute the statement:
Me.Caption = "My Favorite Form" (the caption property of the form would be changed, temporarily, to "My Favorite Form")
Also you can get the form's RecordSource property and store it in the strDataSource variable using this statement:
strDataSource = Me.RecordSource
You can update the form's data source by calling its Requery method as follows:
Me.Requery
NOTE: You can't use test statements that include Me object in the immediate window only in a procedure stored in a form or a
report module.
ALSO: The Me object is preferable to the ActiveForm or ActiveReport property, since you have no doubt as to what Me refers to;
whereas, one cannot always count on a form or report being the "Active" object of it's type.
******************************************************************************************************
Bookmarks
******************************************************************************************************
To track records in a recordset, Access creates bookmarks. A bookmark is a unique binary string (you cannot read it or alter it in any way;
it's also not the same as the record number) that Access creates for each record in a form's recordset each time you open a form.
The value of the form's bookmark property is the value of the bookmark of the current record. You can:
a) Store the bookmark in a string variable.
b) Return to the bookmark by setting the form's bookmark property to the previously stored variable.
strMark = Forms!Customers.Bookmark (stores the current record bookmark in the string "strMark")
Forms!Customers.Bookmark = strMark (to return to the previously stored bookmark)
NOTE: Bookmarks are the fastest way to return to a record. Bookmarks are not saved with the records; when you close the form
the recordset and associated bookmarks cease to exist.
NOTE: While each recordset has it's own set of bookmarks each time the recordset is created a clone will have the same
bookmarks as the recordset from which the clone is derived; therefore, a bookmark obtained from the clone can be used to access the
recordset.
strMark = Forms!Customers.RecordsetClone.Bookmark (stores the bookmark from the current record of clone)
Forms!Customers.Bookmark = strMark (goes to that record in the recordset)
******************************************************************************************************
Clones
******************************************************************************************************
You can create a Recordsetclone object (basically a copy of the Recordset in memory which can be manipulated separately from the
primary Recordset) for the Customers form using the syntax:
Set clone = Forms!Customers.RecordsetClone
You can navigate independently in the Recordset and the Recordsetclone objects each without affecting the other.
NOTE: Some properties and methods are available in one and not the other.
******************************************************************************************************
Variables
******************************************************************************************************
When a property setting refers to an object, you can assign the object to a variable by using a statement similar to:
Set frm = Screen.ActiveForm
******************************************************************************************************
Data-Conversion functions (i.e., converts string or numeric expression)
******************************************************************************************************
CBool Boolean. If expression is zero, False is returned; otherwise, True is returned.
CByte Byte
CCur Currency
CDate Date
CDbl Double
CDec Decimal
CInt Integer
CLng Long
CSng Single
CStr String
CVar Variant
CVDate Variant of subtype Date
CVErr Variant of subtype Error
Fix Returns the integer portion of a number; rounds negative numbers up.
Hex Variant (hexadecimal)
Int Returns the integer portion of a number; rounds negative numbers down.
Oct Variant (octal)
Str Variant (string)
Val Converts a string of number characters into a number of appropriate type.
******************************************************************************************************
Control Source properties (Access)
******************************************************************************************************
As an alternative to using a calculated query field, you can use a calculated form control. A calculated control is a control that displays the
result of an expression. You enter the expression preceded by an equal sign (=) in the control's ControlSource property.
Examples:
=FirstName & " " & LastName (Displays the values in the FirstName and LastName controls separated by a space).
=Left(CompanyName, 4) (Displays the first four characters of the value of the CompanyName control).
=Sum(Quantity*Price) (Displays the sum of the product of Quantity and Price fields for all the records displayed by the form).
=IIf(IsNull(Sum(Quantity*Price),0,Sum(Quantity*Price))
(Displays a zero if the sum is Null; otherwise, displays the sum the IIf function is an in-line if-then-
else).
=Count(EmployeeID) (Displays the number of records displayed by the form that have a non-Null value in the EmployeeID
field).
*** Can also refer to another open form ***
=Forms![Customer Orders]!CustomerID
(Displays the value of the CustomerID field in the data source of the Customer Orders form).
=Forms![Customer Orders]![Customer Orders Subform1]!OrderDate
(Displays the value of the OrderDate control of the current record in the Customer Orders Subform1
subform of the Customer Orders form).
=[Orders Subform]!UnitPrice (Displays the value of the UnitPrice control of the current record in the Orders Subform of the active
form. The calculated control is located on the main form).
=Parent!OrderID (Displays the value of the OrderID control on the parent form of the current subform. The calculated
control is located on the subform).
="The Customers current record is " & Screen.ActiveForm.CurrentRecord & " and the Employees current record is " & Forms!
Employees.CurrentRecord
(Displays the current record numbers of the two open forms)

Domain Aggregate Functions


*** Sometimes you want a form to look up information that is stored in a table or query that is not the form's underlying
record source can use the DLookup() function in the ControlSource property ***
Dlookup() takes 3 arguments as follows: Dlookup("fieldname", "tablename" or "queryname", "searchcondition"). All 3 arguments
must be expressed as strings. If the search condition returns more than one record, the DLookup() function returns the value of the field in
the first record that satisfies the condition. .
Example:
=DLookup("Description", "Categories", "CategoryID = Forms!Products!CategoryID")
In this expression, the search condition tells Access to select the record whose CategoryID field matches
the value in the form's CategoryID control. The syntax for the search condition is as follows:
fieldname = Forms!formname!controlname
where the left side of the search condition is the name of the field in the table or query you are searching,
and the right side is the fully qualified reference to the form control with the value you are searching for.
Dlookup() is an example of a domain aggregate function a built-in Access function that you can use to perform calculations based on the
values in a field of a table or query. The table or query is the domain.

You can use any of the domain aggregate functions (shown below) in the ControlSource expression. All of them use the same syntax. [Note:
The third argument (i.e., setting up a restricted domain with a search condition) is optional; if you don't specify a search condition, the
function uses the larger set of records (the domain). If no record satisfies the search condition, or if the domain contains no records, the
domain aggregate function returns a Null.

DLookup() (Returns the value in the specified field)


DMin(), DMax() (Returns the minimum or maximum value in the specified field)
DFirst(), DLast() (Returns the value in the specified field from the first or last physical record)
DAvg() (Returns the arithmetical average of the values in the specified field)
DSum() (Returns the sum of the values in the specified field)
DStDev(), DStDevP()
(Returns the standard deviation or population standard deviation for the specified field)
DVar(), DVarP() (Returns the variance or population variance for the specified field)
DCount() (Returns the number of records with non-Null values in the specified field)

SQL Aggregate Functions


There are also SQL aggregate functions, which operate similarly to domain aggregate functions (which, incidentally, are analyzed and run by
Access as SQL statements making them a little slower). However, unlike domain aggregate functions, SQL aggregate functions cannot be
called directly from Visual Basic. Because they use SQL statements instead of Visual Basic, SQL aggregate functions (shown below) are
usually more efficient than domain aggregate functions.

Avg (Returns the average of a set of values in a field or query)


Count (Returns the number of records returned by a query)
First, Last (Return a field value from the first or last record in the results of a query)
Min, Max (Return the minimum or maximum of a field's or query's value set)
StDev, StDevP (Return estimates of standard deviation from a sample)
Sum (Returns the sum of a set)
Var, VarP (Return estimates of variance (the square of the standard deviation))

******************************************************************************************************
SQL Statement (Full Syntax with explanation -- click on the Hyperlinks below to go to appropriate subheading)
******************************************************************************************************
SELECT [ALL | DISTINCT] select_list (the fields or columns which we're looking for)
FROM table/view_list (the tables we're selecting from or virtual tables creating by using Joins of various types)
[WHERE search_conditions] (optional criteria specifying what records or rows we which to select)
[GROUP BY group_by_list] (optional grouping criteria need for summary totals at whatever levels)
[HAVING search_conditions] (optional WHERE clause for groups just as WHERE limits rows, HAVING limits groups)
[ORDER BY order_by_list] (optional sort specification)

-- explained in greater detail below --


SELECT * (selects all of the columns in a table)
SELECT TOP 1000 * (allows you to limit the number of records returned via the SQL Statement)
SELECT lastname, firstname, ssn (selects particular columns from the table(s))
SELECT lastname AS Last, firstname AS First, SSN as "Social Security Number" (specifying different column headings)
SELECT tblEmployees.lastname, tblEmployees.firstname (can also specify the table must do so if two tables have similar columns)
SELECT editor1_id as editor, units1 as units, hours1 as hours, (units1 * hours1) as 'Unit hrs' from PubReview WHERE editor1_id = '250503'
' Note the arithmetic computation in the above SQL statement (units1 * hours1)
SELECT *, (units * hours) as UnitHrs FROM NewPubReview ' Slight variation -- including computation with selecting all fields

The DISTINCT and ALL keywords in the SELECT list let you specify what to do with duplicate rows in your results. ALL returns all
qualified rows and is the default. DISTINCT returns only unique rows.

DISTINCT (or ALL) has some stringent requirements:


Use it only once in a SELECT list.
Make it the first word in the SELECT list.
Do not put a comma after it.

Example:
SELECT DISTINCT lastname, firstname, ssn

** More Select Examples (also with LIKE for limited pattern-matching) *****
The LIKE pattern matching operator can also be used in the conditional selection of the where clause. Like is a very powerful operator that
allows you to select only rows that are "like" what you specify. The percent sign "%" can be used as a wild card to match any possible
character that might appear before or after the characters specified. For example:

SELECT first, last, city FROM empinfo WHERE first LIKE 'Er%';
This SQL statement will match any first names that start with 'Er'. Strings must be in
single quotes.
SELECT first, last FROM empinfo WHERE last LIKE '%s';
This statement will match any last names that end in a 's'.
SELECT * FROM empinfo WHERE first = 'Eric';
This will only select rows where the first name equals 'Eric' exactly.
SELECT first, last, city FROM empinfo;
SELECT last, city, age FROM empinfo WHERE age > 30;
SELECT first, last, city, state FROM empinfo WHERE first LIKE 'J%';
SELECT * FROM empinfo;
SELECT first, last, FROM empinfo WHERE last LIKE '%s';
SELECT first, last, age FROM empinfo WHERE last LIKE '%illia%';
SELECT * FROM empinfo WHERE first = 'Eric';

SELECT name
FROM actor, casting
WHERE casting.movieid='1' and casting.actorid = actor.id ' joining two tables together
SELECT title, name
FROM actor, casting, movie
WHERE movie.title = 'Aliens' and movie.id = casting.movieid and
casting.actorid = actor.id ' joining three tables together!!
' Can JOIN multiple SELECT statements by using UNION command to send data to a single field (as with Publishability Review!)

SELECT Editor1_id as Editor, Units1 as Units, Hours1 as Hours


FROM PubReview
WHERE Editor1_id = '250503'
UNION ALL ' The ALL keyword keeps duplicates from being deleted in the output (otherwise default)
SELECT Editor2_id, Units2, Hours2
FROM PubReview
WHERE Editor2_id = '250503'
UNION ALL
SELECT Editor3_id, Units3, Hours3
FROM PubReview
WHERE Editor3_id = '250503'
ORDER BY 3 ' Since columns are different names -- put ORDER BY last & use a number
FROM tblEmployees (the table from which the columns are being selected)

Select emp_lname as "Last Name", emp_fname as "First Name", DBA.employee.dept_id as "Dept. #", dept_name as "Department"
FROM DBA.employee, DBA.department
WHERE DBA.employee.dept_id = DBA.department.dept_id (joining two tables to create a relationship between them)
ORDER BY emp_lname
Note: In the above select example lastname & firstname came from the DBA.employee table while the department name came from the
DBA.department table -- the two tables were joined on dept_id field to create a relationship.
Also Note: Since both table contained the same field (dept_id) when I selected columns in the above example, I had to qualify the dept_id
field with the table name to show which one I was after even though they were both the same.

FROM tblEmployees Emp (gives the tblEmployees the alias of "Emp" which can be used thereafter)
WHERE advance * 2 > ytd_sales * price (can use arithmetic & comparison operations +, -, *, /, =, <, >, <>, !=, <=, >=, and so on)
WHERE advance < 5000 or ytd_sales > 2000 (combinations or logical negations of conditions (AND, OR, NOT)
WHERE lastname > 'McBadden' (whose last names follow McBadden in the alphabet; note -- depends on character-sorting sequence of
DBMS)
WHERE ytd_sales between 4095 and 12000 (between or not between can also be used -- the upper and lower values of 12000 and 4095 are
considered valid)
WHERE state in ('CA', 'IN', 'MD') (in or not in a specified list) this is equivalent to
WHERE state = 'CA' or state = 'IN' or state = 'MD'
the requirements are:
inside parentheses
separated by commas
enclosed in quotes, if they are character or date values
NOTE: Also important use of IN keyword is in nested queries, referred to as subqueries.
WHERE phone NOT LIKE '415%' (character matches like or not like as with limited pattern-matching: the percent % stands for zero or
more characters and the underscore _ stands for any single character. You use the ESCAPE keyword when your pattern includes one of these
wildcard characters to be treated literally).
WHERE notes like '%@%%' escape '@' (here we're looking for a literal percent in the middle of a string and we're designating the @
character as the escape character. So we have wildcards on both ends and one escaped percent in the middle.)
WHERE state IS NOT NULL (to avoid selecting records with no value in this particular row though we're naming the column)

NOTE: Searching for NULLs is a good check on the data-integrity of the database!
SELECT msp.name, msp.party ' This query locates any name in the msp table in which the party field is NULL (nothing entered)
FROM msp
WHERE msp.party IS NULL

SELECT lname as "Customer", SUM(amount) as "Total Cost" ' Customer last name is coming from DBA.customer table
FROM DBA.sales_order, DBA.fin_data, DBA.customer ' Amount is coming from DBA.fin_data table
WHERE DBA.sales_order.fin_code_id = DBA.fin_data.code ' cust_id is coming from the DBA.sales.order
AND DBA.sales_order.cust_id = DBA.customer.id
GROUP BY lname, cust_id
ORDER by lname

SELECT yr, COUNT(yr) ' Just need to make sure that we're summarizing in the Selection the same as we
FROM movie, actor, casting ' are seeking to GROUP BY
WHERE actor.name = 'John Travolta' and
actor.id = casting.actorid and
casting.movieid = movie.id
GROUP BY yr

SELECT COUNT(name), name ' We're counting the number of times actors have had the starring role
FROM actor, casting
WHERE casting.ord = '1' and
casting.actorid = actor.id
GROUP BY name
HAVING COUNT(name) >= 10 ' We're restricting our groups to those containing counts greater than or equal to 10
ORDER BY name

SELECT Count(actorid), title ' We're counting the cast list for films in 1978
FROM movie, casting
WHERE yr = '1978' and
movie.id = casting.movieid
GROUP BY title ' The Count and Group By commands work in conjunction -- each dependent on other
ORDER BY Count(actorid) DESC ' Sorting the list in descending order
ORDER BY price, pub_id (like the SELECT list, the ORDER BY list can have one element or multiple elements if there are multiple
elements they're separated by commas you can have as many sort levels as you like)

The ORDER BY list can include four kinds of expressions, ranging from simple to complex:
A column name (such as lastname, pub_id, etc.)
A column name and arithmetic operators or functions (price * 1.0825)
A display label assigned to a column or expression in the SELECT clause such as "Income"
A position number (the number of this column or expression in the SELECT list in the example below, price is the fourth entry in
the SELECT list)
ORDER BY 4
NOTE: Position numbers work just like column names in ORDER BY. You can:
Use position numbers to represent simple columns as well as more complex expressions.
Freely mix position numbers and columns or expressions.
Specify a direction with ASC or DESC for position numbers just as with column names.
You can specify an ascending (the default) or descending sort by including the keywords ASC or DESC immediately after the sort item.
Example:
ORDER BY price DESC, pub_id DESC

Concerning the sorting of NULLs Not all systems order nulls the same way. They should be either greater than or less than all non-null
values. Which you get depends on your DBMS implementation.

******************************************************************************************************
SQL Commands to Create a Table
******************************************************************************************************
The create table statement is used to create a new table. Here is the format of a simple create table statement:

create table "tablename" ("column1" "data type", "column2" "data type", "column3" "data type");

Format of create table if you were to use optional constraints:

create table "tablename" ("column1" "data type" [constraint], "column2" "data type" [constraint], "column3" "data type" [constraint]);
[ ] = optional

Note: You may have as many columns as you'd like, and the constraints are optional. Example:

create table employee (first varchar(15), last varchar(20), age number(3), address varchar(30), city varchar(20), state varchar(20));

To create a new table, enter the keywords create table followed by the table name, followed by an open parenthesis, followed by the first
column name, followed by the data type for that column, followed by any optional constraints, and followed by a closing parenthesis. It is
important to make sure you use an open parenthesis before the beginning table, and a closing parenthesis after the end of the last column
definition. Make sure you seperate each column definition with a comma. All SQL statements should end with a ";".

The table and column names must start with a letter and can be followed by letters, numbers, or underscores - not to exceed a total of 30
characters in length. Do not use any SQL reserved keywords as names for tables or column names (such as "select", "create", "insert", etc).

Data types specify what the type of data can be for that particular column. If a column called "Last_Name", is to be used to hold names, then
that particular column should have a "varchar" (variable-length character) data type.

Here are the most common Data types:


char(size) Fixed-length character string. Size is specified in parenthesis. Max 255 bytes.
varchar(size) Variable-length character string. Max size is specified in parenthesis.
number(size) Number value with a max number of column digits specified in parenthesis.
date Date value
number(size,d) Number value with a maximum number of digits of "size" total, with a maximum number of "d" digits to the right of the
decimal.
What are constraints? When tables are created, it is common for one or more columns to have constraints associated with them. A constraint
is basically a rule associated with a column that the data entered into that column must follow. For example, a "unique" constraint specifies
that no two records can have the same value in a particular column. They must all be unique. The other two most popular constraints are "not
null" which specifies that a column can't be left blank, and "primary key". A "primary key" constraint defines a unique identification of
each record (or row) in a table.

Creating and filling a table with data from a 2nd table in a single SQL statement !!
NOTE: Everything below can be run in a single SQL Statement!
Create table NewPubReview
(editor numeric(8) not null,
units numeric(8) not null,
hours numeric(8) not null)
Insert into NewPubReview
Select editor1_id as editor, units1 as units, hours1 as hours
FROM PubReview
WHERE editor1_id = '250503'

Insert into NewPubReview ' Then appending some more data -- into same columns with a different query
Select editor2_id as editor, units2 as units, hours2 as hours
FROM PubReview
WHERE editor2_id = '250503'

Insert into NewPubReview ' And still more data


Select editor3_id as editor, units3 as units, hours3 as hours
FROM PubReview
WHERE editor3_id = '250503'
******************************************************************************************************
SQL commands to append data to a table
******************************************************************************************************
The insert statement is used to insert or add a row of data into the table.

insert into "tablename" (first_column,...last_column) values (first_value,...last_value); [] = optional

Example:

insert into employee (first, last, age, address, city, state) values ('Luke', 'Duke', 45, '2130 Boars Nest', 'Hazard Co', 'Georgia');

* Note: All strings should be enclosed between single quotes: 'string'

To insert records into a table, enter the key words insert into followed by the table name, followed by an open parenthesis, followed by a list
of column names separated by commas, followed by a closing parenthesis, followed by the keyword values, followed by the list of values
enclosed in parenthesis. The values that you enter will be held in the rows and they will match up with the column names that you specify.
Strings should be enclosed in single quotes, and numbers should not.

In the example above, the column name first will match up with the value 'Luke', and the column name state will match up with the value
'Georgia'.
******************************************************************************************************
SQL commands to change table data
******************************************************************************************************
The update statement is used to update or change records that match a specified criteria. This is accomplished by carefully constructing a
where clause. The SET statement is the new data and the where statement is the old data.

UPDATE "tablename"
SET "columnname" = "newvalue"[,"nextcolumn" = "newvalue2"...]
WHERE "columnname" OPERATOR "value" [and|or "column" OPERATOR "value"];
[] = optional

Example of completely changing the value in a field -- where entire field is a certain character string:
UPDATE DBA.sales_order
SET fin_code_id = 'r2'
WHERE fin_code_id = 'r1'

Example of partially changing the first four characters of a field -- and leaving the remainder of the field the same. Use in
conjunction with SUBSTR( expression, start, length) expression.
Let's say we want to change the year portion from 1993 to 1992 of the field order_date:
1993-01-02
1993-01-04
1993-01-06

Firstpart = SUBSTR(order_date, 1, 4)
Secondpart = SUBSTR(order_date, 5) ' The length is optional -- will simply take the rest of the expression (very nice!)

Entire expression would be:


UPDATE DBA.sales_order ' The table were updating
SET order_date = '1992' || SUBSTR(order_date,5) ' The field & replacement value (note the partial string replacement)
WHERE SUBSTR(order_date, 1, 4) = '1993' ' Our search criteria -- looking for the first four characters equal to '1993'

More Examples (note: in examples below we can be looking at other fields in same table -- or even other tables!:

UPDATE phone_book
SET area_code = 623
WHERE prefix = 979;

UPDATE phone_book
SET last_name = 'Smith', prefix=555, suffix=9292
WHERE last_name = 'Jones';

UPDATE employee
SET age = age+1
WHERE first_name='Mary' and last_name='Williams';
******************************************************************************************************
SQL commands to delete rows from table
******************************************************************************************************
The delete statement is used to delete records or rows from the table.

DELETE FROM "tablename" WHERE "columnname" OPERATOR "value" [and|or "column" OPERATOR "value"];
[ ] = optional

Examples:

DELETE FROM employee;

Note: if you leave off the where clause, all records will be deleted!

DELETE FROM employee WHERE lastname = 'May';

DELETE FROM employee WHERE firstname = 'Mike' or firstname = 'Eric';

To delete an entire record/row from a table, enter "delete from" followed by the table name, followed by the where clause which contains the
conditions to delete. If you leave off the where clause, all records will be deleted!!
******************************************************************************************************
SQL command to delete a table
******************************************************************************************************
The drop table command is used to delete a table and all rows in the table.

DROP table "tablename"

Example:

DROP table employee;

To delete an entire table including all of its rows, issue the drop table command followed by the tablename. drop table is different from
deleting all of the records in the table. Deleting all of the records in the table leaves the table including column and constraint information.
Dropping the table removes the table definition as well as all of its rows.
******************************************************************************************************
Show/Hide Controls
******************************************************************************************************
lblNames.Visible = True
lstNames.Visible = True
******************************************************************************************************
Userform_Initialize equivalent
******************************************************************************************************
Private Sub Form_Load()
'Set global variable gintResubmitManuscript to be = 0.
gintResubmitManuscript = 0
...
End Sub
******************************************************************************************************
Form Activation
******************************************************************************************************
stDocName = "frmAssignments"
DoCmd.OpenForm stDocName, , , stLinkCriteria
******************************************************************************************************
Combobox Update and Item Selection
******************************************************************************************************
Private Sub cboPubName_Change()
'Reset row source of project number combo box:
With cboProjNum
.SetFocus
.Text = ""
.Requery
.ListIndex = 0 ' Selects the first item to display
End With
End Sub
******************************************************************************************************
Combobox Load with SQL Query
******************************************************************************************************
Private Sub cboPubName_AfterUpdate()
Dim strSQL As String

mstrPubName = cboPubName.Text

'Develop SQL statement for row source of Project Number combo box:
strSQL = "SELECT DISTINCTROW [tblAssignment].[ProjectNumber] FROM tblAssignment WHERE " & _
"[tblAssignment].[PubName] = '" & mstrPubName & "';"

'Finish establishing combo box properties:


cboProjNum.RowSource = strSQL
cboProjNum.RowSourceType = "Table/Query"
cboProjNum.BoundColumn = 1

End Sub
******************************************************************************************************
View Creation (SQL)
******************************************************************************************************
CREATE VIEW ORDERS_BY_STATE
(CLIENT_NAME, STATE, ORDER_NUMBER)
AS SELECT CLIENT.CLIENT_NAME, STATE, ORDER_NUMBER FROM CLIENT, ORDERS
WHERE CLIENT.CLIENT_NAME = ORDERS.CLIENT_NAME;
Note: The above view has three columns: CLIENT_NAME, STATE, and ORDER_NUMBER. CLIENT_NAME appears in both the
CLIENT and the ORDERS table and serves as the link between the two tables. The new view draws the STATE information from the
CLIENT table and takes the ORDER_NUMBER of each order from the ORDERS table.
Note: Could add other conditions (using the AND logical connector) in the WHERE clause.
******************************************************************************************************
Field (i.e., Column) Constraints
******************************************************************************************************
CREATE TABLE TESTS (
TEST_NAME CHARACTER (30) NOT NULL,
STANDARD_CHARGE NUMERIC (6,2)
CHECK (STANDARD_CHARGE >= 0.0 AND STANDARD_CHARGE <= 200.0)
);
Note: UNIQUE is another constraint you can apply to a column, as well as PRIMARY KEY.
******************************************************************************************************
Table Creation (with SQL)
******************************************************************************************************
CREATE TABLE ORDERS (
ORDER_NUMBER INTEGER NOT NULL,
CLIENT_NAME CHARACTER (30),
TEST_ORDERED CHARACTER (30),
SALESPERSON CHARACTER (30),
ORDER_DATE DATE
);
******************************************************************************************************
Recordset (Working With)
******************************************************************************************************
rsCustomer.MoveNext ' Moves the record pointer to the next record causes an error if there isn't a next record (i.e., you're at the last
record or there aren't any records)
rsCustomer.MovePrevious ' Moves to the previous record causes an error if there isn't a previous record (you are at the very first record
or there aren't any records)
rsCustomer.MoveFirst ' Moves to the very first record in the recordset causes an error only if there aren't any records.
rsCustomer.MoveLast ' Moves to the very last record in the recordset same.
rsCustomer.EOF ' This method returns TRUE/FALSE to indicate if the record pointer is currently located at the end of the available
records.
rsCustomer.BOF ' [same for the start of the recordset] Note: If both EOF and BOF are TRUE then the recordset is empty.
rsCustomer.AddNew ' Creates a new blank record for you to place values in.
rsCustomer.Update ' Saves changes to a record back to the recordset, must be called after any modification and after using the
AddNew function.
rsCustomer.Delete ' Deletes the current record causes an error if there isn't a current record.
rsCustomer.RecordCount ' Property returning the number of records in the recordset. [Note: To ensure that the correct value is returned,
you should call the .MoveLast method first, otherwise this property may just return -1.]
rsCustomer.Close ' Closes the recordset
******************************************************************************************************
Recordset Open Options
******************************************************************************************************
rsCustomer.Open SQL, dbMain, adOpenStatic, adLockReadOnly
(1) SQL = Appropriate SQL statement (as shown below)
(2) dbMain = The Connection object that represents the database to be used, or a connection string, which would cause a new connection to
be created just for this recordset.
(3) adOpenStatic = Specifies which type of recordset should be opened. The choices are:
adOpenDynamic (Dynamic indicates one which we can modify)
adOpenForwardOnly
adOpenKeyset
adOpenStatic (Static is for opening the data as read-only)
(4) adLockReadOnly = The parameter which controls how the database handles record locking for edits performed on this recordset. The
choices are:
adLockBatchOptimistic (Batch setting is for advanced situations)
adLockOptimistic (Optimistic locking means that the database only locks the record when you go to save changes to it the least
amount of locking.)
adLockPessimistic (Pessimistic locking causes the database to lock the record as soon as you start working with it, resulting in a
longer locking time but necessary unless you want to be locked out (by someone else) in the middle of an edit).
adLockReadOnly (ReadOnly specifies that no locking is required, as changes to records will not be allowed).
Note: Other options are also allowed but not often used.
******************************************************************************************************
Database Connection Example
******************************************************************************************************
Public Sub CompleteDatabaseExample()
Dim dbMain as New ADODB.Connection ' Declaring the Connection
Dim rsCustomer as New ADODB.Recordset ' Declaring the Recordset
Dim SQL as String ' Declaring a simple variable
dbMain.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _ ' Connecting to the Database
"Persist Security Info=False;" & _
"Data Source=C:\WINDOWS\Desktop\training.mdb"
SQL = "SELECT * FROM Customer" ' Setting up our SQL Command statement
rsCustomer.Open SQL, dbMain, adOpenDynamic, adLockOptimistic ' Opening the Recordset (see Recordset Options)
Do While Not rsCustomer.EOF ' Loop until we reach the End-Of-File marker
MsgBox "The Customer's Name is: " & rsCustomer("Name")
MsgBox "The Customer's City is: " & rsCustomer("City")
rsCustomer("Country") = "United States"
rsCustomer("Name") = rsCustomer("Name") & " Jr."
rsCustomer.Update ' Updating the Records (see also Recordset (Working With))
rsCustomer.MoveNext ' Moving to the next record (necessary to reach the EOF)
Loop
rsCustomer.Close ' Closing the recordset
dbMain.Close ' Closing the database connection
End Sub
******************************************************************************************************
Declaring a Recordset Object
******************************************************************************************************
Dim rsCustomer as New ADODB.Recordset
******************************************************************************************************
Closing a Database Connection
******************************************************************************************************
Public Sub OpenDatabaseConnection()
Dim dbMain as New ADODB.Connection
dbMain.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & "Persist Security Info=False;" & _
"Data Source=C:\WINDOWS\Desktop\training.mdb"
...
dbMain.Close
End Sub
Note (1): Using the Close method of the Connection object doesn't require any instruction; it takes no parameters and is called in th esame
manner as any other method.
Note (2): This method should always be called when you are done with your database connection; as it frees up system resources.
******************************************************************************************************
Opening a Database Connection
******************************************************************************************************
Public Sub OpenDatabaseConnection()
Dim dbMain as New ADODB.Connection
dbMain.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & "Persist Security Info=False;" & _
"Data Source=C:\WINDOWS\Desktop\training.mdb"
End Sub
Note (1): The string following the Open method above is known as the Connection String and can be created (and tested), if ADO is
installed on your machine, by going to the desktop and creating a new Microsoft Data Link object. Once the data link has been created it can
be opened as a text file and cut & pasted into your program.
******************************************************************************************************
Declaring a Database Connection Object
******************************************************************************************************
Dim dbMain As New ADODB.Connection
Note (1): Before we can declare any variable as ADO (ActiveX Data Objects the latest and greatest data access layer from Microsoft.
Which also supports accessing non-traditional data sources) objects, we need to add a reference to ADO to our project. Go to TOOLS |
REFERENCES and select the Microsoft ActiveX Data Objects 2.x library item.
Note (2): In our database-related projects (from Word) we will always start by creating an instance of a Connection object and opening a
connection to our database. When declaring your Connection variable, you need to use the New keyword, to specify that a new instance of
this object type should be created.
Note (3): This object supports two methods that are of primary interest: Open and Close, which link the object to a specific database and
remove that link, respectively.
******************************************************************************************************
SQL Query (Basic Syntax)
******************************************************************************************************
SELECT <Columns> FROM <Tables> WHERE <Criteria>
Example: SELECT * FROM Customer WHERE (City = "Los Angeles") AND (State = "CA")
Note (1): Columns are specified as a list of field names, separated by commas, like this: Name, Address, City. Field names that consist of
more than one word have to be surrounded by brackets [Street Address] in Access. Can also use an asterisk to specify all possible fields.
Note (2):The set of records from our database which matched the above criteria is represented in our code as a recordset, which is a special
type of database object.
******************************************************************************************************
SQL Query (Joining several tables on common field)
******************************************************************************************************
SELECT Customer.Name, Customer.City, Invoice.ID, Invoice.Date, Invoice.Notes FROM Customer, Invoice WHERE Customer.ID =
Invoice.[Customer] AND Customer.City = "Los Angeles"
Note (1): The query shown above joins the Customer and Invoice tables on their common fields (ID in the Customer Table and Customer in
the Invoice Table), which is accomplished through the use of an appropriate WHERE clause in the SQL statement.
Note (2): In your SQL statements, you can specify the table name in front of a field name to prevent any confusion. This is required if a
field by the same name exists in more than one of the tables involved in the query.
******************************************************************************************************

También podría gustarte