1. Technology

Your suggestion is on its way!

An email with a link to:

http://visualbasic.about.com/library/courses/blecvbai0404.htm

was emailed to:

Thanks for sharing About.com with others!

Adding to the System
4 - Event Filled Programming
 Lesson 4
Adding to the System

1 - Making Sure We Understand The Problem (Again)

2 - The Parts Are Greater Than The Whole

3 - Good Form in Programming

4 - Event Filled Programming

5 - Where To From Here?


What does it mean when we say that Visual Basic an "event driven" language? One way to understand this is to look back at the way things used to be (and sometimes, still are) using purely procedural languages.

The original Basic language let you write programs that started, did something, and quit again. So, for example, if you were going to update a customer accounts file with the day's business, a program could start, open a file or database with customer accounts and another one with transactions for the day's business, match transactions against accounts and perform the update, then close the files again: Job Done!

Certainly, that kind of processing is still done, but in the online, realtime era of computing today, it's much more common to see systems that update a customer account just as soon as the business is done! Waiting until the end of the day is just not good enough. For that kind of performance, you have to have systems that start a program to perform the update as the result of an event - in this case, probably when someone clicks a button that says, "Buy This Item!"

So ... event driven programs are already started and are just sitting in a computer's memory waiting for an event that they recognize as 'their job' to happen. When it does, they spring into action and do it, then go back to waiting for the next event. How all this magic happens is a deeper level of programming that involves the way Visual Basic and other event driven languages interact with the Operating System. For now, it's enough to recognize that it does happen and that there is a whole universe of events that you, as a programmer, can use in your code.

The event that we're going to use in our Form Letter program to kick of the substitution of text into the document is the Click event of a CommandButton object.

Make sure your Form Letter document and the Visual Basic Editor are still open. You should have the UserForm you created in the previous segment visible.

(Note: Either the program code or the Form is visible in the Visual Basic editor. I find that the easiest way to switch between them is to right click on the UserForm in the Project Explorer and select either View Code or View Object.)

Now double click the CommandButton object on the Form. This should open the code window with your event subroutine already started for you.

Code Window

In Visual Basic, event procedures are named in a very specific way to tie objects and events together. When a particular object raises a particular event, Visual Basic looks for a subroutine with the standardized name ObjectName_EventName. If it finds one, that subroutine is started.

Click the down arrows on both the Procedure Box and the Object Box and see what else is available. For example, if I wanted something to be done after updating the TextBox txtGreatWork, then I would write some code in the txtGreatWork_AfterUpdate() subroutine.

What code should we write in our event subroutine? In fact, most of it has already been written and it's in the AboutFormLetter subroutine in Module1. Using the 'looping code' that was written in Lesson 3 (primarily because it makes a better illustration!), we have to make exactly one change to get the program to use the value from the TextBox rather than being 'hard coded':

Replace the statement:

NewDoc = NewDoc & "Mr. Publisher Dude"

With the statement:

NewDoc = NewDoc & txtAddrName.Text

(Note: For a more complete program, the result of the text substitution also has to replace the entire current document. This statement just after the Next Searchword statement will do the trick: ActiveDocument.Content = NewDoc)

So far, you've covered quite a bit of territory in Visual Basic programming. This introduction is a long way from being comprehensive and complete, but using the ideas here, you should be able to explore on your own and develop new knowledge. Some suggested ways to go about doing that are in the next section:

5 - Where To From Here?

©2014 About.com. All rights reserved.