Solved

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

Posted on 2016-11-07
8
79 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 96

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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
This article describes a serious pitfall that can happen when deleting shapes using VBA.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

726 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