100% found this document useful (1 vote)
13 views

[FREE PDF sample] Microsoft Access VBA Programming for the Absolute Beginner Michael Vine ebooks

The document provides links to various programming and technical ebooks available for download, including titles on Microsoft Access VBA, Python programming, and more. Each ebook is aimed at beginners and includes essential information about the content and authors. Users can download the ebooks in different formats such as PDF, ePub, and MOBI from the specified website.

Uploaded by

heesezumran5
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
13 views

[FREE PDF sample] Microsoft Access VBA Programming for the Absolute Beginner Michael Vine ebooks

The document provides links to various programming and technical ebooks available for download, including titles on Microsoft Access VBA, Python programming, and more. Each ebook is aimed at beginners and includes essential information about the content and authors. Users can download the ebooks in different formats such as PDF, ePub, and MOBI from the specified website.

Uploaded by

heesezumran5
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

Download the Full Version of the Ebook with Added Features ebookname.

com

Microsoft Access VBA Programming for the Absolute


Beginner Michael Vine

https://ebookname.com/product/microsoft-access-vba-
programming-for-the-absolute-beginner-michael-vine/

OR CLICK HERE

DOWLOAD NOW

Download more ebook instantly today at https://ebookname.com


Instant digital products (PDF, ePub, MOBI) available
Download now and explore formats that suit you...

Python Programming for the Absolute Beginner 3rd Edition


Michael Dawson

https://ebookname.com/product/python-programming-for-the-absolute-
beginner-3rd-edition-michael-dawson/

ebookname.com

Microsoft Visual Basic 2008 Express Programming for the


Absolute Beginner 1st Edition Ford

https://ebookname.com/product/microsoft-visual-basic-2008-express-
programming-for-the-absolute-beginner-1st-edition-ford/

ebookname.com

Microsoft Windows PowerShell 2 0 programming for the


absolute beginner 2nd ed Edition Jerry Lee Ford

https://ebookname.com/product/microsoft-windows-
powershell-2-0-programming-for-the-absolute-beginner-2nd-ed-edition-
jerry-lee-ford/
ebookname.com

Carbonates in Continental Settings Facies Environments and


Processes 1st Edition A.M. Alonso-Zarza And L.H. Tanner
(Eds.)
https://ebookname.com/product/carbonates-in-continental-settings-
facies-environments-and-processes-1st-edition-a-m-alonso-zarza-and-l-
h-tanner-eds/
ebookname.com
The American People in the Great Depression Freedom from
Fear Part One 1st Edition David M. Kennedy

https://ebookname.com/product/the-american-people-in-the-great-
depression-freedom-from-fear-part-one-1st-edition-david-m-kennedy/

ebookname.com

Statistics for Business and Economics 1st European Edition


Carlos Cortinhas

https://ebookname.com/product/statistics-for-business-and-
economics-1st-european-edition-carlos-cortinhas/

ebookname.com

Aircraft Carriers of the World 1914 to the Present Roger


Chesneau

https://ebookname.com/product/aircraft-carriers-of-the-world-1914-to-
the-present-roger-chesneau/

ebookname.com

Aircraft Fuel Systems Roy Langton

https://ebookname.com/product/aircraft-fuel-systems-roy-langton/

ebookname.com

Pathological Anxiety Emotional Processing in Etiology and


Treatment 1st Edition Barbara Olasov Rothbaum

https://ebookname.com/product/pathological-anxiety-emotional-
processing-in-etiology-and-treatment-1st-edition-barbara-olasov-
rothbaum/
ebookname.com
Structures in the Universe by Exact Methods Formation
Evolution Interactions 1st Edition Krzysztof Bolejko

https://ebookname.com/product/structures-in-the-universe-by-exact-
methods-formation-evolution-interactions-1st-edition-krzysztof-
bolejko/
ebookname.com
Microsoft Access
®

VBA Programming
for the Absolute
Beginner
Second Edition

MICHAEL VINE
© 2005 by Thomson Course Technology PTR. All rights reserved. No Publisher and GM of Course PTR:
part of this book may be reproduced or transmitted in any form or by Stacy L. Hiquet
any means, electronic or mechanical, including photocopying, record-
Associate Director of Marketing:
ing, or by any information storage or retrieval system without written
Sarah O’Donnell
permission from Thomson Course Technology PTR, except for the
inclusion of brief quotations in a review. Marketing
g Manager:
Heather Hurley
The Premier Press and Thomson Course Technology PTR logo and related
trade dress are trademarks of Thomson Course Technology PTR and Manager of Editorial Services:
may not be used without written permission. Heather Talbot
Microsoft and Access 2003 are registered trademarks of Microsoft Cor- Acquisitions Editor:
poration in the United States and/or other countries. Mitzi Koontz
All other trademarks are the property of their respective owners. Senior Editor:
Mark Garvey
Important: Thomson Course Technology PTR cannot provide software
support. Please contact the appropriate software manufacturer’s tech- Marketing Coordinator:
nical support line or Web site for assistance. Jordan Casey
Thomson Course Technology PTR and the author have attempted Project Editor:
throughout this book to distinguish proprietary trademarks from Scott Harris/Argosy Publishing
descriptive terms by following the capitalization style used by the man-
ufacturer. Technical Reviewer:
Eric Olson
Information contained in this book has been obtained by Thomson
Course Technology PTR from sources believed to be reliable. However, PTR Editorial Services Coordinator:
because of the possibility of human or mechanical error by our Elizabeth Furbish
sources, Thomson Course Technology PTR, or others, the Publisher Copy Editor:
does not guarantee the accuracy, adequacy, or completeness of any Tonya Cupp
information and is not responsible for any errors or omissions or the
results obtained from use of such information. Readers should be par- Interior Layout Tech:
ticularly aware of the fact that the Internet is an ever-changing entity. Shawn Morningstar
Some facts may have changed since this book went to press.
Cover Designer:
Educational facilities, companies, and organizations interested in mul- Mike Tanamachi
tiple copies or licensing of this book should contact the publisher for
CD-ROM Producer:
quantity discount information. Training manuals, CD-ROMs, and por-
Keith Davenport
tions of this book are also available individually or can be tailored for
specific needs. Indexer:
Nancy Fulton
ISBN: 1-59200-723-6
Library of Congress Catalog Card Number: 2004114910 Proofreader:
Printed in the United States of America Jan Cocker
05 06 07 08 09 BH 10 9 8 7 6 5 4 3 2 1

