Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Visual Basic
Expertise: Beginner
Aug 6, 1997



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Deal With The DataControl At Run Time And Design Time

One of the greatest features of data-aware controls is that you simply connect them to a DataControl and select the fields you need. You have just created a database program. It can't get any easier than that. The only problem is that if you share this program, nine times out of 10 it won't work. Why? Because the database name and directory structure are hard coded into the DatabaseName field in the DataControl. Hard-coding the database name into the DataControl is a major programming no-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 database names from your controls. Then you add them back during design, and so on. What a headache. I don't have time for that. I created a nice Sub that does the work for me. Before I give it to you, I need to review a few idiosyncrasies of loading a DataConrol. With my method, you leave the original database names in the DataControl. The problem with this is that as soon as a form loads, it initializes the DataControl. It tries to load the database and record set and causes an error. To prevent this, set the Enabled property of the DataControl to false. Use this code in each form that uses a DataConrol:
 Private Sub Form_Initialize()
	SetDataDBName frmIn:=ME, sDBName:="C:\MYPROG\DATA\MYDB.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
			End If
		End If
	Next I
End Sub
That's it, easy and painless!
David McCarter
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date