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

SQL Statement or FOR/NEXT loop ?

I have 2 columns in a continuous form.

One called Balance_1 and the other Balance_2.

I want to transfer the contents of Balance_2 to Balance_1 ... for all
2000 rows in the table.

How to go about this ? Should I use a for/next loop or a SQL
statement.

If you'll recommend a SQL Statement, could you please post some air-
code ? My SQL is v.weak.

Thx.

Aug 6 '07 #1
5 17324
The code to do this update is:
dim strSql as string

strSql = "update NameOfTable set Balance_1 = Balance_2"
currentdb.Execute strSql

In fact, you could just use one line of code:

currentdb.Execute "update NameOfTable set Balance_1 = Balance_2"

So, it is lot less code then writing a udpate loop...

It not clear which records you wanted updated? The above statement will
update all records (rows) in the table. If there is some type of filter, or
restriction, then you have to change the above sql to reflect this (in other
words, the above sql has no relation to the form, or records in a form).

Since this is a sql update statement, they are VERY dangerous. So, since you
new to this...do make a backup before you run/test this type of update...
So....say again:

do make a backup before you do this....
--
Albert D. Kallal (Access MVP)
Edmonton, Alberta Canada
pl*****************@msn.com
Aug 6 '07 #2
Thx Albert ! Nice to hear from you after ages. It's me who's been off
this NG for a long time.

I am aware that SQL is far faster than a for/next loop but needed some
hand holding for the statement & syntax.

Secondly, no filter is required. I needed ALL records to be updated.

And yes, I will definitely make a backup of the table before running
this.

Once again ... Thx !!

Best Rgds,
Prakash.


On Aug 6, 1:08 pm, "Albert D. Kallal" <PleaseNOOOsPAMmkal...@msn.com>
wrote:
The code to do this update is:

dim strSql as string

strSql = "update NameOfTable set Balance_1 = Balance_2"
currentdb.Execute strSql

In fact, you could just use one line of code:

currentdb.Execute "update NameOfTable set Balance_1 = Balance_2"

So, it is lot less code then writing a udpate loop...

It not clear which records you wanted updated? The above statement will
update all records (rows) in the table. If there is some type of filter, or
restriction, then you have to change the above sql to reflect this (in other
words, the above sql has no relation to the form, or records in a form).

Since this is a sql update statement, they are VERY dangerous. So, since you
new to this...do make a backup before you run/test this type of update...

So....say again:

do make a backup before you do this....

--
Albert D. Kallal (Access MVP)
Edmonton, Alberta Canada
pleaseNOOSpamKal...@msn.com

Aug 6 '07 #3
One more small query:

What's the difference between using DoCmd.RunSQL and
currentdb.Execute. Which is better & Why ? Any pros/cons ??

Rgds,
Prakash.

On Aug 6, 1:08 pm, "Albert D. Kallal" <PleaseNOOOsPAMmkal...@msn.com>
wrote:
The code to do this update is:

dim strSql as string

strSql = "update NameOfTable set Balance_1 = Balance_2"
currentdb.Execute strSql

In fact, you could just use one line of code:

currentdb.Execute "update NameOfTable set Balance_1 = Balance_2"

So, it is lot less code then writing a udpate loop...

It not clear which records you wanted updated? The above statement will
update all records (rows) in the table. If there is some type of filter, or
restriction, then you have to change the above sql to reflect this (in other
words, the above sql has no relation to the form, or records in a form).

Since this is a sql update statement, they are VERY dangerous. So, since you
new to this...do make a backup before you run/test this type of update...

So....say again:

do make a backup before you do this....

--
Albert D. Kallal (Access MVP)
Edmonton, Alberta Canada
pleaseNOOSpamKal...@msn.com

Aug 6 '07 #4
fp**************@gmail.com wrote:
>What's the difference between using DoCmd.RunSQL and
currentdb.Execute. Which is better & Why ? Any pros/cons ??
Using Currentdb.execute strsql, dbfailonerrors will give you warning messages.
docmd.runsql ignores such.

Also performance can be significantly different between the two methods. One posting
stated currentdb.execute took two seconds while docmd.runsql took eight seconds. As
always YMMV.

Tony
--
Tony Toews, Microsoft Access MVP
Please respond only in the newsgroups so that others can
read the entire thread of messages.
Microsoft Access Links, Hints, Tips & Accounting Systems at
http://www.granite.ab.ca/accsmstr.htm
Tony's Microsoft Access Blog - http://msmvps.com/blogs/access/
Aug 6 '07 #5
Noted !! Thank you for the explanation. Makes a lot of sense.

Best Rgds,
Prakash.

On Aug 6, 11:13 pm, "Tony Toews [MVP]" <tto...@telusplanet.netwrote:
fprakashwadhw...@gmail.com wrote:
What's the difference between using DoCmd.RunSQL and
currentdb.Execute. Which is better & Why ? Any pros/cons ??

Using Currentdb.execute strsql, dbfailonerrors will give you warning messages.
docmd.runsql ignores such.

Also performance can be significantly different between the two methods. One posting
stated currentdb.execute took two seconds while docmd.runsql took eight seconds. As
always YMMV.

Tony
--
Tony Toews, Microsoft Access MVP
Please respond only in the newsgroups so that others can
read the entire thread of messages.
Microsoft Access Links, Hints, Tips & Accounting Systems athttp://www.granite.ab.ca/accsmstr.htm
Tony's Microsoft Access Blog -http://msmvps.com/blogs/access/

Aug 7 '07 #6

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

Similar topics

4
by: AndrewM | last post by:
Hello, I have a SUB which has input variables. If I run it once its okay, but I need to include it in a for....next loop. Can this be done ? for i=0 to 100 call mySub(arr(0,i)) next
8
by: Terry Olsen | last post by:
How do I loop back to the beginning of a for/next loop before getting to the end of it? Isn't there an "iterate" command or something like that? For Each This in That ...code if This = False...
6
by: Dave G | last post by:
I am writing a function to fill in the data in an unbound form. I have a table with field names of 1, 2, 3 etc up to approx 100. I have an unbound form with fields called 1, 2 3 etc. I gave...
2
by: BerkshireGuy | last post by:
I have a routine in Access that loops through an Excel workbook and executes code base on the worksheet name. If the worksheet name contains the word 'Stats' than that is a sheet I want to read...
1
by: Kevin | last post by:
ASP.NET 2.0 I have code that updates a database from a number of textboxes on a web form. I've had to hard coded references to my web form textboxes. I'd like to know how I can reference them...
3
by: semo | last post by:
Hi everybody,,, I’m studying VB.Net for the first time so I’m not an expert on it.. that’s why I need your help… My teacher did not teach us for/next loop.. he just want to know how we can...
7
by: muddasirmunir | last post by:
i am using vb6 i had make a for next loop which fill data into the table now , some time it may needed that we want to cancel that loop . now what is the code to exit the for next loop i want...
15
by: mygirl22 | last post by:
1.I have created a moving object (which is a button) in a form (rectangle box) called "Enjoy Button" and it moves when 2. a specific button named "Start Timer" is pressed...When the "Start Timer"...
3
by: smileyc | last post by:
I have 5 arrays, named array1 array2 array3 array4 array5. They are integer arrays each with 5 elements in them. I want to access all the elements in all the arrays using a for next loop,the pseudo...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.