[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 2016-11-07
8
Medium Priority
?
96 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 1000 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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
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 1000 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

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

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

This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
Explore the ways to Unlock VBA Project Password Excel 2010 & 2013 documents. Go through the article and perform the steps carefully to remove VBA Excel .xls file.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

650 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