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

How to make all negative values positive?

P: n/a
Is there an easy way to make all negative values positive in a particular
table?

I've been experimenting with this:

Dim rst As DAO.Recordset
Set rst = db.OpenRecordset("tblNegValues")

Do
If rst!Amount < 0 Then rst!Amount = rst!Amount - (rst!Amount * 2)
Loop

Is there a better way?
Nov 12 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a
rkc

"deko" <dj****@hotmail.com> wrote in message
news:ON*****************@newssvr29.news.prodigy.co m...
Is there an easy way to make all negative values positive in a particular
table?

I've been experimenting with this:

Dim rst As DAO.Recordset
Set rst = db.OpenRecordset("tblNegValues")

Do
If rst!Amount < 0 Then rst!Amount = rst!Amount - (rst!Amount * 2)
Loop

Is there a better way?


The Abs function?

Nov 12 '05 #2

P: n/a
I'll bet Abs is more efficient than multiplication and subtraction, but I
still need a way to apply this to an entire table. Should I use For Each?
Must I use a Recordset operation here?

Thanks!

"rkc" <rk*@yabba.dabba.do.rochester.rr.nope> wrote in message
news:fb*****************@twister.nyroc.rr.com...

"deko" <dj****@hotmail.com> wrote in message
news:ON*****************@newssvr29.news.prodigy.co m...
Is there an easy way to make all negative values positive in a particular table?

I've been experimenting with this:

Dim rst As DAO.Recordset
Set rst = db.OpenRecordset("tblNegValues")

Do
If rst!Amount < 0 Then rst!Amount = rst!Amount - (rst!Amount * 2)
Loop

Is there a better way?


The Abs function?


Nov 12 '05 #3

P: n/a
Use an update query that only contains the Amount field. Put this expression
where it says Update To:
IIF([Amount] < 0,[Amount]*-1,[Amount])
--
PC Datasheet
Your Resource For Help With Access, Excel And Word Applications
re******@pcdatasheet.com
www.pcdatasheet.com

"rkc" <rk*@yabba.dabba.do.rochester.rr.nope> wrote in message
news:fb*****************@twister.nyroc.rr.com...

"deko" <dj****@hotmail.com> wrote in message
news:ON*****************@newssvr29.news.prodigy.co m...
Is there an easy way to make all negative values positive in a particular
table?

I've been experimenting with this:

Dim rst As DAO.Recordset
Set rst = db.OpenRecordset("tblNegValues")

Do
If rst!Amount < 0 Then rst!Amount = rst!Amount - (rst!Amount * 2)
Loop

Is there a better way?


The Abs function?


Nov 12 '05 #4

P: n/a
Deko,
Why won't a simple update query do the trick for you?

UPDATE tblNegValues SET tblNegValues.Amount = -[Amount]
WHERE tblNegValues.Amount < 0

--
Hope this helps
Arno R

"deko" <dj****@hotmail.com> schreef in bericht
news:ON*****************@newssvr29.news.prodigy.co m...
Is there an easy way to make all negative values positive in a particular
table?

I've been experimenting with this:

Dim rst As DAO.Recordset
Set rst = db.OpenRecordset("tblNegValues")

Do
If rst!Amount < 0 Then rst!Amount = rst!Amount - (rst!Amount * 2)
Loop

Is there a better way?

Nov 12 '05 #5

P: n/a
dj****@hotmail.com (deko) wrote in
<Tz****************@newssvr29.news.prodigy.com>:
I'll bet Abs is more efficient than multiplication and
subtraction, but I still need a way to apply this to an entire
table. Should I use For Each? Must I use a Recordset operation
here?


If you're changing all the values in a column in a table, or a
subset of values that match certain criteria, SQL is definitely the
way to go. Arno has given you one such solution.

A SQL UPDATE is almost always going to be faster than walking
through a recordset and changing one row at a time, though there
are some kinds of changes that can't really be done with SQL, so
you sometimes do need to walk a recordset. But it's not nearly as
often as many people think (especially people who come from an
xBase environment).

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 12 '05 #6

P: n/a
Thanks for the reply.

You're right - an update query is the way to go. "Amount = -Amount" is
simple enough, and works great. I was focusing too much on the arithmetic
operations I thought were necessary to convert the negative to a positive
and didn't think I could do it with SQL.

