Recordset (DAO) in Access

When a query is not enough, a recordset walks through a table record by record: read, check, change, add. Almost every piece of VBA that edits data works this way.

In short: Set rs = CurrentDb.OpenRecordset("table or SELECT") opens the recordset. Do Until rs.EOF … rs.MoveNext … Loop walks through every record, rs!Field reads a field. Change with Edit, set the field, Update; add with AddNew, set the fields, Update.

Before you start: VBA in Access.

Try it now

Task Write the procedure CustomersUSA: it loops through the customers from the USA (sorted by LastName) with a recordset and writes each last name to the Immediate window with Debug.Print.

Customers 25 rows, 10 columns
CustomerIDCompanyFirstNameLastNameCityPostalCodeCountryCustomerSinceRegularDiscount
1NoahEvansLeedsLS1 1URUK28 Feb 2023☑0.1
2Shaw & PartnersNoahDaviesGlasgowG1 1XQUK5 Feb 2024☐0.03
3Northlight IncSophiaHallDenver80202USA30 Jun 2024☑0
4OliviaEvansBristolBS1 4DJUK29 Jun 2022☑0
5OliviaSmithLondonEC1A 1BBUK23 Sep 2025☑0
6Northlight IncMiaEvansManchesterM1 1AEUK1 Feb 2025☐0.1
7Bauer LtdAvaWilsonUK14 Oct 2024☐0.05
8LucasTaylorGlasgowG1 1XQUK2 Jul 2023☐0
9JackClarkSeattle98101USA7 Nov 2022☐0.1
10Northlight IncHenryTaylorDenver80202USA3 Nov 2022☑0.1
11Northlight IncOliverJohnsonBristolBS1 4DJUK6 Mar 2024☐0
12HenryTaylorChicago60601USA24 Dec 2023☐0
13Northlight IncNoahTaylorLondonEC1A 1BBUK12 Oct 2022☐0
14Summit ConsultingMiaSmithAustin78701USA6 Feb 2022☑0
15MiaClarkAustin78701USA9 Mar 2022☑0.05
16ChloeHallSeattle98101USA4 May 2022☐0.03
17Market Street ClinicSophiaJohnsonBoston02108USA4 Jun 2023☑0
18Roth LegalMiaWrightManchesterM1 1AEUK23 Sep 2022☑0.05
19Northlight IncGraceWrightUK16 Dec 2023☐0.03
20Northlight IncOliviaBrownBoston02108USA19 Jun 2023☐0.03
21LiamBrownBoston02108USA23 Jun 2025☐0.03
22Bauer LtdJamesJohnsonManchesterM1 1AEUK5 Jun 2025☐0.05
23NoahWilsonAustin78701USA23 May 2022☐0
24Market Street ClinicJamesSmithBristolBS1 4DJUK16 Jan 2022☐0.1
25Northlight IncSophiaHallDenver80202USA4 Aug 2025☑0

Types of recordset

TypeConstantValueuse
TabledbOpenTable1a local table directly; default for a table name
DynasetdbOpenDynaset2queries and SELECT; editable; default for SQL
SnapshotdbOpenSnapshot4read only

RecordCount is right at once for a table. For a dynaset or snapshot it counts only the records visited so far: 1 right after opening, all of them only after rs.MoveLast. FindFirst exists only for dynasets and snapshots; on a table-type recordset Access reports 3251 “Operation is not supported for this type of object.”

Changing records

Call Edit before the change and Update after it. If you move to the next record without Update, Access discards the change. Access checks validation rules and relationships at Update.

Task Write the procedure DiscountRegulars: using a recordset over the regular customers (Regular = True), it raises each one’s discount by 0.02, but only if the result is at most 0.3.

