Solved

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

Posted on 2016-11-07
8
50 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 119

Expert Comment

by:Rey Obrero
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
 
LVL 119

Expert Comment

by:Rey Obrero
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
Backup Your Microsoft Windows Server®

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

 

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 119

Accepted Solution

by:
Rey Obrero 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 119

Expert Comment

by:Rey Obrero
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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.

914 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

20 Experts available now in Live!

Get 1:1 Help Now