|
|
Title | Bind TextBoxes to an ADO Recordset at run time |
Keywords | ADO, database, bind, data binding |
Categories | Database |
|
|
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 Form_Load()
Dim db_file As String
' Get the database file name.
db_file = App.Path
If Right$(db_file, 1) <> "\" Then db_file = db_file & _
"\"
db_file = db_file & "Data.mdb"
' Open the database connection.
Set m_Conn = New ADODB.Connection
m_Conn.ConnectionString = _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & db_file & ";" & _
"Persist Security Info=False"
m_Conn.Open
' Fetch the Books records.
Set m_Recordset = New ADODB.Recordset
m_Recordset.Open _
"SELECT * FROM Books ORDER BY Title", _
m_Conn, adOpenDynamic, adLockBatchOptimistic
' Bind the TextBoxes to the Recordset.
Set txtTitle.DataSource = m_Recordset
txtTitle.DataField = "Title"
Set txtURL.DataSource = m_Recordset
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 cmdNext_Click()
' Move to the next record.
m_Recordset.MoveNext
' Enable the appropriate buttons.
EnableButtons
End Sub
Private Sub cmdPrev_Click()
' Move to the previous record.
m_Recordset.MovePrevious
' Enable the appropriate buttons.
EnableButtons
End Sub
' Enable the appropriate buttons.
Private Sub EnableButtons()
cmdNext.Enabled = Not m_Recordset.EOF
cmdPrev.Enabled = Not m_Recordset.BOF
End Sub
|
|
|
|
|
|