Deal With The DataControl At Run Time And Design Time

One of the greatest features of data-aware controls is that you simplyconnect them to a DataControl and select the fields you need. You havejust created a database program. It can’t get any easier than that. Theonly problem is that if you share this program, nine times out of 10 itwon’t work. Why? Because the database name and directory structure arehard coded into the DatabaseName field in the DataControl.Hard-coding the database name into the DataControl is a major programmingno-no. Instead, you fill this field at run time. Then, before you can ship(or even send the program out to beta testers), you must remove the databasenames from your controls. Then you add them back during design, and soon. What a headache.I don’t have time for that. I created a nice Sub that does the workfor me. Before I give it to you, I need to review a few idiosyncrasiesof loading a DataConrol. With my method, you leave the original databasenames in the DataControl. The problem with this is that as soon as a formloads, it initializes the DataControl. It tries to load the database andrecord set and causes an error. To prevent this, set the Enabled propertyof the DataControl to false. Use this code in each form that uses a DataConrol:

 Private Sub Form_Initialize()	SetDataDBName frmIn:=ME, sDBName:="C:MYPROGDATAMYDB.MDB"End Sub'Calls the following Sub:Public Sub SetDataDBName(frmIn As Form, sDBName As String)	Dim I As Integer	'On Error needed to prevent problems	'with refreshing bad values in the 	'RecordSource property	On Error Resume Next	'Search through form's controls	For I = 0 To frmIn.Controls.Count - 1		'See if the control is a datacontrol			If TypeOf frmIn.Controls(I) Is Data Then			frmIn.Controls(I).DatabaseName = sDBName			frmIn.Controls(I).Enabled = True		'Refresh if there is something 		'in the recordsource property			If Len(frmIn.Controls(I).RecordSource) _				> 0 Then				frmIn.Controls(I).Refresh			End If		End If	Next IEnd Sub

That’s it, easy and painless!

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: