devxlogo

Link the DataField to the Recordset

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 'ctrlEnd 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.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist