Visual Basic Database Tutorial - Part 1
Don't tell me, your boss has asked you to develop a supercool database system and handed you a deadline of yesterday.
You don't want to hear me babble about database theory - you need to get stuck in! Well, I can take a hint so let's get down and dirty, and develop our first Visual Basic database-integrated application with absolutely no code at all!
1. Start Visual Basic if youre unsure about this, check out my Visual Basic tutorial here.
2. Choose the "Standard EXE" option and click OK
3. A regular blank form should appear on your screen. Double-click on the data control in your Toolbox the one that looks like this . It should appear on your form.
4. Change the Name property of the data control to "datCustomers".
5. Click on the ellipsis next to the DatabaseName property of the data control and select the "Nwind.mdb" file. Mine is located at C:\Program Files\Microsoft Visual Basic\VB98\Nwind.mdb though yours may be different. This is a sample database file that is distributed with Visual Basic!
6. Move a little further down in the Properties window and click on the RecordSource button. After a few seconds, a list will appear these are all the tables and queries in your database. The one entitled "Customers" is a table. Click it!
7. Now add three text boxes to your form name them txtCompanyName, txtContactName and txtPostalCode respectively.
8. Change the DataSource property for each of the text boxes to the name of your data control (probably datCustomers, if you've been good!)
9. For each of the separate text boxes, click the DataField property you will see a list of words appear. These are all the fields within the 'Customers' database table. Change this property for each of your three text boxes as so:
- txtCompanyName - CompanyName
- txtContactName - ContactName
- txtPostcalCode - PostalCode
10. Now hit F5 to run your program!
Congratulations! Youve just created your very first database application!
Try clicking those buttons on the data control. They'll move you backwards and forwards among the rows in that Customers table.
If you're likening this to an Excel spreadsheet, you can imagine that with each click you're moving down or up one row and displaying all the customer information on that line.
Try changing one of the company names and moving forward a few records then moving back. You should notice that your changes have been saved!
That's what a database table is all about. They allow you to add "rows" of information to a table, edit stuff currently sitting in the table, remove entire rows or bunches of rows from a table even find rows in a table!
Top Tip: Instead of using the word "row" to describe a line of information in a table, try using the term "record" instead. Dont ask why, just trust me I'm a programmer.
Page 3 of 5