Friday, March 18, 2011

How to Bind Text Boxes to an ADO Recordset during run time.


'Sample on How to Bind TextBoxes to an ADO Recordset at run time
'Open the ADO Connection and the Recordset to fetch the data. Set the TextBox's
'DataSource properties to the Recordset. Set their DataField properties to the
'names of the Recordset's fields to bind.

Private Sub Form1_Load()
    Dim data_file As String

    ' Get the database file name.
    data_file = App.Path
    If Right$(data_file, 1) <> "\" Then data_file = data_file & "\"
    data_file = data_file & "Data.mdb"

    ' Open the database connection.
    Set m_Conn = New ADODB.Connection
    m_Conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & data_file & _
        ";" & "Persist Security Info=False"
    m_Conn.Open

    ' Fetch the Books records.
    Set Recordset1 = New ADODB.Recordset
    Recordset1.Open "SELECT * FROM Books ORDER BY Title", m_Conn, adOpenDynamic, _
        adLockBatchOptimistic

    ' Bind the TextBoxes to the Recordset.
    Set textTitle.DataSource = Recordset1
    textTitle.DataField = "Title"

    Set txtURL.DataSource = Recordset1
    txtURL.DataField = "URL"
End Sub

'The program can use the Recordset's MovePrevious, MoveNext, and other
'navigation methods to display different records in the bound controls.

Private Sub BtnNext_Click()
    ' Move to the next record.
    Recordset1.MoveNext

    ' Enable the appropriate buttons.
    EnableButtons
End Sub

Private Sub BtnPrev_Click()
    ' Move to the previous record.
    Recordset1.MovePrevious

    ' Enable the appropriate buttons.
    EnableButtons
End Sub

' Enable the appropriate buttons.
Private Sub EnableButtons()
    BtnNext.Enabled = Not Recordset1.EOF
    BtnPrev.Enabled = Not Recordset1.BOF
End Sub

0 comments:

Post a Comment

 
Powered by Blogger