Read an Excerpt
Visual Basic6 Database Programming For Dummies
By Richard Mansfield
John Wiley & Sons
ISBN: 0-7645-0625-0Chapter One
The Big Picture: Visual Basic's Database FeaturesIn This Chapter
* Sampling Visual Basic's most important database features
* Connecting an application to a database in less than five minutes
Visual Basic 6 includes many tools to help you create, revise, manage, and otherwise deal with databases efficiently. This book shows you how easy it is to use those tools, and this chapter introduces the main tools. At the end of this chapter, you find a quick example. It should prove to you beyond any doubt that in choosing Visual Basic to do your database programming, you made a very wise choice, indeed. You connect an application to a database in 17 swift steps.
This chapter does not attempt, though, to give you all the main ideas - the terms and concepts - of database programming. Chapter 2 attempts that. So if you come across a word or two, or a concept, that's unclear in this chapter, take a look at Chapter 2 or this book's index for additional explanation and examples.
Checking Out the Database Tools in Visual Basic 6
Visual Basic 5 and earlier versions had some database facilities. But a lot was missing, too. You often had to open Access or some other database development tool to accomplish some common database programming tasks, such as designing and testing a SQL query. (Here's an example of a SQL query: SELECTTOP 5 PERCENT * FROM tbl Sales. This query means "Give me the upper five percent of sales." For a detailed description of SQL, see Chapters 18 and 19.)
Now, with VB 6, you can count on finding most everything you need right there in Visual Basic. Here are some of the highlights - the powerful database features you find in Visual Basic 6:
For extensive examples of ADO, DAO, and other Microsoft database technologies, see Chapters 14 through 16.
The Data Environment Designer also permits multiple data connections. It can handle new OLE DB data sources (along with the older ODBC sources), it can be directly bound to controls, and you can manipulate it via programming. You can find lots of examples that show off this great tool in Chapter 4.
The WebClass technique is superior to writing script for several reasons. For one thing, your coding is not mixed right in with the HTML, so you can keep it from prying eyes. The code is further concealed because it is compiled and it never leaves your server anyway. IIS applications also employ the ASP (Active Server Pages) object model, but VB automatically generates the necessary wrappers for you. Yet another victory of a VB Designer over the tedium of having to write all the programming by hand. See Chapter 13 for all the details about this designer.
Get Your Hands On This: Five Minutes Tops
The preceding section describes the main database-related features of Visual Basic, but this chapter wouldn't be complete without a hands-on example you can try. This book is filled with step-by-step examples showing you how to make good use of all the tools and technologies I describe in the preceding section. And the following example demonstrates how easy VB often makes tasks that would otherwise take days of learning and programming.
So sample this example and prove to yourself that VB is often the shortest distance between problem and solution.
In all likelihood, no faster way exists in any programming language to connect a database to an application than to use one of Visual Basic's Data Controls. To see how fast (five minutes tops) you can get data out of a database and piped into an application so the user can see it on-screen, follow these steps:
1. Start Visual Basic by clicking its icon on your desktop. Depending on how you've specified VB's startup options, you may or may not see the New Project dialog box. If you don't see this dialog box, choose File [right arrow] New Project.
2. Double-click Standard EXE in the New Project dialog box.
A typical Visual Basic project template appears, with a blank Form1 as the starting point for your work.
3. Double-click the Data Control icon in the Toolbox.
A Data Control appears on Form1.
The VB Data Control is a component on the VB Toolbox that makes it a snap to open a connection to a database (without having to write any programming). Chapter 3 offers examples that show how you work with this useful component.
If the VB Toolbox is not visible, choose View [right arrow] Toolbox.
4. Double-click the DatabaseName property in the Properties window.
The DatabaseName dialog box appears, showing the VB folder. By default, Microsoft Access-style databases (they have an .MDB extension) are listed. An .MDB file is one kind of database, and it's the style you use in this example.
If the Properties window isn't visible, press F4. Also, you should see two sample databases in your VB folder (BIBLIO.MDB and NWIND.MDB). If you don't see these files, you might have the Data Control's Connect property set to some other database type. The solution is to close the dialog box, select Access for the Connect property in the Properties window, and then repeat Step 4. The Connect property defines the type of database you want to use the Data Control with. There are various types of databases, just as there are various types of graphics files. Each type organizes data differently, so VB needs to know which kind of database you're using.
5. Double-click BIBLIO.MDB in the dialog box.
The dialog box closes, and the DatabaseName property is set to point to BIBLIO.MDB. You have connected your Data Control to the BIBLIO.MDB database. You can't just "open" a database the way you open a graphics file. It's not that simple. Between your VB application and the database stands a database engine (a set of tools, rules, and behaviors). In this example, you are connecting to the database through the Microsoft Jet engine, which is used by both VB and Access. For details on database connections and engines, see Chapter 5.
If you still don't see BIBLIO.MDB in the dialog box, locate it by clicking the Windows Start button and then choosing Find [right arrow] Files or Folders. If you still can't find it, rerun Visual Basic's setup program and agree to install all templates and samples. The BIBLIO.MDB sample database is used extensively in this book, and it's on your VB CD.
6. Stretch the Data Control so it's about 2 inches wide and position it at the bottom of Form1, as shown in Figure 1-3.
7. Double-click the TextBox icon on the Toolbox.
VB adds a TextBox to the form.
8. Stretch the TextBox so it's about the same size as the Data Control and position it near the top of Form1.
9. Click the TextBox on the form.
The TextBox is selected (and stretch tabs - tiny blue boxes - appear around it). The Properties window displays the TextBox properties. (If the Properties window isn't visible, press F4.)
10. Attach the TextBox to the Data Control by double-clicking the TextBox's Datasource property in the Properties window.
The Datasource property changes to Data1, the name of the Data Control.
11. Click the Data Control.
The Data Control's properties appear in the Properties window.
12. Click the RecordSource property in the Properties window.
13. Click the drop-down arrow icon next to the RecordSource property.
A drop-down list appears, displaying all the tables available in the Biblio database that you're connected to, as you can see in Figure 1-4. (A table is the largest category in a database. For example, a database for a CD collection might be divided into these tables: Pop, Rock, Industrial, Metal, Rap, and Techno.)
14. Click the Authors table to select it.
The recordset you chose now becomes the set of records that are displayed in the TextBox attached to this data control. A recordset is a custom list of data extracted from a database. For example, if you ask, "Let me see a list of all authors who live in Alaska," the result you get back is called a recordset. It's often a subset of all the data, though you can also request a recordset that includes all the data (every record) in a table. (See Chapter 2 for examples.)
15. Click the TextBox to select it.
16. Keep double-clicking the TextBox's DataField property in the Properties window until Author appears.
You have now specified that the Author field (of the three available fields in the Authors table) will be the one displayed in the TextBox.
17. Press F5.
Your application runs, and you see the first record in the BIBLIO.MDB database, as shown in Figure 1-5.
(Continues...)
Excerpted from Visual Basic6 Database Programming For Dummies by Richard Mansfield Excerpted by permission.
All rights reserved. No part of this excerpt may be reproduced or reprinted without permission in writing from the publisher.
Excerpts are provided by Dial-A-Book Inc. solely for the personal use of visitors to this web site.