Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Visual Basic
Expertise: Beginner
Apr 7, 2000



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

Link the DataField to the Recordset

The data control allows you to quickly link controls and databases; however, not only is it far from flexible compared with the database-objects coding interface, it doesn't look great. I use the latter solution and assume most VB programmers do. But it's quite painful to add or alter several lines of code every time you put a new text field on a dialog. It's a waste of the DataField property, however; it can be just as useful as the Tag property, and in this case, it's more descriptive. So what if you write a few routines to simulate the data control's basic operations through the DataField property? This simple routine loads data from a Recordset to all controls on a form:
Public Sub ReadData(frm As Form, rc as Recordset)
	Dim ctrl As Control
	' Need to ignore errors on controls
	' that don't support databinding.
	On Error Resume Next
	For Each ctrl In frm.Controls
		If ctrl.DataField <> "" Then
			ctrl = rc.Fields(ctrl.DataField)
		End If
	Next 'ctrl
End Sub
You only need to put the field name into the DataField property of involved controls at design time. By altering such routines, you can achieve more complex data handling than if you use data controls, as well as make reading, writing, and validation a lot simpler than doing everything manually.
Martin Girard
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