Date post: | 21-Jan-2016 |
Category: |
Documents |
Upload: | sharon-wilkins |
View: | 217 times |
Download: | 3 times |
17 January, 2011 harshana-fernando.blogspot.com 1
Microsoft Office Access
VBA
17 January, 2011 harshana-fernando.blogspot.com 2
Before run macros
Under the Ribbon, click the Options button
In the Microsoft Office Security Options
Click the Enabled radio button
17 January, 2011 harshana-fernando.blogspot.com 3
17 January, 2011 harshana-fernando.blogspot.com 4
On the Ribbon, click Database Tools
Click the Visual Basic button
17 January, 2011 harshana-fernando.blogspot.com 5
17 January, 2011 harshana-fernando.blogspot.com 6
17 January, 2011 harshana-fernando.blogspot.com 7
Introducing Objects
Digital Camera Object
Property Name Property Value
Name Digital Camera
External Color Black
Unit Price 899.95
17 January, 2011 harshana-fernando.blogspot.com 8
17 January, 2011 harshana-fernando.blogspot.com 9
17 January, 2011 harshana-fernando.blogspot.com 10
Programmatically Accessing the Properties of an Object
Private Sub cmdHide_Click() boxRectangle.Visible = False End Sub
17 January, 2011 harshana-fernando.blogspot.com 11
With the Properties of an Object
Private Sub cmdManipulate_Click() boxEnvelop.BackStyle = 1boxEnvelop.BackColor = 979478 boxEnvelop.SpecialEffect = 1 boxEnvelop.BorderColor = 234657 boxEnvelop.BorderWidth = 2 End Sub
17 January, 2011 harshana-fernando.blogspot.com 12
With ObjectName Statements
End With Private Sub cmdManipulate_Click() With boxEnvelop
.BackStyle = 1 .BackColor = 979478 .SpecialEffect = 1 .BorderColor = 234657
.BorderWidth = 2 End With End Sub
17 January, 2011 harshana-fernando.blogspot.com 13
Events
17 January, 2011 harshana-fernando.blogspot.com 14
17 January, 2011 harshana-fernando.blogspot.com 15
VariablesData type Description Range
Byte 1-byte binary data 0 to 255
Integer 2-byte integer – 32,768 to 32,767
Long 4-byte integer – 2,147,483,648 to 2,147,483,647
Single 4-byte floating-point number
– 3.402823E38 to – 1.401298E – 45 (negative values)
1.401298E – 45 to 3.402823E38 (positive values)
Double 8-byte floating-point number
– 1.79769313486231E308 to– 4.94065645841247E – 324 (negative values)
4.94065645841247E – 324 to 1.79769313486231E308 (positive values)
Currency8-byte number with fixed decimal
point– 922,337,203,685,477.5808 to
922,337,203,685,477.5807
String String of characters Zero to approximately two billion characters
Date 8-byte date/time value January 1, 100 to December 31, 9999
17 January, 2011 harshana-fernando.blogspot.com 16
Dim VariableName As DataType
Dim Variable1 As DataType1 Dim Variable2 As DataType2 Dim Variable3 As DataType3
Private Sub Detail_Click() Dim EmployeeIsFullTime As Boolean Dim FullName As String Dim EmploymentStatus As Byte Dim YearHired As Integer Dim DaysOnJob As Long Dim SickTime As Single Dim WeeklyTime As Double Dim HourlySalary As Currency Dim DateHired As Date End Sub
17 January, 2011 harshana-fernando.blogspot.com 17
Private Sub Detail_Click() SomeColor = vbRed Detail.BackColor = SomeColor End Sub
• Return to Microsoft Access and display the form in Form View
• Click the form and notice that it appears red
17 January, 2011 harshana-fernando.blogspot.com 18
Color Name Constant Value Color
Black vbBlack &h00
Red vbRed &hFF
Green vbGreen &hFF00
Yellow vbYellow &hFFFF
Blue vbBlue &hFF0000
Magenta vbMagenta &hFF00FF
Cyan vbCyan &hFFFF00
White vbWhite &hFFFFFF
17 January, 2011 harshana-fernando.blogspot.com 19
Operators
Assignment Operator = Comma , Colon Operator : Double Quotes "" String Concatenator & Negation Operator – Addition + Subtraction - Multiplication *
17 January, 2011 harshana-fernando.blogspot.com 20
Operators
Integer Division \ Division / Exponentiation ^ Remainder Operator Mod Comments '
17 January, 2011 harshana-fernando.blogspot.com 21
Assignment Operator =
The assignment operation is used to make a copy of a value, an expression, or the content of a control and give the copy to another field or expression.
=FirstName
Private Sub Form_Load() Dim NumberOfTracks As Integer NumberOfTracks = 16 End Sub
Private Sub txtFirstName_LostFocus() txtFullName = txtFirstName End Sub
17 January, 2011 harshana-fernando.blogspot.com 22
Comma ,
The comma is used to separate variables used in a group
Sub Exercise() Dim FirstName As String, LastName As String, FullName As StringEnd Sub
17 January, 2011 harshana-fernando.blogspot.com 23
Colon Operator :
The Visual Basic language allows you to write as many statements as necessary on the same line.
Sub Exercise() Dim FirstName As String, LastName As String FirstName = "Arsène" LastName = "Nkoulou“End Sub
Sub Exercise() Dim FirstName As String, LastName As String FirstName = "Arsène" : LastName = "Nkoulou" End Sub
17 January, 2011 harshana-fernando.blogspot.com 24
Double Quotes: ""
Double-quotes are used to display a string.
= "Manchester United"
Private Sub Form_Load() Dim Address As String Address = "12404 Lockwood Drive Apt D4" End Sub
Private Sub cmdReset_Click() txtFirstName = "" txtMI = "" txtLastName = "" txtFullName = "" txtUsername = "" End Sub
17 January, 2011 harshana-fernando.blogspot.com 25
String Concatenator: &
The & operator is used to append two strings, the contents of two controls, or expressions; this is considered as concatenating them.
Value1 & Value2 Private Sub Form_Load() Dim FirstName, LastName As String Dim FullName As String FirstName = "Francis " LastName = "Pottelson" FullName = FirstName & LastName Text0 = FullName End Sub
=Value1 & " " & Value2
17 January, 2011 harshana-fernando.blogspot.com 26
Negation Operator -
A variable or an expression can also be represented as negative by prefixing it with a - sign.
Private Sub Form_Load() Dim NumberOfTracks As Byte Dim Temperature As Integer NumberOfTracks = 16 Temperature = -94 End Sub
17 January, 2011 harshana-fernando.blogspot.com 27
Addition: +
The addition is used to add one value or expression to another.
Value1 + Value2
= Value1 + Value2
Private Sub cmdSqCalculate_Click() Dim dblSide As Double Dim dblPerimeter As Double dblSide = txtSqSide dblPerimeter = dblSide + dblSide + dblSide + dblSide txtSqPerimeter = dblPerimeter End Sub
17 January, 2011 harshana-fernando.blogspot.com 28
Subtraction: -
The subtraction is performed by retrieving one value from another value.
Value1 - Value2
= Value1 - Value2
17 January, 2011 harshana-fernando.blogspot.com 29
Multiplication: *
The multiplication allows adding one value to itself a certain number of times, set by the second value.
Value1 * Value2
= Value1 * Value2
17 January, 2011 harshana-fernando.blogspot.com 30
Example 1
17 January, 2011 harshana-fernando.blogspot.com 31
Example 2
17 January, 2011 harshana-fernando.blogspot.com 32
Integer Division: \
Dividing an item means cutting it in pieces or fractions of a set value. For example, when you cut an apple in the middle, you are dividing it in 2 pieces. If you cut each one of the resulting pieces, you will get 4 pieces or fractions.
Value1 \ Value2
= Value1 \ Value2
17 January, 2011 harshana-fernando.blogspot.com 33
The Division: /
The second type of division results in a decimal number.
Value1 / Value2
= Value1 / Value2
17 January, 2011 harshana-fernando.blogspot.com 34
Exponentiation: ^
Exponentiation is the ability to raise a number to the power of another number.
yx In Microsoft Visual Basic y^x
Total = y^x
17 January, 2011 harshana-fernando.blogspot.com 35
Remainder Operator: Mod
The division operation gives a result of a number with or without decimal values, which is fine in some circumstances. Sometimes you will want to get the value remaining after a division renders a natural result.
The remainder operation is performed with keyword Mod. Its syntax is:
Value1 Mod Value2
= Value1 Mod Value2
17 January, 2011 harshana-fernando.blogspot.com 36
Comments
A comment is a piece of text in a code section that thedatabase engine would not consider when reading your code.
Private Sub Form_Load() ' This line will not be considered as part of the codeEnd Sub
17 January, 2011 harshana-fernando.blogspot.com 37
What is SQL?
SQL is Structured Query Language
What is SQL for?
SQL is used to interact with your database's data
17 January, 2011 harshana-fernando.blogspot.com 38
SQL contains a number of keywords such asSELECT, DELETE, UPDATE, FROM, WHERE, OR, AND, DISTINCT and many others….
17 January, 2011 harshana-fernando.blogspot.com 39
The Structure of an SQL StatementThis illustration shows the principal parts of a typical SQL query statement:
17 January, 2011 harshana-fernando.blogspot.com 40
Whenever a field is specified you have the option to append the table name, separating the two with a dot
SELECT Firstname, Lastname FROM tblStaff ORDER BY Lastname
is the same as: SELECT tblStaff.Firstname, tblStaff.Lastname FROM tblStaff ORDER BY tblStaff.Lastname
If your query refers to more than one table you must include the table name along with the field name.
17 January, 2011 harshana-fernando.blogspot.com 41
Data Type Qualifiers
Text must be enclosed in either single quotes (') or double quotes (")
WHERE tblStaff.Department = "Marketing” Date should be enclosed in hash marks (#) WHERE tblStaff.BirthDate = #09/27/1950#
Number, of any sort, needs no qualifier and can be entered as it is
WHERE tblInvoices.InvoiceNumber > 1500
17 January, 2011 harshana-fernando.blogspot.com 42
How to Write SQL in VBA
Dim strSQL As StringstrSQL = "... THE SQL STATEMENT GOES HERE ...”DoCmd.RunSQL strSQL
Also
DoCmd.RunSQL "... THE SQL STATEMENT GOES HERE ...”
17 January, 2011 harshana-fernando.blogspot.com 43
Write SQL Keywords in Upper Case
Access doesn't care if you do this or not but you will find your code much easier to read and understand if you do
Select tblStaff.Firstname, tblStaff.Lastname from tblStaff where tblStaff.Office="Paris”;
SELECT tblStaff.Firstname, tblStaff.Lastname FROM tblStaff WHERE tblStaff.Office="Paris”;
17 January, 2011 harshana-fernando.blogspot.com 44
Enclose Field Names in Square Brackets
Always put square brackets around field names. Access only demands that you do this when your field names contain spaces. For example, FirstName is OK but in your code First Name must be written [First Name]. The brackets tell Access that all the words in the field name belong together.
SELECT tblStaff.[Firstname], tblStaff.[Lastname]
FROM tblStaff
WHERE tblStaff.[Office]="Paris”;
17 January, 2011 harshana-fernando.blogspot.com 45
Write Each Clause on a Separate Line
SELECT tblStaff.* FROM tblStaff;
SELECT tblStaff.[Firstname], tblStaff.[Lastname] FROM tblStaff WHERE tblStaff.[Office]="Paris”;
17 January, 2011 harshana-fernando.blogspot.com 46
Alternate Single and Double Quotes
17 January, 2011 harshana-fernando.blogspot.com 47
Working with Variables
17 January, 2011 harshana-fernando.blogspot.com 48
Using DoCmd.RunSQL to Run Action Queries
• Build a New Table
DoCmd.RunSQL "CREATE TABLE tblTest ([StaffID] COUNTER CONSTRAINT ndxStaffID PRIMARY KEY, [FirstName] TEXT(25), [LastName] TEXT(30), [BirthDate] DATETIME);"
Enter the line of code as a single line into the Immediate Window, then press Enter
Open the Immediate Window by pressing Ctrl+G. In Access 2000/2002 the Immediate Window will appear, usually docked to the lower edge of the Visual Basic Editor window
17 January, 2011 harshana-fernando.blogspot.com 49
• Add Records to a Table
DoCmd.RunSQL "INSERT INTO tblTest ([FirstName], [LastName], [BirthDate]) VALUES ('Martin', 'Green', #09/27/1950#);"
Access displays a message asking permission to add a record to the table.
Enter the line of code as a single line into the Immediate Window, then press Enter
17 January, 2011 harshana-fernando.blogspot.com 50
• Add a Field to a Table
DoCmd.RunSQL "ALTER TABLE tblTest ADD COLUMN [Age] BYTE;"
Enter the line of code as a single line into the Immediate Window, then press Enter
17 January, 2011 harshana-fernando.blogspot.com 51
• Modify Existing Records
DoCmd.RunSQL "UPDATE tblTest SET [Age]=52 WHERE [FirstName]='Martin' AND [LastName]='Green';"
17 January, 2011 harshana-fernando.blogspot.com 52
• Delete a Table
DoCmd.RunSQL "DROP TABLE tblTest;"
Enter the line of code as a single line into the Immediate Window, then press Enter
You won't see any warning message, but you will find that the table has gone