473,414 Members | 1,621 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

How to use CurrentDb.RecordsAffected?

TheSmileyCoder
2,322 Expert Mod 2GB
I am trying to use the CurrentDB.Recordsaffected property to ensure that the latest CurrentDB.Execute actually affected some records.
I have a function intented to backup a record into a history table:

Expand|Select|Wrap|Line Numbers
  1. Public Function backupRecord(strTableName As String, strKeyField As String, lngID As Long) As Boolean
  2.     Dim strSQL As String
  3.     strSQL = "INSERT INTO [hist_" & strTableName & "]" & _
  4.             " SELECT [" & strTableName & "].*" & _
  5.             " FROM [" & strTableName & "]" & _
  6.             " WHERE [" & strKeyField & "]=" & lngID
  7.     Debug.Print strSQL
  8.     CurrentDb.Execute strSQL
  9.     Debug.Print "Records:" & CurrentDb.RecordsAffected
  10.  
  11.  
  12. End Function
My problem is that the CurrentDb.RecordsAffected is always returning 0. Can anyone explain why this is?

I have confirmed that a record is actually inserted into the history table.
Jan 4 '12 #1

✓ answered by ADezii

A slight change in Syntax should obtain the desired Results:
Expand|Select|Wrap|Line Numbers
  1. Dim strSQL As String
  2. Dim db As DAO.Database
  3.  
  4. Set db = CurrentDb
  5.  
  6. strSQL = "INSERT INTO tblTest ([Field1], [Field2], [Field3]) VALUES ('One','Two','Three')"
  7.  
  8. With db
  9.   .Execute strSQL, dbFailOnError
  10.     MsgBox "Number of Records Affected: [" & .RecordsAffected & "]"
  11. End With

6 14975
ADezii
8,834 Expert 8TB
A slight change in Syntax should obtain the desired Results:
Expand|Select|Wrap|Line Numbers
  1. Dim strSQL As String
  2. Dim db As DAO.Database
  3.  
  4. Set db = CurrentDb
  5.  
  6. strSQL = "INSERT INTO tblTest ([Field1], [Field2], [Field3]) VALUES ('One','Two','Three')"
  7.  
  8. With db
  9.   .Execute strSQL, dbFailOnError
  10.     MsgBox "Number of Records Affected: [" & .RecordsAffected & "]"
  11. End With
Jan 4 '12 #2
TheSmileyCoder
2,322 Expert Mod 2GB
Thank you very much. You have just made my life alot easier!
Jan 4 '12 #3
ADezii
8,834 Expert 8TB
You are quite welcome, TheSmileyCoder.
Jan 5 '12 #4
NeoPa
32,556 Expert Mod 16PB
You'll notice Smiley, that although the question is quite different from your other one earlier, the solution is the same. Once you get that understanding a whole bunch of similar issues will disappear. BTW the thread Stewart linked to has a whole lot more interesting stuff in it than just that answer - It's pretty darned long though.
Jan 5 '12 #5
TheSmileyCoder
2,322 Expert Mod 2GB
Hi Neo

Yes, I do realise that now. Both questions were posted at the same time though, before I got the explanation.

I will just include the link to the post in the other thread, since I find it quite relevant:
Problem using CurrentDB - Post nr 9
Jan 5 '12 #6
NeoPa
32,556 Expert Mod 16PB
Smiley:
Yes, I do realise that now. Both questions were posted at the same time though, before I got the explanation.
I'm fully aware of that. My comment was not to imply any criticism, but simply to check that you'd perceived the problem. Clearly you had ;-)
Jan 6 '12 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Vladislav Moltchanov | last post by:
I am making data entry tool in Acc2K for several data collecting centres. It is convenient to have several files (mde file, .mdb with tables only etc) located in the same directory. To make...
7
by: Bri | last post by:
Greetings, I have a Passthrough Query to SQL Server that I want to know the RecordsAffected, but it always returns 0. This worked perfectly when it was an Access Query on the ODBC linked...
2
by: Jack | last post by:
It appears that RecordsAffected reports the wrong value when a Null integer or date value is inserted using empty quotes. The insert works fine, but RecordsAffected reports 0 instead of 1. Here...
1
by: jnikle | last post by:
I have a parameter query named "qry_employee_info_reports" that I need to run in the OnOpen event of a form. I'm after its total number of records. The query's got several joins in it, and one of...
7
by: Tony M. | last post by:
I'm trying to execute an append query. I have a case that works, and one that returns an object not set error. THIS WORKS: Private Sub cmdArchiveRecs_Click() BeginTrans CurrentDb.Execute...
3
by: Richard Hollenbeck | last post by:
In DAO, is there any difference between "set db = CurrentDb()" with parentheses and "set db = CurrentDb" without parentheses? I'm just looking for data from a single field in a table inside the...
1
by: sphinney | last post by:
As my Access form opens, I want it to find the names of the tables in the current Access database and populate a combobox with the table (recordset) names. Problem is, the CurrentDb.Recordsets...
6
by: kentdahlgren | last post by:
Hi, I have a rather advanced MS Access 2002 solution where I use the RecordsAffected property of the Execute method for a Querydef. I’m using it all over the solution and it has worked as...
1
MitchR
by: MitchR | last post by:
Hello Folks; I have run myself into a hole I cannot get of and need some assistance. I have a Form "Frm_ATandT_Activation_Center" that has a sub form "Frm_ATandT_Activation"....
7
by: realspido1 | last post by:
Hi all, I have a problem which I can't figure out what is it caused by and how to solve it as I never expericed it before even though I've been using same approach many times before. I have front...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.