Thomson Course Technology PTR,


a division of Thomson Course Technology
25 Thomson Place
Boston, MA 02210
http://www.courseptr.com
Acknowledgments

W
riting any book is not easy, especially a technical programming book. It
takes many great, patient, and talented people to write, edit, design,
market, finance, and produce a book and accompanying CD-ROM. Without
the publishing assistance of Mitzi Koontz, Scott Harris, and Tonya Cupp, it would
be impossible for me to share with you my knowledge of programming in such
a professional and fun manner.
I’d like to thank a good friend, Eric Olson, who ensured the technical accuracy
of this book. Thanks Eric! Now that the book is done, let’s go fishing!
Beyond the technical and business workings of creating a book, the Author must
be fed, loved, and encouraged. Without the support of my beautiful wife Sheila,
this would never have happened. Thanks baby: I love you!
About the Author

M
ichael Vine has taught computer programming, Web design, and database
classes at Indiana University/Purdue University in Indianapolis, IN, and
at MTI College of Business and Technology in Sacramento, CA. Michael
has over 13 years of experience in the information technology profession. He
currently works full time at a Fortune 100 company as an IT Project Manager
overseeing the development of enterprise data warehouses.
Contents
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii

Chapter 1 Access Essentials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1


Getting Started with Access 2003 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
System Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Migrating to Access 2003. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Your First Access Database. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Creating an Access Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Tables and Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Relationships . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Common Controls. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Hungarian Notation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Bound Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Startup Forms and Dashboards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Getting Help with Access 2003 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32

Chapter 2 Introduction to Access VBA . . . . . . . . . . . . . . . . . . .35


The Event-Driven Paradigm. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Object-Based Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
The VBA IDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Introduction to Event Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Introduction to VBA Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Accessing Objects and Their Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
The Forms Collection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
The Me Keyword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Assignment Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Command and Label Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Getting User Input with Text Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
Variables and Beginning Data Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Variable Naming Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
Variable Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Option Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
viii Access VBA Programming for the Absolute Beginner, Second Edition

VBA Arithmetic and Order of Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59


Chapter Program: Fruit Stand . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65

Chapter 3 Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .67


If Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Nested If Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Compound If Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Select Case Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
Dialog Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
Message Box . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
Input Box. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
Common Controls Continued . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
Option Group. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
Option Buttons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
Check Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
Toggle Buttons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
Chapter Program: Hangman . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95

Chapter 4 Looping Structures . . . . . . . . . . . . . . . . . . . . . . . . . . .97


Introduction to Looping Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
Do While . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
Do Until. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
Loop While . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
Loop Until . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
For . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
List and Combo Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
Adding Items . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
Removing Items. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
Managing Columns. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
Random Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
Chapter Program: Math Quiz . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118

Chapter 5 Functions Continued . . . . . . . . . . . . . . . . . . . . . . . . .121


String-Based Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
UCase . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
LCase . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
Len . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
Contents ix

StrComp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
Right . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
Left . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
Mid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
InStr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
Date and Time Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
Date . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
Day . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
WeekDay . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
Month . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
Year. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
Second . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
Minute . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
Hour . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
Now . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
Conversion Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
Val. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
Str . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
Chr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
Asc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
Formatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
Formatting Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
Formatting Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
Formatting Date and Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
Chapter Program: Secret Message . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144

Chapter 6 Code Reuse and Data Structures . . . . . . . . . . . . .147


Code Reuse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
Introduction to User-Defined Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
Subprocedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
Function Procedures. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
Arguments and Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
Standard Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
Single-Dimension Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
Two-Dimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
Dynamic Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
Passing Arrays as Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
x Access VBA Programming for the Absolute Beginner, Second Edition

User-Defined Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164


Type and End Type Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
Declaring Variables of User-Defined Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
Managing Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
Chapter Program: Dice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177

Chapter 7 Debugging, Input Validation,


File Processing, and Error Handling . . . . . . . . . .181
Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
Stepping Through Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
Breakpoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
Immediate Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
Locals Window. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
Watch Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
Input Validation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
IsNumeric . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
Checking a Range of Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
Error Handling. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
The Err Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
The Debug Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
File Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
About Sequential File Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
Opening a Sequential Data File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
Reading Sequential Data from a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
Writing Sequential Data to a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
Closing Data Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
Error Trapping for File Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
Chapter Program: Trivial Challenge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210

Chapter 8 Microsoft Jet SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . .213


Introduction to Jet SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
Data Manipulation Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
Simple SELECT Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
Computed Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
Built-In Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
Sorting. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
Contents xi

Grouping. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
INSERT INTO Statement. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
UPDATE Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232
DELETE Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232
Data Definition Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
Creating Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
Altering Tables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
DROP Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235

Chapter 9 Database Programming with ADO . . . . . . . . . . . .239


ADO Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
Connecting to a Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240
Working with Recordsets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
Introduction to Database Locks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
Introduction to Cursors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
Retrieving and Browsing Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
Updating Records . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
Adding Records . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259
Deleting Records . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261
Chapter Program: Choose My Adventure. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274

Chapter 10 Object-Oriented Programming


with Access VBA . . . . . . . . . . . . . . . . . . . . . . . . . . . . .277
Introduction to Object-Oriented Programming . . . . . . . . . . . . . . . . . . . . . . . . . . 277
Creating Custom Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278
Working with Class Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
Property Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281
Method Procedures. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284
Creating and Working with New Instances . . . . . . . . . . . . . . . . . . . . . . . . . . 287
Working with Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291
Adding Members to a Collection. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291
Removing Members from a Collection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292
Accessing a Member in a Collection. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
For Each Loops. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
Chapter Program: Monster Dating Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294
Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
xii Access VBA Programming for the Absolute Beginner, Second Edition

