Understanding VBA

Visual Basic is a programming language — a language for writing instructions that a computer can read and process. VBA is a programming language that's specifically designed to work with the application programs in Microsoft Office including Word, Excel, Outlook, and of course, Access.

When you write text in a programming language (as opposed to writing in plan English), you're writing code. Programmers use the term code to refer to anything that's written in a computer programming language. For example, Figure 1-2 shows some sample VBA code. The whole trick to learning VBA is learning what all the various words in the language mean so that you can write code that tells Access exactly how to perform some task.

Figure 1-2:

Some sample VBA code.

Figure 1-2:

Some sample VBA code.

Public Function PCase(anyText)

'Custom Access VBA function to fix all

uppercase letters.

PCase = StrConv(anyText, vbProperCase)

PCase = "P.O." £ Mid(PCase, 5) End If

PCase = "He" £ UCase(Mid(PCase, 3, End If

1)) £ Mid(PCase, 4)

PCase = "Mac" £ UCase(Mid(PCase, 1 End If

1)) £ Mid(PCase, 5)

End Function

If the sample code shown in Figure 1-2 looks like meaningless gibberish to you, don't worry about it. People aren't born knowing how to read and write VBA code. Programming (writing code) is a skill you have to learn. For now, it's sufficient just to know what code looks like. Knowing what the code means is one of the skills you'll master in this book.

Because VBA code looks like a bunch of meaningless gibberish typed onto a sheet of paper, this begs the question of why anybody would want to learn to read and write some dreadful language like that. The answer to that question lies in the role played by VBA in an application like an Access database.

Do, not die

Think of the term execute in the sense of to procedure. Don't think of execute in the sense carry out, as in execute a U-turn or execute the of terminate the life of.

The ability to use the same code over and over again is key to automating mundane tasks in Access. For example, if you used Access to print checks, you might have to manually type the part of the check where you type the amount in words, like Ninety-two and 99/100 Dollars for $92.99 because Access can't do that translation on its own. But if you could write some code to translate a number like $92.99 into words, you wouldn't need to type all those dollar amounts. Access would just print the correct information as it prints each check.

Access does indeed have a ton of tools that let you create a database without any programming at all. You could easily spend months or years just learning all the things you can do in Access without writing any VBA code. Yet despite the huge number of things you can do without programming, sometimes you will want your database to accomplish some task that's not built into Access. That's where VBA comes in. When you want Access to perform a task that it doesn't already know how to perform, you write the steps to be performed in the VBA programming language.

When you're writing VBA code or just looking at some VBA code written by someone else, Access doesn't do anything. Access doesn't actually perform the steps described by that code until Access executes the code. When you write VBA code, you're actually writing a set of instructions that Access can perform at any time, over and over again.

0 0

Post a comment