Pages

Sunday, August 4, 2013

How to Open a Recordset

How to Open a Recordset

Recordsets are Visual Basic objects that hold list of records retrieved from a database. You can use recordset objects with any database including a Microsoft Access or an SQL server. To use a recordset object, the database is specified with the query or table used to populate the object's values. This is beneficial for Web designers who use dynamic processing for Web or desktop applications.

Instructions

    1

    Define your variables that retrieve the database and call the table information that fills the recordset object. The following code defines your Visual Basic variables:
    Dim myDB As Database
    Dim myRS As RecordSet

    2

    Open your database and assign it to the database variable defined in Step 1. The following code retrieves a Microsoft Access database and assigns it to the variable:
    Set myDB = OpenDatabase("AccessDatabase.mdb")

    3

    Fill the recordset with the retrieved table values. In this example, the customer table is retrieved and assigned to the recordset. The code below illustrates how to assign the recordset:
    Set myRS = myDB.OpenRecordSet("Customer", dbOpenDynaset)

    4

    Print the first value for the recordset. The message box is used to test your code and print the first value, so you know the recordset was filled. The following code prints the first recordset value:
    MsgBox myRS(0)

    5

    Press the F5 key to save your changes and execute the new code in the compiler's debugger. The debugger is used to run code in "test mode," which helps you find errors in syntax.

0 comments:

Post a Comment