Chapter 11 Microsoft Office Objects . . . . . . . . . . . . . . . . . . . .303


Introduction to Microsoft Office Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303
Assistant Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304
Balloon Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310
Command Bars. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
CommandBarControl Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314
CommandBarButton Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315
FileDialog Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316
Chapter Program: Animated Math . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323

Chapter 12 Introduction to Data Access


Pages and Access Security . . . . . . . . . . . . . . . . . . .325
Introduction to Data Access Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325
Creating Data Access Pages. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327
Adding Group Levels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331
Publishing Data Access Pages. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335
Introduction to Access Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336
Activating Logon Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336
Enabling User-Level Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338
Connecting to a Workgroup. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342
Securing Access from Data Access Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344
Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346

Appendix A Common Character Codes . . . . . . . . . . . . . . . . . . .349

Appendix B Keyboard Shortcuts for the Code Window . . .351

Appendix C Trappable Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . .353

Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .359
Introduction

I
ntroduced in the early 1990s, Microsoft Access has become one of the
most powerful and popular applications in the Microsoft Office suite of
applications. Part of Microsoft Office Professional Edition 2003,
Microsoft Access 2003 allows database developers and programmers to build
dynamic and easily portable databases. Access comes with many easy-to-use features
such as graphical forms, report designers, and SQL query builders, as well as a sub-
set of the Visual Basic language known as VBA for building data-driven applications.
Microsoft Access VBA Programming for the Absolute Beginner, Second Edition, is not a
guide on how to use Access and its many wizards. There are already many books
that do that! Instead, Microsoft Access VBA Programming for the Absolute Beginner
concentrates on VBA programming concepts including variables, conditions,
loops, data structures, procedures, file I/O, and object-oriented programming
with special topics including Microsoft Jet SQL, database programming with
ADO, Microsoft Office objects, and data access pages.
Using Premier Press’s Absolute Beginner series guidelines’ professional insight, clear
explanations, examples, and pictures, you learn to program in Access VBA. Each
chapter contains programming challenges, a chapter review, and a complete program
that uses chapter-based concepts to construct a fun and easily built application.
To work through this book in its entirety, you should have access to a computer
with Microsoft Access installed. The programs in this book were written in
Microsoft Office 2003, specifically Access 2003. Those readers using older ver-
sions of Microsoft Access, such as Access 2002 or Access 2000, will find that many
of the VBA programming concepts still apply.

What You’ll Find in This Book


To learn how to program a computer, you must acquire a complex progression of skills.
If you have never programmed at all, you will probably find it easiest to go through the
chapters in order. Of course, if you are already an experienced programmer, it might
not be necessary to do any more than skim the earliest chapters. In either case, pro-
gramming is not a skill you can learn by reading. You’ll have to write programs to
learn. This book has been designed to make the process reasonably painless.
xiv Access VBA Programming for the Absolute Beginner, Second Edition

Each chapter begins with a brief introduction to chapter-based concepts. Once inside the
chapter, you’ll look at a series of programming concepts and small programs that illustrate
each of the major points of the chapter. Finally, you’ll put these concepts together to build a
complete program at the end of the chapter. All of the programs are short enough that you
can type them in yourself (which is a great way to look closely at code), but they are also avail-
able on the accompanying CD-ROM. Located at the end of every chapter is a summary that
outlines key concepts learned. Use the summaries to refresh your memory on the important
concepts. In addition to summaries, each chapter contains programming challenges that will
help you learn and cement chapter-based concepts. Throughout the book, I’ll throw in a few
other tidbits, notably the following:

TRI
CK These are good ideas that experienced programmers like to pass on.

TRA
P There are a few areas where it’s easy to make a mistake. I’ll point them out to
you as we go.

HIN
T Pay special attention to these areas for clarification or emphasis on chapter
concepts.

In the Real World


As you examine concepts in this book, I’ll show you how the concepts are used beyond beginning
programming.

Who This Book Is For


Microsoft Access VBA Programming for the Absolute Beginner, Second Edition, is designed for the
beginning Access VBA programmer. Persons with backgrounds in other programming lan-
guages and databases will find this book to be a good tutorial and desk reference for Access
VBA. Specifically, this book is for the following groups:
• High school or college students enrolling or enrolled in an Access programming class
• Programming hobbyists and enthusiasts
• Office personnel with beginning database programming responsibilities
• Professional database developers wanting to learn the Microsoft Access VBA language
• Home users wanting to learn more about Access and VBA
1
C H A P T E R

Access
Essentials

ntroduced over a decade ago, Microsoft Access is a fully functional RDBMS

I (Relational Database Management System) that has become one of the most
powerful programs in the Microsoft Office suite of applications.
Part of the Microsoft Office Professional Edition 2003, Access 2003 provides both
beginning and professional database developers alike a cost effective way to lever-
age key database functionality with an easy-to-use graphical interface.
In this chapter I get you started using the Access 2003 application by showing you
Access essentials such as files, databases, tables, fields, relationships, forms, con-
trols, and queries. You learn these Access essentials by building a simple database
to manage students and homework assignments.

Getting Started with Access 2003


Microsoft has done a great job making Access 2003 intuitive and easy to learn by
providing a simple interface and an excellent help system. If you’re already famil-
iar with previous versions of Access databases, you’ll appreciate the new and
updated functionality for
• Viewing object dependencies
• Enabling automatic error checking in forms and reports
• Propagating control property changes to bound fields
• Smart Tags with Access objects
2 Access VBA Programming for the Absolute Beginner, Second Edition

