Access VBA programming

Similar documents
DATABASE AUTOMATION USING VBA (ADVANCED MICROSOFT ACCESS, X405.6)

d2vbaref.doc Page 1 of 22 05/11/02 14:21

DEVELOPING DATABASE APPLICATIONS (INTERMEDIATE MICROSOFT ACCESS, X405.5)

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

ME 142 Engineering Computation I. Condition Statements

La Mesa Language Reference Manual COMS 4115: Programming Languages and Translators Professor Stephen Edwards

CS130/230 Lecture 12 Advanced Forms and Visual Basic for Applications

COUNT Function. The COUNT function returns the number of rows in a query.

SQL-Server. Insert query in SQL Server. In SQL Server (Transact-SQL), the INSERT statement is used to

SQL-Server. learn SQL and operations in ms sql server 2008, ms sql server 2012, ms sql server 2014, ms sql server 2016

MS Access Part 1 (One Day Workshop) Khader Shaik

Not For Sale. Using and Writing Visual Basic for Applications Code. Creating VBA Code for the Holland Database. Case Belmont Landscapes

An InputBox( ) function will display an input Box window where the user can enter a value or a text. The format is

Rani Durgavati Vishwavidyalaya Jabalpur (M.P.) (UICSA) Master of Computer Application (MCA) Practical List of. MCA III SEM Session -2010

1) Introduction to SQL

NOTES: String Functions (module 12)

Programming Logic and Design Sixth Edition

SAMPLE PAPER: 2015 Class :XII Subject : Informatics Practices. Time: 3 hrs. M.M. 70. Section A

Introductory Notes: Condition Statements

CSC Web Programming. Introduction to SQL

SQL User Defined Code. Kathleen Durant CS 3200

CPSC 203 Extra review and solutions

Petros: A Multi-purpose Text File Manipulation Language

Fundamental Microsoft Jet SQL for Access 2000

Ordinary Differential Equation Solver Language (ODESL) Reference Manual

SQL STORED ROUTINES. CS121: Relational Databases Fall 2017 Lecture 9

Creating a Dynamo with VBA Scripts

Statements and Operators

Chapter 2 Working with Data Types and Operators

CPSC 230 Extra review and solutions

VBA Collections A Group of Similar Objects that Share Common Properties, Methods and

CPSC 203 Final exam review and solutions

Microsoft Access XP (2002) - Advanced Queries

CHAD Language Reference Manual

Oracle 1Z0-071 Exam Questions and Answers (PDF) Oracle 1Z0-071 Exam Questions 1Z0-071 BrainDumps

function [s p] = sumprod (f, g)

7 Control Structures, Logical Statements

variables programming statements

Introduction to: Computers & Programming: Review prior to 1 st Midterm

Download the files from you will use these files to finish the following exercises.

Visual C# 2012 How to Program by Pe ars on Ed uc ati on, Inc. All Ri ght s Re ser ve d.

Control Structures. CIS 118 Intro to LINUX

Twister: Language Reference Manual

Fundamentals. Fundamentals. Fundamentals. We build up instructions from three types of materials

DATABASE AUTOMATION USING VBA (ADVANCED MICROSOFT ACCESS, X405.6)

Burrows & Langford Appendix D page 1 Learning Programming Using VISUAL BASIC.NET

A DVANCED T OPICS IN A CCESS: MACROS

Subquery: There are basically three types of subqueries are:

A Beginner s Guide to Programming Logic, Introductory. Chapter 6 Arrays

egrapher Language Reference Manual

In addition to the primary macro syntax, the system also supports several special macro types:

Customizing Access Parameter Queries

Database Technology. Topic 6: Triggers and Stored Procedures

IDENTIFYING UNIQUE VALUES IN AN ARRAY OR RANGE (VBA)

NOTES: Procedures (module 15)

BASIC EXCEL SYLLABUS Section 1: Getting Started Section 2: Working with Worksheet Section 3: Administration Section 4: Data Handling & Manipulation

The PCAT Programming Language Reference Manual

The SPL Programming Language Reference Manual

BoredGames Language Reference Manual A Language for Board Games. Brandon Kessler (bpk2107) and Kristen Wise (kew2132)

Band Editor User Guide Version 1.3 Last Updated 9/19/07

Jarek Szlichta

CISC 1600 Lecture 3.1 Introduction to Processing

SQL Functionality SQL. Creating Relation Schemas. Creating Relation Schemas

Chapter 2.4: Common facilities of procedural languages

Lab # 6. Using Subqueries and Set Operators. Eng. Alaa O Shama

CS 3360 Design and Implementation of Programming Languages. Exam 1

Ex: If you use a program to record sales, you will want to remember data:

OBJECT ORIENTED SIMULATION LANGUAGE. OOSimL Reference Manual - Part 1

Excel Macro Record and VBA Editor. Presented by Wayne Wilmeth

Using Parameter Queries

Introduction to SQL. IT 5101 Introduction to Database Systems. J.G. Zheng Fall 2011

CS201- Introduction to Programming Current Quizzes

Introduction to Visual Basic and Visual C++ Arithmetic Expression. Arithmetic Expression. Using Arithmetic Expression. Lesson 4.

Oracle Database 11g: SQL and PL/SQL Fundamentals

SECTION II: LANGUAGE BASICS

VBA Handout. References, tutorials, books. Code basics. Conditional statements. Dim myvar As <Type >

DINO. Language Reference Manual. Author: Manu Jain

Mahathma Gandhi University

Chapter 3: Programming with MATLAB

