DataEnvironment Controll
Contents:
• Download and Run Example Program 7
• Build Example Program 7 from Scrach
o Database Design
o Software Design
This program is yet another swing on the ADO stuff. This time we're using the
*ahem* "recommended" database access mechanism for hitting Access Databases
from VB. This took a while to figure out, actually, and I'd like to credit Shaun upstairs
here for helping out on this one quite a bit (he's the one that found it and pointed it
out to me!!). :)
One of the possible disadvantages of this method is that you can't use the app.path
property to specify the install directory of your application (although I bet there is a
way). But you can just specify a filename for your database without a path and as
long as you never change the path that your application is looking at (with a
Common Dialog or something) you should be safe. Anyways, this is a cute example
and actually works quite well.
With that going, you can push the play button (center of the top tool bar) and see
what it looks like. There are a couple of things you can do with it:
• You can add CDs to the Database by entering some info in the textboxes and
pushing the "Add This Info" button
o This has changed: now, the "Add Entry" button will not enable until all
three textboxes contain text.
o The "Track Count" textbox will only accept numeric characters
o This program does not check for duplicate entries in the database. If
you do that, it'll crash. :)
• You can select a row from the grid and hit the "Remove Selected" button to
remove it permanently from the database
• The form now resizes properly. So, if you resize the form, the grid and frames
will resize appropriately (to a certain point)
The new improved CD Collector program window
Pretty amazing, eh? :) Okay, so maybe not so amazing by this stage in the game. :)
back to top!!
Software Design
13. Add two frames to the form using the tool and drawing them on the form.
o Change for one frame:
its caption to Add Entry
its (name) to fraAddEntry
o Change for the other (second) frame:
its caption to Remove Entry
its (name) to fraRemoveEntry
1.Draw the following controls in the Add Entry frame (yes, actually in the
frame):
A text box with the (name) txtArtistName
A label above that text box with the caption Artist Name
A text box with the (name) txtAlbumTitle
A label above that text box with the caption Album Title
A text box with the (name) txtTrackCount
A label above that text box with the caption Number of Tracks
A command button with the (name) cmdAddEntry and the
caption Add this info
2.Now, to the Remove Entry frame, add the following controls:
A command button with the (name) cmdRemoveEntry and the
caption Remove Selected
A label with the caption Select the entry you want to remove
and click the button:
14. In the form design window, double click the form, which should bring up the
code window with a blank Form_Load() subroutine. Here be's the code, which
looks a lot simpler than last time, but the same size-tracker variables are
defined globally:
Option Explicit
' couple'o global vars for size trackin'
Dim MinHeight As Long
Dim MinWidth As Long
' resize the happy columns to look pretty (40% for each text
column, 20% for Track)
DataGrid1.Columns(0).Width = 0.4 * DataGrid1.Width
DataGrid1.Columns(1).Width = DataGrid1.Columns(0).Width
DataGrid1.Columns(2).Width = DataGrid1.Width -
(DataGrid1.Columns(0).Width * 2) - 60
' reposition and resize the frames on the screen to fit nicely
(there was no
' science here, just did it by trial and error)
fraAddEntry.Top = (Form1.ScaleHeight / 2) + 100
fraAddEntry.Height = (Form1.ScaleHeight / 2) - 150
fraAddEntry.Width = (Form1.ScaleWidth * 0.64)
fraRemoveEntry.Height = (Form1.ScaleHeight / 2) - 150
fraRemoveEntry.Top = (Form1.ScaleHeight / 2) + 100
fraRemoveEntry.Width = (Form1.ScaleWidth * 0.36) - 100
fraRemoveEntry.Left = fraAddEntry.Width + 100
End Sub
16. Now, go back to the form design window and double click the Add this info
button. You should now have a blank cmdAddEntry_Click() subroutine. The
code is pretty much identical to the old database example, but here's what to
fill in, anyways:
Private Sub cmdAddEntry_Click()
' add a new entry to our table.
With DataEnvironment1.rsDataTable
.AddNew
!ArtistName = txtArtistName
!AlbumTitle = txtAlbumTitle
!Tracks = txtTrackCount
.Update
End With
' requery the db and re-bind the data source to the data grid
DataEnvironment1.rsDataTable.Requery
Set DataGrid1.DataSource = DataEnvironment1
Call Form_Resize
' clear the text fields once the new record is added
txtArtistName = ""
txtAlbumTitle = ""
txtTrackCount = ""
' if the key pressed was a)not a number and b) not the backspace
key,
' just erase the keystroke (it won't get processed or sent)
If (Not IsNumeric(TrackKey) And Not (KeyAscii = vbKeyBack)) Then
KeyAscii = 0
End If
End Sub