• Database backups
• XML support
• Improved Microsoft Office online help
• Collaborate development efforts with friends and colleagues using Microsoft
SharePoint Services
• Improved macro and function security
• Context-based help for SQL view
• Manage SQL Server databases with Access projects
• Updated Microsoft Office 2003 Object Model
• New Visual Basic objects, properties, and methods

System Requirements
System requirements for installing and using Microsoft Access 2003 are straightforward, as
depicted in Table 1.1.

T A B L E 1.1 A C C E S S 20 03 S Y S T E M R E Q U I R E M E N T S

Component Requirements
Processor Pentium 233 MHz or faster processor
Memory 128 MB or RAM or greater
Hard Disk 180 MB minimum, an additional 200 MB for optional files
Drive CD-ROM or DVD
Display 600×800 or higher resolution monitor
Operating System Windows 2000 with sp3, Windows XP or later

Once installed, you can harness many of the great features of Access 2003 on your local PC
by building databases with tables and queries, and creating user interfaces with forms,
reports, and Data Access Pages.
If you wish to deploy Data Access Pages over the Internet, you also need access to a web
server such as IIS (Internet Information Services). For more information please visit
Microsoft’s IIS web site at http://www.microsoft.com/WindowsServer2003/iis/default.mspx.
Chapter 1 • Access Essentials 3

Microsoft SharePoint Services is also recommended for professional Access developers col-
laborating with other colleagues on development activities such as versioning, check-in/
check-out processes, and approval workflows. For more information, see Microsoft’s Share-
Point Services web site at http://www.microsoft.com/windowsserver2003/technologies/
sharepoint/default.mspx.

Migrating to Access 2003


Depending on which previous Access version you were leveraging, migration woes range
from none to minor. In fact, migrating from Access 2002 is real easy. In most cases you simply
need to open your previously created Access 2002 database in Access 2003 once installed.
Your Data Access Pages and VBA code from Access 2002 should also open in Access 2003 with
no problems.
If you’re migrating from Access 2000, it is also easy to upgrade as Access 2003 supports the
same file format. Like previous versions of Access 2002 databases, Access 2003 should import
your Access 2000 databases with no problems, including your Access 2000 VBA code. How-
ever, you need to convert any Data Access Pages built in Access 2000 by simply opening the
Data Access Page in Design view with an Access 2002 or higher database. Microsoft Access
should then prompt you to convert the Data Access Page.
For versions older than 2000, such as Access 97, you must convert the Access files first.
Microsoft provides step-by-step procedures and toolkits for converting older Access files in
this situation.
For more information on converting older Access files, simply use the key phrase “convert-
ing an Access file” in the Microsoft Office online help in Access 2003 or on the web at
http://office.microsoft.com.

Your First Access Database


IT professionals, teachers, students, accountants, managers, scientists, computer enthusi-
asts, and many more people just like you have a need to collect and manage data. Data that
is complex or changes frequently is best stored and managed in a database such as Access.
Let’s say you are a school teacher who has a list of students, attendance, grades, tests,
quizzes, and homework. You could keep track of your students, their attendance, grades,
and such in a notebook or in an electronic document. But suppose one of your students has
a name change in the middle of the school year. To solve this record-keeping issue, you need
to update all occurrences of this student’s name in your lists, whether paper or electronic.
In a well-designed Access database, you only need to make this change once.
Another Random Scribd Document
with Unrelated Content
All at once the negro, with a roar like that of a wild beast, whipped a
curved sword out of a belt about his waist, and, springing forward, delivered
a furious sweeping cut which, if it had taken effect, must have severed
Royce's head from his shoulders.

Fortunately for him, however, he was quick of eye and wit, and nimble in
his movements. At school he had had no match in boxing and fencing. Being
absolutely unarmed, he had no means of parrying the stroke; but he dropped
on one knee, and the scimitar whistled within an inch of his crown, striking
with a crashing stroke the wall on his right hand.

While the negro was still bent forward with the force of his blow, Royce
sprang low at his knees, and, tugging them towards him, brought the man
with a thud to the floor. The sword fell from his hand and clashed on the
stone flags, and Royce reached down to get hold of it. But the negro sprang
to his feet with agility amazing in so huge a man, and hurled himself upon
the Englishman.

Royce had just time to straighten himself and prevent himself from being
thrown down; the next moment the negro's arms were about him; he felt hot
breath upon his face, and saw the gleaming teeth and infuriated eyes of a
man from whom he knew he could expect no mercy.

He was well acquainted with the styles of wrestling in vogue in England


—the Cumberland, the Devon, the Lancashire; but he was instantly aware
that the negro's method was none of these. It was, in fact, a form of wrestling
like that which had been practised ages ago in the Olympic games, and had
no doubt been introduced into Northern Africa by the Romans in the days of
Cæsar and Pompey. It resembled the catch-as-catch-can style of Lancashire
more nearly than the lighter styles with which Royce was familiar.

The negro's aim seemed to be to throttle his opponent, or to squeeze the


breath out of his body; and Royce, struggle as he might, felt the thick,
muscular arms gripping him more and more closely. Slighter in build, he had
no chance of employing the feints and tricks which might have compensated
for a less powerful physique in dealing with an Englishman. In that straining
grasp, there was no hope for the lesser man; in a few seconds the struggle
must end.
AT GRIPS WITH THE NEGRO

The encounter, the coming to grips, had happened so swiftly that Royce
had had no time to think that there was help at hand in the shape of John. But
now, at this critical moment, when he felt that the very life was being
crushed out of him, he remembered the staunch companion of his journey,
who could not be far away.

Making a desperate effort to fill his lungs, he uttered a shout, or rather a


choking gurgle, which no one would have recognised as the voice of an
Englishman. The negro laughed, anticipating the moment when the white
man would lie limp and lifeless at his feet. Bub John, climbing the hill with a
rabbit dangling in his hand, heard the two sounds—the gasping cry, the loud,
mocking laugh. Hastening forward at a run, he shouted aloud, giving a long,
penetrating note like the yodel of the Swiss mountain shepherd. The sound,
growing louder moment by moment, came to the ears of the negro. He
realised instantly that, unless he could dispose of the Englishman at once, he
would soon have two men to deal with.