CS2 Databases TEST 1 25 August 2003 Student Number: MARK: /35

Microsoft Visual Basic 2015: Reloaded


Access VBA Fundamentals

Introduction The SELECT statement: basics Nested queries Set operators Update commands Table management

Separate, Split & Remove Substring & Number from Text with Excel Functions & VBA

Visual Basic for Applications

Sprite an animation manipulation language Language Reference Manual

Retrieving Data Using the SQL SELECT Statement. Copyright 2004, Oracle. All rights reserved.

SQL Fundamentals. Chapter 3. Class 03: SQL Fundamentals 1

Real SQL Programming 1

Oracle EXAM - 1Z Oracle Database SQL Expert. Buy Full Product.

DEVELOPING OBJECT ORIENTED APPLICATIONS

Combining Data.the EG way

MANAGING DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9)

Microsoft Excel 2010 Level 1

Getting Information from a Table

Software Development Techniques. December Sample Exam Marking Scheme

Save and Load Searches in Access VBA

Input File Syntax The parser expects the input file to be divided into objects. Each object must start with the declaration:

Dr. Nahid Sanzida b e. uet .ac.

Transcription:

Access VBA programming TUTOR: Andy Sekiewicz MOODLE: http://moodle.city.ac.uk/ WEB: www.staff.city.ac.uk/~csathfc/acvba

The DoCmd object The DoCmd object is used to code a lot of the bread and butter operations of Access e.g., DoCmd.OpenForm "frmcustomers" OBJECT METHOD ARGUMENT The OpenForm part of the statement is a method of the object (one of many). A dot separates the object and the method. The first argument required by the method is the name of the form to open, supplied in the form of a string, which means the name is enclosed by double quotes.

Filtername argument The Filtername argument allows you to limit the records opened in the form to the records defined by a saved query e.g., DoCmd.OpenForm "frmcustomers",,"qryregionalcustomers"

WhereCondition argument The syntax for the WhereCondition argument is: a string expression that's a valid SQL WHERE clause without the word WHERE therefore, the string expression we need is "City <> 'London' "

Sub Rectangle() Variables a basic program Dim height as Single, width as Single, area as Single height = 5 width = 4 area = width * height MsgBox "The area is " & area End Sub The concatenation operator is & (Shift+7).

Getting input from the user This code asks the user for their height and stores the input in a variable Sub AskHeight() Dim height as Single height = InputBox("Please enter your height in metres") End Sub

Msgbox A MsgBox is used in a program to communicate to the user e.g., MsgBox "100 row(s) copied",, "City University" A MsgBox can also be used by the user of the program to choose a course of action e.g., var = MsgBox("Do you want to continue?", vbyesnocancel) NOTE THE COMPULSORY USE OF BRACKETS IN THIS USAGE An If Then ElseIf statement would be needed to usefully employ the MsgBox

If Then statement If <condition> Then End If line(s) of code to be executed if condition is True You can specify an alternative course of action using Else If <condition> Then line(s) of code to be executed if condition is True Else line(s) of code to be executed if condition is False End If

If Then ElseIf You can specify multiple alternative courses of action using ElseIf If <condition1> Then statement(s) to be executed if condition1 is True ElseIf <condition2> Then statement(s) to be executed if condition1 is False and condition2 is true ElseIf <condition3> Then Else End If statement(s) to be executed if condition1 and condition2 are false and condition3 is True, and so on. optionally, statement(s) to be executed if none of the above conditions are True

Functions The purpose of a function is to return a value. The first line of a function, the function declaration, defines how the function operates. The example shows a function that will return a real number, given a integer. Function Discount (q as Integer) As Single FUNCTION ARGUMENT DATA TYPE DATA TYPE OF NAME OF ARGUMENT RETURN VALUE Specifically, if you supply an order quantity, the function will return a percentage to be used as a discount value. A function can be used directly in a query, on a form or report, or can be called by a VBA subprocedure

Example Function Function Discount (q as Integer) as Single 'calculates a discount based on the quantity ordered If q >= 20 Then Discount = 0.2 ElseIf q >= 10 Then Discount = 0.1 Else Discount = 0 End If End Function

Objects VBA provides Objects for you to manipulate. Any Object has three things associated with it. An Object has: PROPERTIES Eg. Forms!frmCustomers.CustomerID.Forecolor = vbred METHODS Eg. DoCmd.OpenReport Employee Rates EVENTS Eg. The Click event of a button. A program can be made to run only when a button is clicked.

Collections Some objects are collection objects e.g., Forms Some objects are singular e.g., Form Application Forms Collection Form A collection object can be said to consist of the singular object that bears the same name e.g., The collection of Forms consists of individual Form objects A singular object can be said to contain other objects e.g., The Form object contains a collection of Controls Controls Collection Control

Forms Collection example Application Forms Collection frmcustomers Form frmsalesorders Form Form Controls Collection Controls Collection CustomerID Control CustomerName Control Control OrderNo Control CustomerID Control Control The illustration depicts a situation where two forms are open, frmcustomers and frmsalesorders

Referring to a Control on a Form The unnecessarily long way Application.Forms!frmCustomers.Controls!CustomerID can be shortened to Forms!frmCustomers!CustomerID Forms!frmCustomers.CustomerID However, if you are writing code on the module for the form that contains the control then you can refer to the control directly, e.g., CustomerID better still, you can use the Me property of the form e.g., Me!CustomerID The two examples below are equivalent assuming the code is on the module belonging to frmcustomers Forms!frmCustomers.CustomerID.Forecolor = vbred Me!CustomerID.Forecolor = vbred or

.....