do while loop syntax

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • issactang
    New Member
    • Dec 2011
    • 25

    #1

    do while loop syntax

    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)

    Code:
    do while (me.item.value<>"")
    if a then do a
    if b then do b
    if c then do a and a1
    if d then do d and Z
    if e then do g and h
    loop
    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.






    Code:
        If (rsbooks!Status = 2 And rsbooks!LastPatron <> rspatrons!ID) Then
                MsgBox "Item cannot be lent, it is on loan"
              Me.bookidfrmlend = ""
            Me.bookidfrmlend.SetFocus
        End If
          
        
         ' check if exceeds quota
        If (rsbooks!Status = 1 And rspatrons!quota < rspatrons!quotarule) Then
                    Dim updatebkstatusonloan As String
                    Dim updatepatronquota As String
                    Dim updatelastpatron As String
                   
                    Me.[checked out to].Value = rspatrons![first name] & " ," & rspatrons![last name]
                    Me.[BookID] = rsbooks!title
                    
                    Dim addnewtrans As String
                    addnewtrans = "INSERT INTO Transactions(BOOKID,[Checked out to])" & _
                                "VALUES (" & rsbooks!ID & ", " & rspatrons!ID & ")"
                    DoCmd.SetWarnings False
                    DoCmd.RunSQL addnewtrans
                    updatebkstatusonloan = "UPDATE books Set status=2 WHERE Barcode= '" & _
                    Me.bookidfrmlend.Value & "'"
                    
                    updatepatronquota = "UPDATE patrons set quota =quota+1 WHERE [Patron Barcode]= '" & _
                    Me.cmbpbarcode.Value & "'"
                    
                    updatelastpatron = "UPDATE books set LastPatron = "
    updatelastpatron = updatelastpatron & "'" & rspatrons!ID & "'"
    updatelastpatron = updatelastpatron & " WHERE id ="
    updatelastpatron = updatelastpatron & rsbooks!ID
    
                    
                    DoCmd.RunSQL updatebkstatusonloan
                    DoCmd.RunSQL updatepatronquota
                    DoCmd.RunSQL updatelastpatron
                    Me.[DueDate].Value = rstrans![DueDate]
                    
    
                    
                    MsgBox rsbooks!title & "Item is Borrowed"
                    
        End If
    Last edited by issactang; Jan 6 '12, 12:46 PM. Reason: for clarity in psuedocode
  • Mariostg
    Contributor
    • Sep 2010
    • 332

    #2
    Maybe you want to provide more than pseudo-code here. Did you look at the Help in Access for the Do Loop statement?

    Code:
    Do While Not SomeValue
       ExecuteSomething
    Loop
    Code:
    Do While SomethingRemainsTrue
      ExecuteStuff
    Loop

    Comment

    • issactang
      New Member
      • Dec 2011
      • 25

      #3
      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

      Code:
      do while (condition)
      if a then msgbox a end if
      if b then msgbox b end if
      loop
      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,

      Comment

      • Mariostg
        Contributor
        • Sep 2010
        • 332

        #4
        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:
        Code:
        do while (condition) 
          if a then 
            msgbox a 
          end if 
          if b then 
            msgbox b 
          end if 
        loop
        Or (depending on the school of thoughts)
        Code:
        do while (condition) 
          if a then msgbox a  
          if b then msgbox b  
        loop
        Because there are no bracket, although indentation is not necessary, it is recommended to use it to make structure stands out.
        Last edited by Mariostg; Jan 6 '12, 02:11 PM. Reason: Correct other typos. I should really always preview...

        Comment

        • Mihail
          Contributor
          • Apr 2011
          • 759

          #5
          After all Mariostg say I think is nothing to add about Do-Loop structure.
          Here I rewrite your first pseudo-code for VB
          Code:
          do while (me.item.value<>"")
              if a then do a
              if b then do b
              if c then
                  do a
                  do a1
              end if
              if d then
                  do d
                  do Z
              end if
              if e then
                  do g
                  do h
              end if
          loop

          Comment

          • issactang
            New Member
            • Dec 2011
            • 25

            #6
            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,

            Comment

            • NeoPa
              Recognized Expert Moderator MVP
              • Oct 2006
              • 32669

              #7
              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.

              Comment

              Working...