The encouraging sound gave Royce new strength. He put forth his last
energies to resist the strangling grip.

"Yoi-aloo! Yoi-aloo!"

The newcomer was close at hand. The panting negro lowered his arms,
caught Royce about the hips, and tried to lift him, intending to dash him
upon the floor. Royce flung his legs about the giant's thighs, stiffened his
muscles, and dragged with all his force upon the negro's shoulders.

"Yoi-aloo! Yoi-aloo!"

The game was up! The negro dared not wait longer. Loosening his grip,
he wrenched himself out of Royce's entwining arms, thrust him away, and,
turning about, rushed through the passage into the open. There he saw John
hurrying up within twenty paces of him, and swerving to the left, in five
seconds had disappeared among the bushes.

CHAPTER III
THE STORY OF GORUBA

Royce, when the negro left him, was breathless from the struggle. But he
had the presence of mind to run back to the room where he had passed the
night, pick up his rifle and revolver from the floor, and hurry to the entrance.
There John met him. The anxious alarm on the Hausa's face gave way to a
broad smile when he saw that his master was safe.

"Where is that ruffian?" asked Royce, looking round for his assailant.

"Him run away quick," replied John, pointing to the bushes. "Berry much
'fraid of me."

"You were just in time, John. He was crushing me to a jelly. It's a lesson
to me not to part with my revolver for an instant. You saw no other black
men about?"

"No, sah. Him berry big chap, sure 'nuff."

"A giant! I am bruised all over. I met him as I was coming out for a bathe,
and he sprang at me at once. Why should he do that?"

"Bad fella, sah."

"I daresay; but it was something more than original sin. He went for me
with as much ferocity as an eagle whose nest I disturbed once. Does this
place belong to him, I wonder?"

John could only repeat that the man was a "bad fella." But Royce felt a
good deal puzzled. The negro's savage onslaught might be explained by his
regarding the white man as a robber, but there appeared to be nothing in the
place worth stealing. It was strange that he should have so fiercely resented
what was, after all, an innocent intrusion.

"We'll have another look round before we start," said Royce. "Or, rather, I
will. You keep guard at the doorway, John, and call me if you see anyone
moving about outside."
Royce searched the building thoroughly. The result confirmed his
overnight impression, that it was in a fair state of preservation. But there was
nothing in any of the rooms to indicate present or even remote occupation.
Except for fragments of stone and rubble, they were bare. There was nothing
to tempt a robber. Royce could only conclude that the man had attacked him
from an instinct of self-preservation. What had led him to enter the building
was a mystery.

Royce returned to John, who during his absence had kindled a fire,
skinned the rabbit, and set it to roast. They made a good breakfast, then
started in the direction of the village where Royce hoped to purchase food
for his men.

"We must keep a good look-out," he said, "in case that fellow should be
one of a band prowling about here. He won't be difficult to recognise. There
can't be many men of his height and size. And if there were, I should know
him again by some strange marks on his face. Why do these black men gash
themselves, John?"

"To make him look pretty, sah."

"Um! They've a queer notion of beauty, then."

Anxious to accomplish his errand and return to Challis, Royce pushed on


as rapidly as possible. The country was pathless, for the most part flat, with
undulations here and there, covered with thick bush varied by an occasional
gum-tree. Drysdale's rough sketch-map gave him little more than a bare
direction, and he had to trust a good deal to luck. After three hours' steady
marching, which ought to have brought them to the village, if the estimate of
its distance were correct, they were still in the same wild, barren country,
without a sign of mankind. It seemed probable that they had overshot the
mark, so, after taking a short rest, they altered their direction in the hope of
discovering a path.

It was late in the afternoon, and they were very tired, when at last they
struck into a narrow, beaten track, far to the left of their original course.

"This looks promising," said Royce. "We'll make a spurt, John."


Another half-hour brought them to cultivated fields. Crossing these, they
found themselves faced by a mud wall, fourteen or fifteen feet high, pierced
by a single gate. This was closed.

"It's a town, after all," said Royce; "not a village. So much the better, if
the people are friendly. Give them a call, John."
JOHN ADDRESSES THE SENTRY
The Hausa let out his long yodelling cry. A head was seen peering over
the top of the wall. John lifted both hands, and spread them, palms open, as a
sign of friendliness; then began an address in the native tongue, somewhat as
follows:

"Ho, you admirable watchman of a noble chief! Tell your kind and
worthy lord that a stranger from the lands of the Great White King seeks to
look upon his face, and have a friendly talk with him. My lord and master is
a very great man, with horses and cattle in number as the grains in a
cornfield, and it is a great honour that he does to your chief. Open, then,
your gate, and let this great lord enter; and, as he passes, let your people fall
to the ground, and throw earth upon their bodies, as befits folk who are but
as worms in his sight."

Unconscious of the extravagant claims made on his behalf, Royce leant


on his rifle, waiting. The head disappeared. Some minutes elapsed; then the
watchman came back to his perch, and a long conversation ensued between
him and the Hausa, who grew more and more excited, and raised his voice
until it became almost a yell.

"What is it all about?" asked Royce, beginning to suspect that his man
was growing impolite.

"Son of a dog, dat fella!" replied John indignantly. "He want to know too
much. Talk 'bout sah's father and mother, how many wives, what he come
for, too much!"

"Did you tell him I want to buy food?"

"No, sah; I tell him——"

"Then do so at once," Royce interrupted sternly.

In a more subdued tone of voice, John gave his message. The man again
withdrew.

After another interval, the gate was thrown open, and Royce, entering,
found himself among a band of stalwart natives, carrying long, broad-bladed
spears, and marked on each cheek, near the ear, with five or six narrow cuts,
the badge of their tribe. Escorted by them, and watched by a curious crowd
of townsfolk, Royce proceeded to the chief's large mud house in the centre
of the town.

