|
![]() Recordset BuilderWriting the VB6/VBA code to open a database and create a recordset on a table or query/view is very commonplace, yet complex and tedious. The Recordset Builder helps you create ADO and DAO code to access your data. Data can be from a Microsoft Access database or any DSN data source like SQL Server. Once you select the data source, you can pick the table or query/view. You can also select a subset of fields. By setting a few options, you quickly have code to browse, add, or edit your records.
Press [OK] to insert the code directly into your project at your cursor, or send it to the clipboard, file, or notepad. Recordset Builder ResultsADO Example for Adding New RecordsWith a few mouse clicks, you can quickly create ADO code that opens a database connection, selects a table, and lists all its fields for you to add new records:
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset Set cnn = New ADODB.Connection Set rst = New ADODB.Recordset cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & vbCrLf & _ "Data Source=C:\Office2007\Office12\SAMPLES\Northwind.accdb" rst.Open "Categories", cnn, adOpenDynamic, adLockOptimistic, adCmdTable With rst .AddNew ![CategoryID] = 0 ![CategoryName] = 0 ![Description] = 0 ![Picture] = 0 .Update .Close End With Set rst = Nothing cnn.Close Set cnn = Nothing All the complexity of creating connection and recordset objects are provided. Simply assign the field values between the .AddNew and .Update and you're done. All the complexity with opening the connections and table are handled. DAO Example for Editing RecordsSimilarly, with a few mouse clicks, you can quickly create DAO code that opens a database, selects a table, and lists all its fields for you to update. In this example, we use the Current Database option in Microsoft Access:
Dim dbs As DAO.Database
Dim rst As DAO.Recordset Set dbs = CurrentDb Set rst = dbs.OpenRecordset("Categories", dbOpenDynaset, dbSeeChanges) With rst .Edit ![CategoryID] = 0 ![CategoryName] = 0 ![Description] = 0 ![Picture] = 0 .Update .Close End With Set rst = Nothing dbs.Close Set dbs = Nothing All the complexity with opening the database and recordset are handled. Simply assign the field values between the .Edit and .Update and you're done. Recordset Builder OptionsA variety of options are available to let you customize the Recordset Builder to generate the code you want. Defining the DataThe first part is getting the database, table, and fields. Specify whether you want to open an Access database or DSN data source such as SQL Server, then open it:
Once you've selected the database, a list of tables and queries/views appear. Select the object you want:
The fields within that object appear. They are all selected by default, but if you only want a subset, select them:
Setting Recordset OptionsWith the data defined, you specify the options for the code you want to build Database Library
Access Database Connection
Variable Options
Recordset Options
Additional Options
|