Solved

Ms Access VBA Where would I place my End If during a Loop

Posted on 2016-11-07
8
57 Views
Last Modified: 2016-11-07
Ok I am having trouble with this code and placing the End If. I have 2 If Statements. One for the Loop and one for the Dcount.

If everything I understand is true then.... I have to have the End If for the loop after LOOP.

But I have a If statement for the Dcount in the middle of that.

Where do I place the Dcount End If so it doesn't mess with the If Loop Statement?

If I place it after the Loop won't it carry over on the next records Loop?


Thanks!


Private Sub Command34_Click()
    'DoCmd.SetWarnings False
    If Not (Recordset.EOF And Recordset.BOF) Then
        Recordset.MoveFirst 'Good habit
            Do Until Recordset.EOF = True
            
    DoCmd.OpenQuery "qryStockTransferDeductFrom" ' Deducts the quantity from QtyLoc in the ProdLocations Table.
    
        If DCount("*", "[ProdLocations]", "[ProductID] =" & Me.ProductID & " AND [LocID] =" & Me.LocID) > 0 Then
            DoCmd.OpenQuery "qryStockTransferAddTo" 'Updates QtyLoc in the ProdLocations Table If there is one already existing from the Dcount.
         Else
        DoCmd.OpenQuery "qryStockTransferAddAppend" 'Adds a record to ProdLocations Table if one doesn't already exist from the Dcount.
    
  

'Move to the next record. Don't ever forget to do this.
        Recordset.MoveNext
        
    Loop
Else
    MsgBox "There are no records in the recordset."
End If
End If

MsgBox "Finished looping through records."

'Recordset.Close 'Close the recordset
'Set Recordset = Nothing 'Clean up
'DoCmd.SetWarnings True
End Sub

Open in new window

0
Comment
Question by:Dustin Stanley
  • 4
  • 3
8 Comments
 
LVL 95

Assisted Solution

by:Lee W, MVP
Lee W, MVP earned 250 total points
ID: 41877699
This is why proper indenting is important!
Private Sub Command34_Click()
    'DoCmd.SetWarnings False
    If Not (Recordset.EOF And Recordset.BOF) Then
            Recordset.MoveFirst 'Good habit
            Do Until Recordset.EOF = True
            
                   DoCmd.OpenQuery "qryStockTransferDeductFrom" ' Deducts the quantity from QtyLoc in the ProdLocations Table.
    
                   If DCount("*", "[ProdLocations]", "[ProductID] =" & Me.ProductID & " AND [LocID] =" & Me.LocID) > 0 Then
                            DoCmd.OpenQuery "qryStockTransferAddTo" 'Updates QtyLoc in the ProdLocations Table If there is one already existing from the Dcount.
                   Else
                            DoCmd.OpenQuery "qryStockTransferAddAppend" 'Adds a record to ProdLocations Table if one doesn't already exist from the Dcount.
'With proper indenting, you would see THIS is where you need an [b]End If[/b]!
                  'Move to the next record. Don't ever forget to do this.
                  Recordset.MoveNext
           Loop
   Else
            MsgBox "There are no records in the recordset."
   End If
'THIS End If doesn't belong!

    MsgBox "Finished looping through records."

    'Recordset.Close 'Close the recordset
    'Set Recordset = Nothing 'Clean up
    'DoCmd.SetWarnings True
End Sub

Open in new window

0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 41877700
place the first end if inside the loop.

btw, what is the purpose of these codes?

Private Sub Command34_Click()
    'DoCmd.SetWarnings False
    If Not (Recordset.EOF And Recordset.BOF) Then
        Recordset.MoveFirst 'Good habit
            Do Until Recordset.EOF = True
            
    DoCmd.OpenQuery "qryStockTransferDeductFrom" ' Deducts the quantity from QtyLoc in the ProdLocations Table.
    
        If DCount("*", "[ProdLocations]", "[ProductID] =" & Me.ProductID & " AND [LocID] =" & Me.LocID) > 0 Then
            DoCmd.OpenQuery "qryStockTransferAddTo" 'Updates QtyLoc in the ProdLocations Table If there is one already existing from the Dcount.
         Else
        DoCmd.OpenQuery "qryStockTransferAddAppend" 'Adds a record to ProdLocations Table if one doesn't already exist from the Dcount.
    
        End If

'Move to the next record. Don't ever forget to do this.
        Recordset.MoveNext
        
    Loop
Else
    MsgBox "There are no records in the recordset."

End If

MsgBox "Finished looping through records."

'Recordset.Close 'Close the recordset
'Set Recordset = Nothing 'Clean up
'DoCmd.SetWarnings True
End Sub

Open in new window

0
 

Author Comment

by:Dustin Stanley
ID: 41877707
This is why proper indenting is important!
Yes I totally understand ave been learnig as I go. I agree and have worrked on but it is way off. I will see if I can find some more info on it and better perfect it.

btw, what is the purpose of these codes?

This is so that i can transfer stock (Products) correctly.

Can you clarify for me something. When the Loop Re-Loops. Does it start at the Do Until, Before Do Until, or After Do Until?
0
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 41877715
when the Loop is called, it will check if the EOF is true, if it is the code exits the loop.
if EOF is false it will continue looping until eof is reached
0
 

Author Comment

by:Dustin Stanley
ID: 41877723
Ok but whet does the Loop exactly start in the code when it reloops.  When the code hits Loop where does it go next
If Not (Recordset.EOF And Recordset.BOF)?
0
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 250 total points
ID: 41877736
rewriting your codes for easy reading

Private Sub Command34_Click()
    'DoCmd.SetWarnings False
    If Recordset.EOF Then
	
		MsgBox "There are no records in the recordset."
		Exit sub
	end if
	
     Recordset.MoveFirst 'Good habit
     Do Until Recordset.EOF = True
            
    DoCmd.OpenQuery "qryStockTransferDeductFrom" ' Deducts the quantity from QtyLoc in the ProdLocations Table.
    
        If DCount("*", "[ProdLocations]", "[ProductID] =" & Me.ProductID & " AND [LocID] =" & Me.LocID) > 0 Then
            DoCmd.OpenQuery "qryStockTransferAddTo" 'Updates QtyLoc in the ProdLocations Table If there is one already existing from the Dcount.
         Else
        DoCmd.OpenQuery "qryStockTransferAddAppend" 'Adds a record to ProdLocations Table if one doesn't already exist from the Dcount.
    
        End If

'Move to the next record. Don't ever forget to do this.
        Recordset.MoveNext
        
    Loop

MsgBox "Finished looping through records."

'Recordset.Close 'Close the recordset
'Set Recordset = Nothing 'Clean up
'DoCmd.SetWarnings True
End Sub

Open in new window

0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 41877815
When the code hits Loop where does it go next
 If Not (Recordset.EOF And Recordset.BOF)?   YES
0
 

Author Closing Comment

by:Dustin Stanley
ID: 41877862
Thank you!
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Modern/Metro styled message box and input box that directly can replace MsgBox() and InputBox()in Microsoft Access 2013 and later. Also included is a preconfigured error box to be used in error handling.
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now