On entering he was greeted with the words: "Sanu, bature!" (Hail, white
man!) from a stout, pleasant-faced, bearded black man, somewhat past
middle age, who motioned to him to be seated, and ordered one of his slaves
to bring forward a present of a roasted fowl.

The customary salutations passed between host and visitor. Royce had
already learnt to endure this lengthy ceremony with patience. It was
something like this:

John: "Hail, chief! Is it well with you?"

Chief: "It is well."

John: "Allah be praised! Is it well with your wives?"

Chief: "It is well."

John: "Allah be praised! Is it well with your children?"

Chief: "It is well."

John: "Allah be praised! And your horses?"

Chief: "It is well."

John: "Allah be praised! And your cattle?"

Chief: "They are well."

John: "Allah be praised! Your house and all that is yours?"

Chief: "All is well."

John: "Allah be praised!"


And then they got to business. The chief apologised for the delay in
opening the gate. His watchmen had to be careful, because it was reported
that Tubus were in the neighbourhood. A few days before a fugitive had
come in from the westward, and told how his village had been raided and
destroyed. From the chief's description, Royce gathered that this was the
village which he had himself seen, and near which Challis was encamped.

Royce then explained the object of his visit, instructing John to translate
exactly what he said, without adding or subtracting anything. The chief at
once agreed to sell a quantity of millet and manioc for the men, and to
present Royce with a few dozen eggs—an offer which Royce gracefully
accepted, though he knew that most of the eggs would turn out to be bad.
The African native can never understand the white man's squeamishness in
the matter of addled eggs.

The next question was about the transportation of the food to the camp,
nearly twenty miles distant. At first the chief, for fear of the Tubus, was
reluctant to supply carriers. But when Royce explained that there had been
no sign of Tubus on the way, and that the area of their depredations appeared
to be considerably to the west, he yielded, and gave orders for the food to be
loaded into calabashes, and for a dozen slaves to be ready to start with them
in the morning.

It was clearly too late for a start to be made that evening, though Royce
was very anxious to get back to his friend. He accepted the chief's hospitality
for the night, and, though very tired, kept up a tedious conversation with him
through John. In the course of this he related the incident of the early
morning.

The chief seemed amused at the thought of a wrestling match between an


Englishman and a negro, and laughed heartily at the negro running away on
hearing John's cry, "Yoi-aloo!" which he made the Hausa repeat again and
again. But his amusement soon gave place to alarm, his smiles to a look of
thoughtfulness. He had asked Royce to describe the negro. He seemed little
impressed by details of the man's height and size, but when Royce
mentioned that he had noticed two straight cuts down the middle of each
cheek he uttered a sudden, sharp exclamation.
"Goruba!"

"What does he mean by Goruba?" asked Royce of John, after a brief


silence.

The chief seemed to deliberate whether he should speak or not. At last he


said:

"I will tell the white man—my friend. Years ago I was chief of only a
small village, and lord of little wealth. And I sought to increase my wealth
by prudent trading, to which end I hunted the elephant, and sold his tusks to
merchants from the East. And one time, having got together some few tusks
in readiness for the barter, I was beset in my village by a horde of strange
warriors, armed with guns, a terror to all my people. And these evil-doers
came to me and made me captive, and demanded that I should deliver up to
them those few tusks which were the spoil of my hunting. And, when I
refused, they treated me shamefully and cruelly, so that I bear the marks on
my body to this day. And there was no help for me, no hope of deliverance;
and then, for the safety of my life, I was fain to yield, and saw myself robbed
of the treasure that had fallen to my spear.

"And the captain of those wretches, he that put me to the torture, was a
man of vast stature and the strength of a giant, Goruba by name. He was
from the east country, the slave of Rabeh, King of Dar Runga, who laid
waste all the lands on this side of the great lake, and whose warriors were as
locusts on the face of the earth.

"It was a good day for all this country when Rabeh was slain, and his men
were scattered to the winds. I knew not what had become of Goruba, one of
his chiefest captains; but in very truth it was he that laid hands on you, for
his stature and those marks upon his cheeks betray him. And this news that
you give me is heavy upon my heart, for without doubt Goruba is again
prowling like a lion about these lands, and many a village will fall a prey to
him."

Royce did his best to reassure the chief, pointing out that Goruba had
been alone, and there was no evidence that he was the leader of any
considerable body of men. This somewhat heartened the old man, who
declared, however, that he would henceforth be doubly watchful, and
advised the white man to leave the country as soon as possible.

"I shall go at my own time," said Royce quietly. "I thank you for your
warning, and will do what I can to repay your kindness."

Next morning he left the village with John and a dozen carriers, well
loaded with food-stuffs, and hastened at his best pace to rejoin his friend.

CHAPTER IV

RUSHED BY TUBUS

Challis, after Royce had left him, felt somewhat uneasy. Royce had
spoken hopefully of getting back on the following day, but Challis,
remembering the difficulties of finding the way in a strange country, was
pretty sure that Royce had overestimated his powers, and was prepared to be
left in sole charge for at least two days, and perhaps three. It was not a
pleasant prospect, for the raiders could not be very far away, and the
possibility of being attacked was disturbing. He kept a smiling face,
however, and said nothing about his anxieties to the boys.

The first thing to do was to form as secure a camp as possible. The


camping arrangements had been interrupted by the discussion leading to
Royce's departure. As soon as Royce was gone, Challis took the matter in
hand.

The spot which Royce had suggested as suitable for the camp was a
knoll, on which a few palm-trees grew, at one end of the village, and Challis
was rather surprised to find that, during his conversation with Royce, the
Hausas had not made a start. Wearing woe-begone expressions on their
faces, they were squatting beside their packages.
"Now, Kulana," said Challis to the second in command, "what are you all
idling for? Carry the stuff to those palm-trees yonder."