Customers 25 rows, 10 columns
CustomerIDCompanyFirstNameLastNameCityPostalCodeCountryCustomerSinceRegularDiscount
1NoahEvansLeedsLS1 1URUK28 Feb 2023☑0.1
2Shaw & PartnersNoahDaviesGlasgowG1 1XQUK5 Feb 2024☐0.03
3Northlight IncSophiaHallDenver80202USA30 Jun 2024☑0
4OliviaEvansBristolBS1 4DJUK29 Jun 2022☑0
5OliviaSmithLondonEC1A 1BBUK23 Sep 2025☑0
6Northlight IncMiaEvansManchesterM1 1AEUK1 Feb 2025☐0.1
7Bauer LtdAvaWilsonUK14 Oct 2024☐0.05
8LucasTaylorGlasgowG1 1XQUK2 Jul 2023☐0
9JackClarkSeattle98101USA7 Nov 2022☐0.1
10Northlight IncHenryTaylorDenver80202USA3 Nov 2022☑0.1
11Northlight IncOliverJohnsonBristolBS1 4DJUK6 Mar 2024☐0
12HenryTaylorChicago60601USA24 Dec 2023☐0
13Northlight IncNoahTaylorLondonEC1A 1BBUK12 Oct 2022☐0
14Summit ConsultingMiaSmithAustin78701USA6 Feb 2022☑0
15MiaClarkAustin78701USA9 Mar 2022☑0.05
16ChloeHallSeattle98101USA4 May 2022☐0.03
17Market Street ClinicSophiaJohnsonBoston02108USA4 Jun 2023☑0
18Roth LegalMiaWrightManchesterM1 1AEUK23 Sep 2022☑0.05
19Northlight IncGraceWrightUK16 Dec 2023☐0.03
20Northlight IncOliviaBrownBoston02108USA19 Jun 2023☐0.03
21LiamBrownBoston02108USA23 Jun 2025☐0.03
22Bauer LtdJamesJohnsonManchesterM1 1AEUK5 Jun 2025☐0.05
23NoahWilsonAustin78701USA23 May 2022☐0
24Market Street ClinicJamesSmithBristolBS1 4DJUK16 Jan 2022☐0.1
25Northlight IncSophiaHallDenver80202USA4 Aug 2025☑0

This exercise is part of Premium.

Premium unlocks every exercise in every lesson, unlimited practice with fresh tasks and the final test with certificate.

See Premium Already Premium? Log in

The first exercise in this lesson is free.

New records

AddNew starts an empty record; Access assigns the AutoNumber at that moment. After Update the recordset is back on the record it was on before.

Task Write the procedure NewCategory: it adds the category “Toys” with AddNew and writes the new CategoryID to the Immediate window with Debug.Print.

Categories 4 rows, 2 columns
CategoryIDCategory
1Office
2Tech
3Furniture
4Paper

This exercise is part of Premium.

Premium unlocks every exercise in every lesson, unlimited practice with fresh tasks and the final test with certificate.

See Premium Already Premium? Log in

The first exercise in this lesson is free.

In real Access

  1. Put the code into a standard module (Create, Module) and start it with F5 or from the Immediate window.
  2. Declare it with Dim rs As DAO.Recordset; the DAO library is referenced in Access databases.
  3. Close the recordset at the end with rs.Close.

Common mistakes

Endless loop

Without rs.MoveNext, EOF stays False and the loop never ends.

No current record

Error 3021 “No current record.” appears when you call MoveNext past the last record, read an empty recordset or read a record you just deleted.

Setting a field without Edit

Error 3020 “Update or CancelUpdate without AddNew or Edit.”: rs.Edit or rs.AddNew is missing before rs!Field = ….

A field that does not exist

rs!NoSuchField reports 3265 “Item not found in this collection.”

FAQ

Recordset or update query?

If the change fits into one condition, an update query is shorter and faster. You need a recordset when each record needs its own checks or calculations.

Why Set db = CurrentDb?

CurrentDb returns a new object on every call. Set tdf = CurrentDb.TableDefs("Customers") is invalid right afterwards (3420 “Object invalid or no longer set.”), and CurrentDb.RecordsAffected is always 0. A variable db avoids both.

Learn next: Import and export

Exchange data with Excel.

Import and export

Write to us

Choose language

Select your preferred language