I think Abs is a good way to go, too (rck's suggestion).

UPDATE tblNegValues SET Amount = Abs(Amount) WHERE Amount < 0
"Arno R" <ar****************@tiscali.nl> wrote in message
news:3f**********************@dreader2.news.tiscal i.nl...
Deko,
Why won't a simple update query do the trick for you?

UPDATE tblNegValues SET tblNegValues.Amount = -[Amount]
WHERE tblNegValues.Amount < 0

--
Hope this helps
Arno R

"deko" <dj****@hotmail.com> schreef in bericht
news:ON*****************@newssvr29.news.prodigy.co m...
Is there an easy way to make all negative values positive in a particular table?

I've been experimenting with this:

Dim rst As DAO.Recordset
Set rst = db.OpenRecordset("tblNegValues")

Do
If rst!Amount < 0 Then rst!Amount = rst!Amount - (rst!Amount * 2)
Loop

Is there a better way?


Nov 12 '05 #7

P: n/a
"deko" <dj****@hotmail.com> wrote in news:ONjHb.2907$NJ7.1857
@newssvr29.news.prodigy.com:
Is there an easy way to make all negative values positive in a particular
table?

I've been experimenting with this:

Dim rst As DAO.Recordset
Set rst = db.OpenRecordset("tblNegValues")

Do
If rst!Amount < 0 Then rst!Amount = rst!Amount - (rst!Amount * 2)
Loop

Is there a better way?


I suppose (whimsically), that you are storing a calculated field if you do
this, and that you should leave your data as it is, and simply use and/or
display the absolute value of these values.
--
Lyle
(for e-mail refer to http://ffdba.com/contacts.htm)
Nov 12 '05 #8

P: n/a
hi,

update MyTable
Set Amount=(Amount*-1)
where Amount <0
"deko" <dj****@hotmail.com> schrieb im Newsbeitrag
news:ON*****************@newssvr29.news.prodigy.co m...
Is there an easy way to make all negative values positive in a particular
table?

I've been experimenting with this:

Dim rst As DAO.Recordset
Set rst = db.OpenRecordset("tblNegValues")

Do
If rst!Amount < 0 Then rst!Amount = rst!Amount - (rst!Amount * 2)
Loop

Is there a better way?

Nov 12 '05 #9

P: n/a
Actually, it's part of an import process that downloads bank statements and
populates a table with all the various transactions. The database then
crunches the numbers, which need to be all positive values. But the
different banks are not consistent in how they represent the amounts - some
are negative (2,483.39), some are positive $2,483.39.

This seems to do the trick:

UPDATE tblTransactions SET Amount = Abs(Amount) WHERE Amount < 0

Thanks to all who replied - I appreciate the help!
"Lyle Fairfield" <Mi************@Invalid.Com> wrote in message
news:Xn*******************@130.133.1.4...
"deko" <dj****@hotmail.com> wrote in news:ONjHb.2907$NJ7.1857
@newssvr29.news.prodigy.com:
Is there an easy way to make all negative values positive in a particular table?

I've been experimenting with this:

Dim rst As DAO.Recordset
Set rst = db.OpenRecordset("tblNegValues")

Do
If rst!Amount < 0 Then rst!Amount = rst!Amount - (rst!Amount * 2)
Loop

Is there a better way?


I suppose (whimsically), that you are storing a calculated field if you do
this, and that you should leave your data as it is, and simply use and/or
display the absolute value of these values.
--
Lyle
(for e-mail refer to http://ffdba.com/contacts.htm)

Nov 12 '05 #10

P: n/a
hi
I just made a little query to do the job and this is the SQL code it
generated

UPDATE t_BANK_LINE SET t_BANK_LINE.BKL_EXPENSE = Abs([BKL_EXPENSE]);

it change all values of BKL_EXPENSE to positive . .

in code you could use

DoCmd.RunSql "UPDATE t_BANK_LINE SET t_BANK_LINE.BKL_EXPENSE =
Abs([BKL_EXPENSE]);"

there is no test for positive or negative numbers . . as the result is ALL
positive

cheers
paul g


"David W. Fenton" <dX********@bway.net.invalid> wrote in message
news:94***************************@24.168.128.78.. .
dj****@hotmail.com (deko) wrote in
<Tz****************@newssvr29.news.prodigy.com>:
I'll bet Abs is more efficient than multiplication and
subtraction, but I still need a way to apply this to an entire
table. Should I use For Each? Must I use a Recordset operation
here?


If you're changing all the values in a column in a table, or a
subset of values that match certain criteria, SQL is definitely the
way to go. Arno has given you one such solution.

A SQL UPDATE is almost always going to be faster than walking
through a recordset and changing one row at a time, though there
are some kinds of changes that can't really be done with SQL, so
you sometimes do need to walk a recordset. But it's not nearly as
often as many people think (especially people who come from an
xBase environment).

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc

Nov 12 '05 #11

P: n/a
"Kinetic - Paul G" <ki*****@alwaysonline.net.au> wrote in
news:3f******@alwaysonline.net.au:
hi
I just made a little query to do the job and this is the SQL
code it generated

UPDATE t_BANK_LINE SET t_BANK_LINE.BKL_EXPENSE =
Abs([BKL_EXPENSE]);

it change all values of BKL_EXPENSE to positive . .

in code you could use

DoCmd.RunSql "UPDATE t_BANK_LINE SET t_BANK_LINE.BKL_EXPENSE
= Abs([BKL_EXPENSE]);"

there is no test for positive or negative numbers . . as the
result is ALL positive

cheers
paul g


This is true, however, it is bad practice to update a record that's
not changing. There may be locking issues, some rdbms may timestamp
the records, Etc.

There is also the issue of performance in large tables where
updating two million records to fix a problem with two hundred
records imposes a serious load on the server.

Just my two cents worth.

Bob Q
Nov 12 '05 #12

P: n/a

"Bob Quintal" <bq******@generation.net> wrote in message
news:67******************************@news.teranew s.com...
"Kinetic - Paul G" <ki*****@alwaysonline.net.au> wrote in
news:3f******@alwaysonline.net.au:
hi
I just made a little query to do the job and this is the SQL
code it generated

UPDATE t_BANK_LINE SET t_BANK_LINE.BKL_EXPENSE =
Abs([BKL_EXPENSE]);

it change all values of BKL_EXPENSE to positive . .

in code you could use

DoCmd.RunSql "UPDATE t_BANK_LINE SET t_BANK_LINE.BKL_EXPENSE
= Abs([BKL_EXPENSE]);"

there is no test for positive or negative numbers . . as the
result is ALL positive

cheers
paul g


This is true, however, it is bad practice to update a record that's
not changing. There may be locking issues, some rdbms may timestamp
the records, Etc.

There is also the issue of performance in large tables where
updating two million records to fix a problem with two hundred
records imposes a serious load on the server.

Just my two cents worth.

Bob Q


Good points Bob . .. I'll keep them in mind . . cheers paul g


Nov 12 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.