The men got up obediently, but they appeared to have a strange hesitation
in lifting their loads.

"Come, come!" said Challis. "I know that you are done up, but you are
not so bad as all that. Besides, it's only a few yards away, and the sooner we
form camp the sooner you'll get to sleep."

There was a murmuring among them. Still they did not offer to lift the
bundles.

Challis curbed his rising anger. The men were usually so willing that he
wished not to be hard on them. And both Royce and he had already taken
much interest in studying the queer moods and ideas of these Africans.

"What is troubling them, Kulana?" he asked quietly.

The Hausa looked him full in the face, and, seeing no anger there,
replied:

"No want camp dar, sah—berry bad place."

"Oh! Why is that?"

And then the man began to pour out an explanation of which at first
Challis could make nothing. Kulana's English was not adequate to express
his thoughts. He talked of "debbils" and "bad medicine," and went through a
pantomime of gestures expressing fear and fright, the other men listening
intently, and murmuring approval now and then.

After a time, however, Challis had a glimmering of light.

"You think the spirits of the dead men haunt the village—is that it? And
you are afraid they will come and bother you during the night?"

"Sah savvy all 'bout it," said the man, delighted that his meaning was
understood.
The others clapped their hands, and looked much more cheerful.

"Well, I don't want you to see black ghosts, I'm sure," Challis went on. "I
don't know that I'd like to see them myself. We'll find another place."

The men shouted, and springing to their feet, hoisted their loads with
alacrity. Challis reflected that the work of constructing a camp more in the
open would be good for them, preventing them from brooding over their
troubles, real and imaginary.

After a rapid glance around, he led them to an open spot at some little
distance from the village, and ordered them to make a small zariba with
branches from the bushes around. It was an excellent situation for a camp.
Its openness rendered it possible to detect the approach of an enemy, and a
small stream running close by furnished an ample supply of water.

As he had expected, the work of cutting the bushes took the men's minds
from their misfortunes. They soon began to chatter with their usual
cheerfulness. By nightfall they had constructed a thick fence six feet high
and several yards square. The tent was erected in the middle of the
enclosure, the baggage was placed against one of the walls, and the men,
tired out, but no longer despondent, were ready for their supper.

Challis dealt out pills to the ailing ones, promised them all a long rest on
the following day, and spoke confidently of the speedy return of his
companion with ample supplies. He arranged for the watching of the camp
during the night, and, as an extra precaution, set his alarm clock, when he
himself turned in, so that it wakened him in an hour. Then he took a look
round, set the clock again for an hour ahead, and so on, through the night.
He enjoyed the inestimable power of sleeping at any minute.

The night passed quietly. In the morning, Challis sent two of the men
outside the zariba to fetch water and to take a look round.

When they returned they were rather excited. They explained that they
had made two discoveries. One was that across the stream lay an extensive
swamp, upon which they had seen a large flock of birds pecking for worms.
The other was that a little farther off was a banana plantation, which had
escaped the ravages of the raiders.

"Good boys!" said Challis. "We will make some use of your news by and
by."

The presence of the birds and the bananas afforded a welcome


opportunity of replenishing their scanty larder. Challis decided to go out and
try his skill as a sportsman. There would be some risk, of course, that the
noise of his gun might attract the notice of undesirable visitors; but there was
very little food of any kind left, and poultry would be a treat to the men. In
view of the possibility of Royce's return being delayed, it seemed too good a
chance to be neglected. Besides, he reflected, there was no other village in
the neighbourhood, and the raiders, having accomplished their destructive
work only too completely, were probably by this time far away. After he had
made a bag of birds, he would send out some men to gather bananas, and the
party would revel in a regular banquet.

Accordingly, he ordered Kulana to keep a careful watch, and on no


account to allow the men to move outside the zariba; then, taking his shot-
gun, he set out for the swamp, with the two who had brought the
information, to retrieve the game.

The swamp was half-a-mile away, and Challis found it necessary to walk
round it for some distance before he could get a fair shot at the birds, which
had settled down in a bed of rushes. They appeared to be a variety of pigeon,
a delicacy which the men would appreciate after living almost wholly on
meal.

When he had located the game, he sent his men forward to start them.
The instant they rose in the air he let fly with both barrels in rapid
succession, and brought down a bird with each shot. Following up the rest of
the covey until they settled again, he killed another brace, and so went on for
nearly an hour, until there were as many birds as the men could carry.

His success had quite banished the misgivings with which he had started.
Picturing the delight of his boys at getting a whole day's good eating, he had
given the order to return, and was half-way back to camp, when he was
startled by a great shouting and commotion from its direction.

Sprinting on at full speed, he was just in time to see a number of his boys
running across the open space in front of the zariba, closely pursued by a
dozen horsemen. The runners, who appeared to be laden with bananas,
darted into the enclosure through the gap they had left in the fence; but
before they could draw the rough gate across it, the foremost horsemen
galloped through at their heels.

Challis guessed in a moment what had happened. The men's appetite had
been whetted by the report of the nearness of a banana plantation. They had
been too childishly impatient to await his return, and, either with Kulana's
consent or in defiance of him, they had gone out to gather the fruit, only to
be surprised by some wandering tribe.

He rushed impetuously across the open space to the support of his men,
not staying to reflect that he could do nothing really effective. Besides his
shot-gun, he carried a revolver. The horsemen had fire-arms, which they had
not used as yet, feeling no doubt that their swords were sufficient for the
work in hand. Some of the Hausas had rifles, but it was clear that they were
too much paralysed by the appearance of a mounted enemy to make use of
them. They were yelling with fright.

Before Challis was half-way to the zariba, a second party of horsemen


