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

do while loop syntax

P: 25
I am trying to write a do while loop with various condition how to do that ?

whenever the book barcode is not enter loop
but not the content,
as I have objects inside (recordset)
that will cause runtime error,
if the book barcode is entered the loop the content once.

abridged code ( pseudocode)

Expand|Select|Wrap|Line Numbers
  1. do while (me.item.value<>"")
  2. if a then do a
  3. if b then do b
  4. if c then do a and a1
  5. if d then do d and Z
  6. if e then do g and h
  7. loop
  8.  
  9.  
  10.  
how is it possible to do so ?
by doing
do while (me.item.value<>"") or (if a)or (if b)
is so how do you skip the line ?


example 1.

reminders: you don't have to proofread the following code, but just to give you a gist of the various conditions.
the question is
i have many lines, how do i put them
into a syntax format in do while ("condition") or ("condition") ?
thanks.






Expand|Select|Wrap|Line Numbers
  1.     If (rsbooks!Status = 2 And rsbooks!LastPatron <> rspatrons!ID) Then
  2.             MsgBox "Item cannot be lent, it is on loan"
  3.           Me.bookidfrmlend = ""
  4.         Me.bookidfrmlend.SetFocus
  5.     End If
  6.  
  7.  
  8.      ' check if exceeds quota
  9.     If (rsbooks!Status = 1 And rspatrons!quota < rspatrons!quotarule) Then
  10.                 Dim updatebkstatusonloan As String
  11.                 Dim updatepatronquota As String
  12.                 Dim updatelastpatron As String
  13.  
  14.                 Me.[checked out to].Value = rspatrons![first name] & " ," & rspatrons![last name]
  15.                 Me.[BookID] = rsbooks!title
  16.  
  17.                 Dim addnewtrans As String
  18.                 addnewtrans = "INSERT INTO Transactions(BOOKID,[Checked out to])" & _
  19.                             "VALUES (" & rsbooks!ID & ", " & rspatrons!ID & ")"
  20.                 DoCmd.SetWarnings False
  21.                 DoCmd.RunSQL addnewtrans
  22.                 updatebkstatusonloan = "UPDATE books Set status=2 WHERE Barcode= '" & _
  23.                 Me.bookidfrmlend.Value & "'"
  24.  
  25.                 updatepatronquota = "UPDATE patrons set quota =quota+1 WHERE [Patron Barcode]= '" & _
  26.                 Me.cmbpbarcode.Value & "'"
  27.  
  28.                 updatelastpatron = "UPDATE books set LastPatron = "
  29. updatelastpatron = updatelastpatron & "'" & rspatrons!ID & "'"
  30. updatelastpatron = updatelastpatron & " WHERE id ="
  31. updatelastpatron = updatelastpatron & rsbooks!ID
  32.  
  33.  
  34.                 DoCmd.RunSQL updatebkstatusonloan
  35.                 DoCmd.RunSQL updatepatronquota
  36.                 DoCmd.RunSQL updatelastpatron
  37.                 Me.[DueDate].Value = rstrans![DueDate]
  38.  
  39.  
  40.  
  41.                 MsgBox rsbooks!title & "Item is Borrowed"
  42.  
  43.     End If
  44.  
Jan 6 '12 #1
Share this Question
Share on Google+
6 Replies


100+
P: 332
Maybe you want to provide more than pseudo-code here. Did you look at the Help in Access for the Do Loop statement?

Expand|Select|Wrap|Line Numbers
  1. Do While Not SomeValue
  2.    ExecuteSomething
  3. Loop
Expand|Select|Wrap|Line Numbers
  1. Do While SomethingRemainsTrue
  2.   ExecuteStuff
  3. Loop
  4.  
Jan 6 '12 #2

P: 25
Mariostg
thanks for the input.
the actual need of condition is listed in example one.
all the if are my condition and they are the stuff i would like to execute.

in programming C.
the syntax are as follows
do {
xxx
} while ();

as you see there is a big bracket to hold all the conditions and execution stuff, but in vba I do not find a bracket to hold everything but just a single line and wonder do I have to put things into one line
because i have tried

Expand|Select|Wrap|Line Numbers
  1. do while (condition)
  2. if a then msgbox a end if
  3. if b then msgbox b end if
  4. loop
  5.  
  6.  
i got an error message for loop without do.
for the actual code,
image the do while is put before line (1) of the example 1
and loop at line (44)
thanks,
Jan 6 '12 #3

100+
P: 332
Yep, there are quite some differences between VBA and C. In VBA, there are no bracket. Also, there are no ; to indicate end of line.

For future reference, you should really indicate what is the error you are getting. But from you above code, it should be:
Expand|Select|Wrap|Line Numbers
  1. do while (condition) 
  2.   if a then 
  3.     msgbox a 
  4.   end if 
  5.   if b then 
  6.     msgbox b 
  7.   end if 
  8. loop 
Or (depending on the school of thoughts)
Expand|Select|Wrap|Line Numbers
  1. do while (condition) 
  2.   if a then msgbox a  
  3.   if b then msgbox b  
  4. loop 
  5.  
Because there are no bracket, although indentation is not necessary, it is recommended to use it to make structure stands out.
Jan 6 '12 #4

100+
P: 759
After all Mariostg say I think is nothing to add about Do-Loop structure.
Here I rewrite your first pseudo-code for VB
Expand|Select|Wrap|Line Numbers
  1. do while (me.item.value<>"")
  2.     if a then do a
  3.     if b then do b
  4.     if c then
  5.         do a
  6.         do a1
  7.     end if
  8.     if d then
  9.         do d
  10.         do Z
  11.     end if
  12.     if e then
  13.         do g
  14.         do h
  15.     end if
  16. loop
Jan 7 '12 #5

P: 25
thanks for all your effort.
i have restructured my program instead of putting in a sub I write individual sub in the afterupdate section of each textbox to get what i want.
thanks,
Jan 7 '12 #6

NeoPa
Expert Mod 15k+
P: 31,709
I reset the Best Answer - as it didn't answer the question - and was one of your own posts.

It would need to be somewhat remarkable anyway even to allow the latter, but without even being an answer it was a no-brainer.

PS. The Do While Loop syntax is explained perfectly well within the Context-Sensitive Help system.
Jan 9 '12 #7

Post your reply

Sign in to post your reply or Sign up for a free account.