Programming becomes basic
with Microsoft's new tool
Learning to program has always seemed like learning to read The Iliad in Greek: It's a really noble idea that just isn't worth the effort for most of us.
Oh, sure, once upon a time many of us wrote little programs in UNIX, DOS, batch and scripting languages. But a project of any complexity required a devotion to the language that precluded other work -- such as putting out a newspaper.
Things got even worse as users moved to graphical user interfaces such as Microsoft's Windows or Apple's System 7. Now a programmer needed to pound out hundreds of lines of code just to draw the interface, before getting to the part where the software does any work.
Consider, for example, a simple program we use in the Waterbury (Conn.) Republican-American newsroom: A database front-end that allows users to search from within their word processor, WordPerfect for Windows. Here's the part of the code that does the heavy lifting (stay awake, now):
DialogDisplay (Box;1)
If (MacroDialogResult = 1)
SearchText
(
SearchString:SearchName;
SearchDirection:Forward!;
SearchScope:Extended!
)
That's pretty straightforward. The first three lines tell the program to initiate a search if the user clicks "OK." The parenthetical three lines tell the program to get the variable the user typed in (SearchName), and search the whole file until it is found.
Short and sweet, right? Well, not quite. Here's the code to draw the box:
Title:="Search Census Data"
Box:=1
LeftEdge:=5
TextWidth:=50
TextHeight:=8
EditWidth:=183
EditHeight:=12
MaxChars:=45
VPos:=5
Spacing:=5
SearchName:=""
Box:="MyBox"
DialogDefine (
Box;
50;
50;
200;
100;
19;
Title
)
DialogAddText (
Box;
"Text1";
LeftEdge;
VPos;
TextWidth;
TextHeight;
1;
"Name?"
)
VPos:= VPos + TextHeight + Spacing
DialogAddEditBox (
Box;
"Name";
LeftEdge;
VPos;
EditWidth;
EditHeight;
1;
SearchName;
MaxChars
)
Not only is this a lot of code, with a lot of terms you need to understand how and when to use, this is just a simple program. It yields a single pop-up box with only one place to type in a variable, an OK button and a Cancel button.
The average program has dozens of pop-ups, each of which is orders of magnitude more complex than what's written here. Think, for example, of the ubiquitous File Open box that occurs in some form in just about every piece of software.
To create a File Open box, you must draw the box, establish an area that either shows the current filename or allows a user to type one in, an area that shows directories that is mouse navigable, an area that shows drives that is mouse navigable, an area that shows files that is mouse navigable, and then a bunch of buttons to enable the user to Open, Cancel, get Help and so on.
And then you have to link everything, so the highlighted file gets opened when the user clicks OK, the directories and files change when the user switches drives, and Help calls up help for the Open File box.
How long would it take you to do all that? That depends on how you go about it.
If you use Visual Basic, allow about 10 seconds.
Visual Basic is to most programming languages what a good word processor is to a pad and paper -- nothing short of a revolutionary change.
Need a box like the one above? Just grab a form. Need buttons? Grab the button tool and draw them. Need a drive object so your user can change drives? Grab the drive tool and draw one. Even cooler, it starts to work the second you draw it on the form. Need a directory listing? Grab the directory tool and ....
You get the picture.
Within hours of installing Visual Basic I had built a file-viewing program for use with graphics. By the third day I was writing my own word processor.
Since Visual Basic code can be reused, you can cannibalize old programs for parts for your new software. Or, you can purchase complicated routines, such as file viewers and compression utilities, to add to your programs.
For newspapers and other media organizations, probably the most useful thing about Visual Basic is the ability to construct database front-ends without any programming.
A word here on the subject of information appliances: My philosophy is that software should be designed to fit the way the user works, rather than forcing the user to learn the software.
For access to fixed data -- census numbers, for example -- we prefer to port the data into a hypertext file that can run on the Microsoft Windows help engine. This allows users to get data without ever leaving their application, while allowing the same hypertext file to run on a variety of platforms and software.
Not all databases deal with fixed data, however. Some databases are fluid -- and you want them to stay that way.
Take, for example, an organization-wide contact and phone number database. Such a database should be accessible to everyone. It should be easy to update, and easy to verify. And it should be easy for users to get the data they need.
Database software is the perfect choice for these functions, but even the friendliest is about as much fun to use as banging nails into oak with your forehead. (If you'd like to test this theory, grab the nearest database manual and read the section on key violations.)
There are lots of little phone book programs, but most of them tend to run on one machine at a time and are not very good at importing and exporting data. You could, of course, drop tens of thousands of dollars on a groupware solution such as Lotus Notes -- but that's just silly now, isn't it?
Visual Basic, on the other hand, allows you to neatly solve all these problems. Visual Basic has the Microsoft Access database engine built in, so it's good at controlling databases. You can build a program as simple to use as the little Cardfile program included with Windows -- but one that has the power and flexibility of a relational database.
Best of all, you can do it without programming. If you can work a mouse, you can build this app in Visual Basic.
The key to all this magic is the Visual Basic Data Control, which enslaves a selected database to your program. If you can play the Solitaire game that comes with Windows, you're ready to write a Visual Basic database front-end.
Instant App
Let's build a Visual Basic database front-end for a contact and phone number database.
To keep this simple, we'll assume we already have data in a Microsoft Access database. (Since Visual Basic has the Access engine built in, we won't have to import the data. And importing the data into a database is simple enough -- but outside the scope of this article.)
Visual Basic boots with a blank form in place, ready for you to draw the main interface of your program.
Go to the tool palette to the left, click on the Data Control -- it's the one with the opposing arrows and the two descending data fields just below dead center on the middle column of tools -- and draw a Data Control on the form.
Next, click on the properties palette, make sure the Data Control is selected at the top of the palette, and type in the name of your database where it says DatabaseName.
That binds the database to the form -- and you're pretty much done. Now just draw in a text box wherever you want data to appear on the form, then draw in a label next to it. For example, in a phone book you are probably going to want the first field used to be Last Name.
Draw the text box with your mouse by selecting the boxed "ab" on the tool palette, which is second from the top in the left column, then dragging your mouse on the form. Click on the properties palette, make sure Text1 is selected, and scroll down to DataField. Clicking in the box will pull up a list of all the field names from your database. Click on Last Name.
Next, draw a label by selecting the large "A" in the upper right corner of the tool palette, click over to the properties palette, make sure Label1 is selected, scroll down to Caption and type in Last Name.
And that's it. When you click on the right single arrow of the Data Control, the last names in your data base will page through your text box one by one. Click on the arrow with the end line, and jump to the end. Click on the single left arrow, and you page back name by name. Click on the left arrow with the end line to jump to the beginning.
Obviously, you'll want a couple more fields to show in your program -- First Name and Phone Number spring to mind -- but you add them simply by repeating the steps you used to create the Last Name field and labels.
Getting fancy
Visual Basic's simplicity doesn't rule out getting really fancy. You can buy some fairly elaborate software that was written with this stuff, and you can certainly dress up our little project.
One nice feature for our program would be the ability to search the database. Little phone book programs like the Windows Cardfile let you do single searches -- find the first "Smith," for example -- but we, of course, want better than that.
Since that's a real database underneath our little front-end, we can actually run queries, such as "Go fetch all the records that have Politics in the Expertise field." This, however, takes a bit of actual coding.
To get this to work you must first create a way for the user to interact with the program. You can do this by grabbing the button tool and drawing a button on your form. Double clicking on the button brings up the event code -- event is programmer talk for something occurring, in this case a user clicking on the button.
Suffice to say that the process gets a bit complicated at this point, since you've moved into production of database code as well as Visual Basic code. Fortunately, Microsoft packages with Visual Basic Professional an entire set of sample applications, including a fairly involved database front-end -- and most of the code is recyclable.
If you do decide to build in a query function, I would advise using a Like statement rather than an exact match, and also ignoring case. This will keep your users from spending half the night asking for Brown while the stubborn app ignores them because it can find only Browne.
But even if you don't decide to get that fancy, you can build a perfectly serviceable database front-end in a couple of minutes, most of which is spent drawing with the mouse. Frankly, it's easier to draw up a new database application in Visual Basic than it is to draw up a page in Quark XPress, a feat accomplished daily in newsrooms across the land.
Even better, you can recycle these database applications over and over again. Once you've got one the way you like it, you can simply copy it, open the properties palette, change the database you're calling and save. (You'll also have to update your field names and labels, but the pop-up lists for these will change once the new database is selected.)
The regular version of Visual Basic tops $100, while the professional version runs around $500. Get the professional version -- it comes with extra tools and controls, and it will pay for itself the first couple of times your reporters and editors pull their own data out of a database.
Or, you can pay your lone database guru to do it ... on overtime.
For more information on Visual Basic, consider subscribing to Visual Basic Programmers Journal,
(415) 917-7650.
-- Christopher J. Feola
|