How to Bind TextBoxes to an ADO Recordset at run time
Posted on January 4, 2009
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | 'Database - 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 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 |