Search code examples
vbams-accessstack-overflowdaoinsertion

Using RecordsAffected-method in VBA causes error when the number exceeds approximately 90.000 records?


I'm running an append-query in VBA (inside MS Access) that looks like the code below. When I use the RecordsAffected-method in VBA in order to keep track of how many records that have been inserted, it causes an error when the number exceeds approximately 90.000 records and above? (some kind of stackoverflow error it says)

The funny part is, that when I don't use RecordsAffected-method, the query works just fine. And it also works fine when the number of rows affected is below 90.000.

What can be wrong? Is this a bug in VBA?
.
.

Dim dbs As DAO.Database  
sql As String  
iCount As Integer  
Set dbs = CurrentDb  

sql = "INSERT INTO " & ReceiveTable_selected & " SELECT " & NavisionTable_selected & ".* " & _
                   "FROM " & NavisionTable_selected & " " & _
                   "WHERE ((([" & NavisionTable_selected & "].[Entry No_]  ) >" &   
                    Counter_selected & "))"


dbs.Execute sql, dbFailOnError
iCount = dbs.RecordsAffected

Solution

  • Change Dim iCount As Integer to Dim iCount As Long

    From the help file:

    Integer variables are stored as 16-bit (2-byte) numbers ranging in value from -32,768 to 32,767.

    and:

    Long (long integer) variables are stored as signed 32-bit (4-byte) numbers ranging in value from -2,147,483,648 to 2,147,483,647.