RE: vba loop statement

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



If you don't have one, I would add a field that captures the Date/Time that
the record was updated as without it you won't know which records were
updated in the event that the code craps out. Going with the SQL Statement
will obviously eliminate that possibility.

"David H" wrote:

It isn't so much loops that you need to investigate as it is DAO. Loops are
actually quite easy as in ...

for i = 0 to 100
[do something]
next i

for i = 0 to Forms.count - 1
[do something]
next i

while not rs.EOR
[do something]
wend

Its the [do something] that's the key.

The example below you get you started and is pretty standard when looping
through records. I adapted it from an example to show you how to update the
value in a field.

Function DAORecordsetExample()
'Purpose: How to open a recordset and loop through the records.
'Note: Requires a table named MyTable, with a field named MyField.
Dim rs As DAO.Recordset
Dim strSql As String

strSql = "SELECT MyField FROM MyTable;"

'Creates a Recordset object pointing to the records selected in strSQL
Set rs = DBEngine(0)(0).OpenRecordset(strSql)

'Basic loop
While Not rs.EOF
Debug.Print rs!MyField
rs.MoveNext
wend

'Basic loop to update the value in the 'Amount' field by 10%.
While Not rs.EOF
with rs
.Edit
.Fields("Amount") = .Fields("Amount") * 1.10
.Update
.MoveNext
wend

rs.Close
Set rs = Nothing
End Function

That being said, if you're updating records in batch, you're probably better
off updating the records via a SQL Statement as in...

UPDATE Products SET Standard_Amount = Standard_Amount * 1.10,
Discount_Amount = Discount_Amount * 1.10

The SQL statement above will change all of the records in one fell swoop. If
you have a situation where the discounted price is fixed price less than the
standard you could go with...

UPDATE Products SET Standard_Amount = Standard_Amount * 1.10,
Discount_Amount = Standard_Amount *.30 * 1.10

adding a WHERE statement can be used to select specific records.

"Grimwadec" wrote:

My table has about 50 fields, one containing "Products" and the other fields
containing various prices for the products depending on certain
circumstances, e.g. one price field is simply called "PriceStandard" I don't
have a good enough understanding (Translation - no idea!) of loops to be able
to write the code required to go through each field (some are Null) and if it
contains a Currency Value then increase it by a percentage criteria which I
will feed into the code from a dialog box. Now this may be how I will learn
to understand loops now that I have a practical application. Can anybody help
please?
--
Grimwadec
.



Relevant Pages

  • RE: vba loop statement
    ... "David H" wrote: ... How to open a recordset and loop through the records. ... off updating the records via a SQL Statement as in... ... you have a situation where the discounted price is fixed price less than the ...
    (microsoft.public.access.modulesdaovba)
  • RE: vba loop statement
    ... It isn't so much loops that you need to investigate as it is DAO. ... The SQL statement above will change all of the records in one fell swoop. ... you have a situation where the discounted price is fixed price less than the ...
    (microsoft.public.access.modulesdaovba)
  • RE: does this sql capability exist
    ... more ways to dynamically create the SQL statement. ... what happens if you need to have a price for 1-25 items instead ... you convert the quote to a PO or create a separate PO later? ... I have 10 text boxes that represent 10 of the fields in my table ...
    (microsoft.public.access.formscoding)
  • Re: UPDATE Using Previous Record Value
    ... It looks like your formula is similar to getting running YTD sales ... Then advance one in the date loop and rejoin current/previous and repeat ... Now suppose a price has undergone the following sequential percent daily ...
    (microsoft.public.sqlserver.programming)
  • Re: How to declare a end of document loop
    ... I just had to replase the "wend" ... keyword with "loop" and then it worked just perfect. ... > The trick to this is that the Execute method of the Find object returns a ... > boolean value depending on whether it found something. ...
    (microsoft.public.word.vba.general)