Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using Data Control at Form_Load()

VB Programming Concepts

Using Data Control at Form_Load()

by  phinoppix  Posted    (Edited  )
One common problem for most DBMS developers is fetching record via data control before the form becomes visible and initialize the appropriate controls with that record.

It's been "customary" for most programmers to initialize data control at Form_Load(), but errors usually follow. One reason is that the connection properties will take effect after the data control is completely painted in the form, that is, after Form_Load(), or at Form_Activate().

With these FAQs in mind, one could then use any of the following methods to resolve this problem.

***** This portion applies to VB's intrinsic data control ****

1) Initialize the Data control at Form_Load, but fetch at Form_Activate().
Take into consideration switches to ensure that initialization takes place only once since Form_Activate()will be raised very often esp. when switching windows.
e.g.
Code:
        Private Sub Form_Activate()
            Static sw as Boolean
            If Not sw Then
                With daoctrl.Recordset
                    .MoveFirst
                    Text1.Text = .Fields(0)
                End With
            End If
        End Sub
        

        Private Sub Form_Load()
            With daoctrl
                .Databasename = "DBPath"
                .Recordsource = "rcdSource"
            End With
        End Sub

2. Execute Refresh method immediately
e.g.
Code:
        Private Sub Form_Load()
            With daoctrl
                .Databasename = "DBPath"
                .Recordsource = "rcdSource"
                .Refresh
                .Recordset.MoveFirst
                Text1.Text = .Recordset.Fields(0)
            End With
        End Sub

***** This portion applies to ADO data control ****

ADO data control's design is most practical like for this matter. ADO data control remains disabled on any form events even if all connection properties were defined until the Refresh method is executed, similar to method #2.
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top