473,322 Members | 1,421 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,322 software developers and data experts.

RecordsAffected reporting the wrong value


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 is the code...

--------

Dim dbMyData As Database
Dim strSQL As String

Set dbMyData = CurrentDb

strSQL = "INSERT INTO tblExample (KeyField, TextField) VALUES ('test1', '');"
dbMyData.Execute (strSQL) 'Insert succeeds
MsgBox ("test1 records affected = " & dbMyData.RecordsAffected) ' Reports 1

strSQL = "INSERT INTO tblExample (KeyField, Count) VALUES ('test2', '');"
dbMyData.Execute (strSQL) 'Insert succeeds
MsgBox ("test2 records affected = " & dbMyData.RecordsAffected) ' Reports 0

strSQL = "INSERT INTO tblExample (KeyField, RecvDate) VALUES ('test3', '');"
dbMyData.Execute (strSQL) 'Insert succeeds
MsgBox ("test3 records affected = " & dbMyData.RecordsAffected) 'Reports 0

--------

The database has 4 fields, and all except the KeyField allow empty records.

I just wasted a whole day chasing this down. My data has a lot of null
values that will need to be stored in a variety of fields. If I have to,
I can replace the '' with NULL, but it'll be a real pain because I'll have
to examine the value of every field on every insert.

Why is RecordsAffected lying to me?

Thanks,
Jack
Nov 13 '05 #1
2 3696
That's an interesting issue, Jack.

The core of the problem is that the SQL strings are invalid: you cannot
assign a zero-length string to a Number type field, nor to a Date/Time
field. Consequently, the insert *should* fail. Instead, Access assigns what
it can from the insert anyway, and then reports that it failed. Clearly that
result is inconsistent: in my view, the error is that JET is allowing the
invalid string to partially execute, when the append should completely fail.

To avoid the problem, use the dbFailOnError switch, which blocks the invalid
append, and so the RecordsAffected is correctly returned as zero:
Dim dbMyData As Database
Dim strSQL As String

Set dbMyData = CurrentDb

strSQL = "INSERT INTO tblExample (KeyField, TextField) VALUES ('test1',
'');"
dbMyData.Execute strSQL, dbFailOnError 'Insert succeeds
Debug.Print "test1 records affected = " & dbMyData.RecordsAffected '
Reports 1

strSQL = "INSERT INTO tblExample (KeyField, Count) VALUES ('test2',
'');"
dbMyData.Execute strSQL, dbFailOnError 'Insert succeeds
Debug.Print "test2 records affected = " & dbMyData.RecordsAffected '
Reports 0

strSQL = "INSERT INTO tblExample (KeyField, RecvDate) VALUES ('test3',
'');"
dbMyData.Execute strSQL, dbFailOnError 'Insert succeeds
Debug.Print "test3 records affected = " & dbMyData.RecordsAffected
'Reports 0

To get that to run to completion, you will need to add:
On Error Resume Next

To correctly form the SQL strings, use Null instead of a zero-length string:
INSERT INTO tblExample (KeyField, TextField) VALUES ('test1', Null);
INSERT INTO tblExample (KeyField, RecvDate) VALUES ('test3', Null);

BTW, I would also encourage you to set the AllowZeroLength property of your
Text fields to No, so you do not have to constantly test for both a ZLS
(zero-length-string) and a Null, and so your users (who can't see the
difference) don't get confused. When you do, the first of your examples also
fails.

If you are not clear on the difference between null and a zls, see:
http://allenbrowne.com/casu-12.html

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Jack" <jn***@pcisysSPAM.net> wrote in message
news:pa****************************@pcisysSPAM.net ...

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 is the code...

--------

Dim dbMyData As Database
Dim strSQL As String

Set dbMyData = CurrentDb

strSQL = "INSERT INTO tblExample (KeyField, TextField) VALUES ('test1',
'');"
dbMyData.Execute (strSQL) 'Insert succeeds
MsgBox ("test1 records affected = " & dbMyData.RecordsAffected) ' Reports
1

strSQL = "INSERT INTO tblExample (KeyField, Count) VALUES ('test2', '');"
dbMyData.Execute (strSQL) 'Insert succeeds
MsgBox ("test2 records affected = " & dbMyData.RecordsAffected) ' Reports
0

strSQL = "INSERT INTO tblExample (KeyField, RecvDate) VALUES ('test3',
'');"
dbMyData.Execute (strSQL) 'Insert succeeds
MsgBox ("test3 records affected = " & dbMyData.RecordsAffected) 'Reports 0

--------

The database has 4 fields, and all except the KeyField allow empty
records.

I just wasted a whole day chasing this down. My data has a lot of null
values that will need to be stored in a variety of fields. If I have to,
I can replace the '' with NULL, but it'll be a real pain because I'll have
to examine the value of every field on every insert.

Why is RecordsAffected lying to me?

Thanks,
Jack

Nov 13 '05 #2

Thanks for your clear and informative reply. I had a feeling
that the inserts should've failed. Guess I'll have
to replace my empty strings with NULLs (and add dbFailOnError
per your suggestion).

Regards,
Jack

Nov 13 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

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...
3
by: Oney | last post by:
Sql Reporting Services uses sql command and stored procedures to prepare report. but I need to use dataset to manipulate data in a class. So is it possible with Sql Reporting Services ? Thanks
1
by: rmk | last post by:
How can I get the 2000 and 2005 versions of SQL Server Reporting Services both working on my development laptop ????? I have ASP.NET 1.1 and 2.0 installed on my laptop. I have Visual Studio...
0
by: YellowFin Announcements | last post by:
Whitepaper: "Yellowfin Reporting" enables Embedded Business Intelligence -------------------------------------------------------------------------------- Embedded reports are a standard...
3
by: Brian A | last post by:
I have written some Javascript that works perfectly in Firefox - no errors reported. The dreaded MSIE 6 produces nothing but a long line of 'undefinedundefinedundefinedundefinedundefined ....'...
0
by: Phils | last post by:
Hello guys; I i have just found the solution to my problem but would like to full understand the solution so would be great full if you could enlighten me. OUR Set-up
1
by: aspineux | last post by:
imaplib use exception to report errors, but some problems must be detected by checking the return value ! For example, when trying to append into a mailbox with wrong ACL, imaplib return 'NO',...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.