Results 1 to 4 of 4

Thread: recordset question

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2003
    Posts
    2

    Question recordset question

    I am new to VBA and Access. I'm trying to write a VBA app within Access. I need to query a database and step thru the results one record at a time for processing. I've picked up that I can do this with ADO and a recordset. Does anyone have specific example code to do this? I would greatly appreciate any help .

  2. #2
    Lively Member
    Join Date
    Jun 2003
    Posts
    114
    Search this forum or goto www.codeguru.com in the VB forum there and do a search There is TONS! of ADO code floating around ...

  3. #3
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    VB Code:
    1. ' You'll need to go upto the Project > References menu & set
    2. ' the Microsoft ActiveX Data Objects x.0 box on for this to work
    3.  
    4. Private Sub Form_Load()
    5.     Dim cnnAccessDb As ADODB.Connection
    6.     Dim rsCartoons As ADODB.Recordset
    7.     Dim strCnnString As String
    8.     Dim strSQL As String
    9.    
    10.     ' Setup an SQL statement to filter the information returned
    11.     ' from the database, then the connection string to tell ADO
    12.     ' where & how to connect to the database (if you don't know
    13.     ' hoiw to set these, do a forum seach for 'UDL' with my name).
    14.     strSQL = "SELECT * FROM field1 WHERE field1 = 'DBZ'"
    15.     strCnnString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    16.     "Data Source=C:\AccessDatabaseName.mdb"
    17.    
    18.     ' Setup the connection to the database
    19.     Set cnnAccessDb = New ADODB.Connection
    20.    
    21.     With cnnAccessDb
    22.         .CursorLocation = adUseClient
    23.         .ConnectionString = strCnnString
    24.         .Open
    25.     End With
    26.    
    27.     ' Now setup the recordset of information.
    28.     Set rsCartoons = New ADODB.Recordset
    29.    
    30.     rsCartoons.Open strSQL, cnnAccessDb
    31.    
    32.     ' Perform a loop to go through each of the records/rows in the
    33.     ' database until the End Of File, or last record is reached. For
    34.     ' each one found, print the value of the first column named field1
    35.     ' into the immediate window, then move to the next record.
    36.     Do Until rsCartoons.EOF
    37.         Debug.Print rsCartoons!Field1
    38.         rsCartoons.MoveNext
    39.     Loop
    40.    
    41.     rsCartoons.Close
    42.     Set rsCartoons = Nothing
    43.    
    44.     cnnAccessDb.Close
    45.     Set cnnAccessDb = Nothing
    46. End Sub

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  4. #4

    Thread Starter
    New Member
    Join Date
    Jul 2003
    Posts
    2
    That worked beautifully...thanks!!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width