Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Modules / DAO / VBA / March 2007

Tip: Looking for answers? Try searching our database.

do loop with if then Access 2002

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
tgl - 14 Mar 2007 03:01 GMT
i want to loop through records and assign numbers for each.  records are in
order and every group of records has one with a number 1 in "varnum".  i want
to fill in a a field, "aftervar", with the 1 and then with 2,3,4,5 and so on
until it reaches next 1, stopping when it hits "DONE". it keeps saying "Loop
without Do"?  i assume you can you do "if then" within loops? may not be
doing this correctly!! thanks in advance!

Dim db As DAO.Database
Dim rs As DAO.Recordset
Set db = CurrentDb

Dim tempnum As Variant
tempnum = 1

Set rs = db.OpenRecordset("newtable", dbOpenDynaset)
With rs
.MoveFirst
Do Until rs.EOF
.Edit
If rs!varnum = 1 Then
rs!aftervar = 1
Else
If rs!varnum Is Null Then
rs!aftervar = tempnum + 1
End If
.Update
tempnum = rs!aftervar
.MoveNext
If rs!varnum = "DONE" Then
Exit Do
End If
Loop
End With
End Sub

Signature

tgl

Rick Brandt - 14 Mar 2007 03:12 GMT
> i want to loop through records and assign numbers for each.  records
> are in order and every group of records has one with a number 1 in
[quoted text clipped - 13 lines]
> Set rs = db.OpenRecordset("newtable", dbOpenDynaset)
> With rs
   > .MoveFirst
   > Do Until rs.EOF
       > .Edit
       > If rs!varnum = 1 Then
           > rs!aftervar = 1
       > Else
           > If rs!varnum Is Null Then
               > rs!aftervar = tempnum + 1
           > End If
       > .Update
       > tempnum = rs!aftervar
       > .MoveNext
       > If rs!varnum = "DONE" Then
           > Exit Do
       > End If
   > Loop
> End With
> End Sub

If you indent your code like I have above you would see that you have an...

Else
If

...that should either be...

ElseIf    (all one word)

or else should have an additional End If in there somewhere.

Signature

Rick Brandt, Microsoft Access MVP
Email (as appropriate) to...
RBrandt   at   Hunter   dot   com

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.