broke from cover in the direction of the village, and rode straight at him.
Flinging up his shot-gun, he emptied the barrels almost at random; then drew
his revolver. But at that moment a shot from the pistol of one of the
galloping horsemen struck him above the wrist, and the revolver fell to the
ground. In another second he was spun round by the impact of a horse's
shoulder, reeled, and fell. The horsemen galloped over him on their way to
the zariba, and before he lost consciousness it seemed to him that he had
been kicked and trampled by a hundred hoofs.
CHAPTER V

UNDER THE LASH

Challis opened dazed eyes upon a scene that bewildered him, and for a
few moments he could not account for the pains that gripped all his limbs.
Remembrance stole back into his reviving consciousness, and gradually he
became aware of the meaning of what he saw.

The zariba had been demolished. At one side of what had been his camp a
number of horses were tethered. In the centre his Hausa boys were busily
packing the baggage, much more rapidly than Challis had ever seen them
doing it before. The reason of their haste was easily discovered. Over them
stood a circle of negroes, who urged them with fierce cries and drawn
swords. The camp had fallen into the hands of an enemy.

And it was not long before Challis guessed who this enemy was. Only
one tribe in this part of Africa, so far as he knew, rode horses. These men
must be Tubus from across the Yo—the ruthless brigands who were the
terror of the country. It could hardly be doubted that these were the men who
had raided the village, and left only too clear proofs of their merciless
ferocity.

Apparently there had been no fight at the zariba. The Hausas, armed
though they were, had succumbed without a struggle. The truth was that, in
disobedience to Challis's order, some of the men had left the camp, and been
pounced on suddenly by the enemy. As they fled back to the zariba, their
comrades dared not fire for fear of hitting them, and the swift onset of the
horsemen had made resistance hopeless.

Aching all over, Challis struggled to his feet. Immediately a hand was
laid upon him from behind. He noticed that his wrist was bleeding, and
taking out his handkerchief, he began to wind it round the wound as his
captor pushed him towards the centre of the camp. And then from behind the
horses there came forth a huge negro, taller than he by six or seven inches,
with massive shoulders and muscular arms.
The giant's face broke into a grin as he approached the Englishman. He
uttered some words which Challis did not understand, but which seemed to
have in them the ring of triumph.

"I have caught you, white face!" was what he said.

As to the white man all negroes seem at first alike, so to Goruba Challis
at this moment appeared to be the man with whom he had wrestled at dawn
of day.

He spoke again, addressing Challis; then, recognising that he was not


understood, he called for one of the Hausas to come and interpret. Kulana
came up, and keeping his eyes averted from Challis, he translated what the
big man said.

"What are you doing in this country?"

Challis quickly made up his mind to give no information, trusting that he


might at least save Royce from his own fate. He refused to speak. The next
question puzzled him, but confirmed him in his resolution.

"What were you doing in my fort?"

Though he made no reply, it was plain that he showed his surprise in the
expression of his face, for Goruba looked hard at him, and seemed to be in
some puzzlement himself. Then the negro's harsh features darkened with
anger. He flourished his sword.

"Dog! This will make you speak!" he shouted.

Challis looked at him, without quailing, and did not flinch when the
sword was flashed across his eyes. His courage seemed to impress Goruba,
who laughed, spat on the ground, and giving an order to his men to keep
guard over the prisoner, walked away with Kulana into the midst of the
sweating Hausas.

What he had failed to elicit from the Englishman his threats soon
extracted from the carriers. He learnt that there was another white man, who
had set out on the previous afternoon for a village to the north, to buy
provisions. He chuckled on receiving this information. There was little doubt
what village the white man he had met in the fort was bound for. He
chuckled again. The white man was no doubt on his way back to the camp. It
would not be difficult to waylay him.

The work of packing was completed. Goruba ordered the Hausas to


mount their loads. Their rifles, with Challis's, were in the possession of his
men. When all was ready, the Tubus leapt to their saddles, and the whole
party set off northwards, Challis being tied to the saddle of one of the horses.

Often on that march Challis's blood boiled as he saw how his men were
treated, and knew his helplessness to defend them. The Tubus urged them
with whips, sometimes with the points of their swords. The wretched
Hausas, some of whom were weak with sickness, panted along under their
loads, striving to keep pace with the impatient horsemen. They dared not
even groan, for a murmur brought the lash on their shoulders. When Challis
protested through Kulana, explaining that the men were ill, Goruba only
grinned and mocked him.

By-and-by, however, it became apparent to Goruba that the men were


incapable of further marching. The slave-driver is usually callous enough as
to the fate of his victims; he will watch them with unconcern growing
weaker and weaker, see them drop in their tracks, sometimes kill them in
sheer rage at their inability to keep up. But Goruba did not wish to lose these
men. They were themselves valuable. They bore valuable loads. It would be
a mistake to over-drive them. In the afternoon, therefore, some hours earlier
than a march is usually ended, he gave the order to halt. The Hausas laid
down their burdens, and threw themselves on the ground in utter exhaustion.
THE PRISONER

Challis himself was in little better case. He had not been given a load to
carry, but he had felt himself growing weaker and weaker as the day wore
on. Though his wound was not serious, he had lost some blood, and was
enfeebled by the shock and the bruises he had suffered in the trampling.
When he lagged on the march, the man to whose saddle he was fastened
prodded him in the back with the point of his spear. His own sufferings, and
the sufferings of his men, made him realise with new force the horrors of
slave-driving, which, in spite of all efforts to crush it, still exists in parts of
the dark continent.

It was therefore with inexpressible relief that he welcomed the order to


halt. The place chosen for camp was the crest of a slight undulation. The soil
was sandy, and hot from the beating of the sun upon it all day. There were a
few scrubby bushes dotted around, but no grass. Nor was there a stream in
which the marchers could bathe their burning feet.

The Tubus fetched water from a small water-hole near by. They made a
meal of the provisions carried in their wallets. The Hausas consumed the last
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade

Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.

Let us accompany you on the journey of exploring knowledge and


personal growth!

ebookname.com

You might also like