By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,398 Members | 944 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,398 IT Pros & Developers. It's quick & easy.

0 + 0 = -1 ?

P: n/a
Hi,

I have a very strange behaviour on adding the same field in different
rows of a table in an Access 2000 file format database. The field is
defined as double, there is the value 0 in it in every row in the
table, yet when I read these rows and add that field to get its sum, I
get -1 as the result, and that already after reading the first row !

Anybody can tell me what's happenening ?

Thanks
Bernard

Oct 13 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Exactly what expression are you using to sum these fields?

-1 is Access-speak for True, so it seems that - whatever expression you are
using - it evaluates to True instead of the sum.

--
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.

<be************@ubs.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
>
I have a very strange behaviour on adding the same field in different
rows of a table in an Access 2000 file format database. The field is
defined as double, there is the value 0 in it in every row in the
table, yet when I read these rows and add that field to get its sum, I
get -1 as the result, and that already after reading the first row !

Anybody can tell me what's happenening ?

Thanks
Bernard

Oct 13 '06 #2

P: n/a
Here is what I was doing:

Dim sQty As Double
Dim sAmnt As Double

Set rsTradP = CurrentDb.OpenRecordset("SELECT quantity, amount FROM
tbTrades", dbOpenDynaset)
sQty = sAmnt = 0
rsTradP.MoveFirst
While Not rsTradP.EOF
sQty = sQty + rsTradP.Fields!quantity
sAmnt = sAmnt + rsTradP.Fields!amount
rsTradP.MoveNext
Wend

sQty got to -1, sAmnt was correct ! Both are defined as double in the
table.

Bernard

Allen Browne wrote:
Exactly what expression are you using to sum these fields?

-1 is Access-speak for True, so it seems that - whatever expression you are
using - it evaluates to True instead of the sum.

--
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.

<be************@ubs.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...

I have a very strange behaviour on adding the same field in different
rows of a table in an Access 2000 file format database. The field is
defined as double, there is the value 0 in it in every row in the
table, yet when I read these rows and add that field to get its sum, I
get -1 as the result, and that already after reading the first row !

Anybody can tell me what's happenening ?

Thanks
Bernard
Oct 13 '06 #3

P: n/a
Here is what I was doing:

Dim sQty As Double
Dim sAmnt As Double

Set rsTradP = CurrentDb.OpenRecordset("SELECT quantity, amount FROM
tbTrades", dbOpenDynaset)
sQty = sAmnt = 0
rsTradP.MoveFirst
While Not rsTradP.EOF
sQty = sQty + rsTradP.Fields!quantity
sAmnt = sAmnt + rsTradP.Fields!amount
rsTradP.MoveNext
Wend

sQty got to -1, sAmnt was correct ! Both are defined as double in the
table.

Bernard

Allen Browne wrote:
Exactly what expression are you using to sum these fields?

-1 is Access-speak for True, so it seems that - whatever expression you are
using - it evaluates to True instead of the sum.

--
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.

<be************@ubs.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...

I have a very strange behaviour on adding the same field in different
rows of a table in an Access 2000 file format database. The field is
defined as double, there is the value 0 in it in every row in the
table, yet when I read these rows and add that field to get its sum, I
get -1 as the result, and that already after reading the first row !

Anybody can tell me what's happenening ?

Thanks
Bernard
Oct 13 '06 #4

P: n/a
The line:
sQty = sAmnt = 0
evaluated whether sAmnt is zero (which will be True since VBA initializes a
Double to zero), and assigns True (-1) to sQty.

Could that be where the -1 is coming from?

--
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.

<be************@ubs.comwrote in message
news:11**********************@e3g2000cwe.googlegro ups.com...
Here is what I was doing:

Dim sQty As Double
Dim sAmnt As Double

Set rsTradP = CurrentDb.OpenRecordset("SELECT quantity, amount FROM
tbTrades", dbOpenDynaset)
sQty = sAmnt = 0
rsTradP.MoveFirst
While Not rsTradP.EOF
sQty = sQty + rsTradP.Fields!quantity
sAmnt = sAmnt + rsTradP.Fields!amount
rsTradP.MoveNext
Wend

sQty got to -1, sAmnt was correct ! Both are defined as double in the
table.

Bernard

Allen Browne wrote:
>Exactly what expression are you using to sum these fields?

-1 is Access-speak for True, so it seems that - whatever expression you
are
using - it evaluates to True instead of the sum.

<be************@ubs.comwrote in message
news:11**********************@b28g2000cwb.googleg roups.com...
>
I have a very strange behaviour on adding the same field in different
rows of a table in an Access 2000 file format database. The field is
defined as double, there is the value 0 in it in every row in the
table, yet when I read these rows and add that field to get its sum, I
get -1 as the result, and that already after reading the first row !

Anybody can tell me what's happenening ?

Thanks
Bernard

Oct 13 '06 #5

P: n/a
In other words, the line...

sQty = sAmnt = 0

....is the same as...

sQty = (sAmnt = 0)

....which is the same as...

If (sAmnt = 0) Then
sQty = CDbl(True) ' which is -1
Else
sQty = CDbl(False) ' which is 0
End If
Allen Browne wrote:
The line:
sQty = sAmnt = 0
evaluated whether sAmnt is zero (which will be True since VBA initializes a
Double to zero), and assigns True (-1) to sQty.

Could that be where the -1 is coming from?

--
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.

<be************@ubs.comwrote in message
news:11**********************@e3g2000cwe.googlegro ups.com...
Here is what I was doing:

Dim sQty As Double
Dim sAmnt As Double

Set rsTradP = CurrentDb.OpenRecordset("SELECT quantity, amount FROM
tbTrades", dbOpenDynaset)
sQty = sAmnt = 0
rsTradP.MoveFirst
While Not rsTradP.EOF
sQty = sQty + rsTradP.Fields!quantity
sAmnt = sAmnt + rsTradP.Fields!amount
rsTradP.MoveNext
Wend

sQty got to -1, sAmnt was correct ! Both are defined as double in the
table.

Bernard

Allen Browne wrote:
Exactly what expression are you using to sum these fields?

-1 is Access-speak for True, so it seems that - whatever expression you
are
using - it evaluates to True instead of the sum.

<be************@ubs.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...

I have a very strange behaviour on adding the same field in different
rows of a table in an Access 2000 file format database. The field is
defined as double, there is the value 0 in it in every row in the
table, yet when I read these rows and add that field to get its sum, I
get -1 as the result, and that already after reading the first row !

Anybody can tell me what's happenening ?

Thanks
Bernard
Oct 13 '06 #6

P: n/a
Well thanks to both of you, because I am using that method for
initialising many variables in one go at another place in the app ! So
I'll change these 2 statements asap ! I saw that somewhere in a code
snippet and thought it was handy to init many vars, but that's actually
bl.. dangerous !

Bernard

Gord wrote:
In other words, the line...

sQty = sAmnt = 0

...is the same as...

sQty = (sAmnt = 0)

...which is the same as...

If (sAmnt = 0) Then
sQty = CDbl(True) ' which is -1
Else
sQty = CDbl(False) ' which is 0
End If
Allen Browne wrote:
The line:
sQty = sAmnt = 0
evaluated whether sAmnt is zero (which will be True since VBA initializes a
Double to zero), and assigns True (-1) to sQty.

Could that be where the -1 is coming from?

--
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.

<be************@ubs.comwrote in message
news:11**********************@e3g2000cwe.googlegro ups.com...
Here is what I was doing:
>
Dim sQty As Double
Dim sAmnt As Double
>
Set rsTradP = CurrentDb.OpenRecordset("SELECT quantity, amount FROM
tbTrades", dbOpenDynaset)
sQty = sAmnt = 0
rsTradP.MoveFirst
While Not rsTradP.EOF
sQty = sQty + rsTradP.Fields!quantity
sAmnt = sAmnt + rsTradP.Fields!amount
rsTradP.MoveNext
Wend
>
sQty got to -1, sAmnt was correct ! Both are defined as double in the
table.
>
Bernard
>
Allen Browne wrote:
>Exactly what expression are you using to sum these fields?
>>
>-1 is Access-speak for True, so it seems that - whatever expression you
>are
>using - it evaluates to True instead of the sum.
>>
><be************@ubs.comwrote in message
>news:11**********************@b28g2000cwb.googleg roups.com...
>
I have a very strange behaviour on adding the same field in different
rows of a table in an Access 2000 file format database. The field is
defined as double, there is the value 0 in it in every row in the
table, yet when I read these rows and add that field to get its sum, I
get -1 as the result, and that already after reading the first row !
>
Anybody can tell me what's happenening ?
>
Thanks
Bernard
Oct 13 '06 #7

P: n/a
>I saw that somewhere

Other languages can support that type of statement because they
differentiate between the assignment operator and the equality
operator. For example, in PHP...

$Qty = $Amnt = 0;

....will assign the value zero to both variables (as you had intended),
while...

$Qty = $Amnt == 0;

....will compare $Amnt with zero and assign the result to $Qty (as in
the previous VBA examples).
be************@ubs.com wrote:
Well thanks to both of you, because I am using that method for
initialising many variables in one go at another place in the app ! So
I'll change these 2 statements asap ! I saw that somewhere in a code
snippet and thought it was handy to init many vars, but that's actually
bl.. dangerous !

Bernard

Gord wrote:
In other words, the line...

sQty = sAmnt = 0

...is the same as...

sQty = (sAmnt = 0)

...which is the same as...

If (sAmnt = 0) Then
sQty = CDbl(True) ' which is -1
Else
sQty = CDbl(False) ' which is 0
End If
Allen Browne wrote:
The line:
sQty = sAmnt = 0
evaluated whether sAmnt is zero (which will be True since VBA initializes a
Double to zero), and assigns True (-1) to sQty.
>
Could that be where the -1 is coming from?
>
--
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.
>
<be************@ubs.comwrote in message
news:11**********************@e3g2000cwe.googlegro ups.com...
Here is what I was doing:

Dim sQty As Double
Dim sAmnt As Double

Set rsTradP = CurrentDb.OpenRecordset("SELECT quantity, amount FROM
tbTrades", dbOpenDynaset)
sQty = sAmnt = 0
rsTradP.MoveFirst
While Not rsTradP.EOF
sQty = sQty + rsTradP.Fields!quantity
sAmnt = sAmnt + rsTradP.Fields!amount
rsTradP.MoveNext
Wend

sQty got to -1, sAmnt was correct ! Both are defined as double in the
table.

Bernard

Allen Browne wrote:
Exactly what expression are you using to sum these fields?
>
-1 is Access-speak for True, so it seems that - whatever expression you
are
using - it evaluates to True instead of the sum.
>
<be************@ubs.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...

I have a very strange behaviour on adding the same field in different
rows of a table in an Access 2000 file format database. The field is
defined as double, there is the value 0 in it in every row in the
table, yet when I read these rows and add that field to get its sum, I
get -1 as the result, and that already after reading the first row !

Anybody can tell me what's happenening ?

Thanks
Bernard
Oct 13 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.