Solved

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

Posted on 2016-11-07
8
42 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

 

Author Comment

by:Dustin Stanley
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you!
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Dcount unique 6 21
Excel VBA - open a DataValidation dropdown 6 19
TT ADD Formula 2 9
Changing absolute cell references 3 14
This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
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…
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

772 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

12 Experts available now in Live!

Get 1:1 Help Now