VB Quicktakes - Binding Controls at runtime

This code binds data controls to specific fields in a ADO recordset at runtime, just make sure the array of textboxes you are binding are in the same tab order as the order of the fields.

Option Explicit

    Dim conn As New ADODB.Connection
    Dim rst As New ADODB.Recordset

Private Sub Form_Load() Dim connString As String connString = "Provider=Microsoft.Jet.OLEDB.3.51;Persist Security Info=False;Data Source=C:\My Documents\Contacts.mdb" conn.Open (connString) rst.Open "Collegues", conn, adOpenDynamic, adLockOptimistic BindControls End Sub
Private Sub BindControls() Dim i As Integer For i = 0 To txtBox().Count - 1 Set txtBox(i).DataSource = rst txtBox(i).DataField = rst.Fields(i).Name Next rst.MoveFirst End Sub
About this page: