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.