Solved

Getting "Object variable or WITH block variable not set"

Posted on 2013-01-16
12
389 Views
Last Modified: 2013-01-16
I'm getting an error on the onclick event of a command button...  "Object variable or With block variable not set".  Here's the code...  (I believe it is somewhere after " 'Start of code to write records to tblFinGoods if part IS serialized and applied qty is >0"



Private Sub cmdMainMenu_Click()
On Error GoTo Err_cmdMainMenu_Click
   
    If Me.chkbxFinGoodsInvApplied = True Then
   
    Dim intLoop As Integer
    Dim rs As dao.Recordset

    'Start of code to write records to tblFinGoods if part is NOT serialized and applied qty is >0
    If Me.chkbxSerialize = False Then

    Set rs = CurrentDb.OpenRecordset("tblFinGoods")

    'For intLoop = 1 To IIf(Me.chkbxSerialize = False And Me.txtAppliedInvQty > 0, 1, Me.txtAppliedInvQty)

        rs.AddNew

        rs!OpenOrdRecID = txtRecID
        rs!Cust = Me.txtCustomer_1
        rs!DateFin = #10/10/2020#
        rs!PartN = Me.txtPart_No
        rs!Qty = Me.txtAppliedInvQty
        rs!Comments = "This record is from applied inventory from the Open Order record entry."
       
        rs.Update
               
    End If

    rs.Close
    'End of code to write records to tblFinGoods if part is NOT serialized and applied qty is >0
   
   
   
    'Start of code to write records to tblFinGoods if part IS serialized and applied qty is >0
    If Me.chkbxSerialize = True Then
   
    Set rs = CurrentDb.OpenRecordset("tblFinGoods")

    For intLoop = 1 To IIf(Me.chkbxSerialize = True And Me.txtAppliedInvQty > 0, 1, Me.txtAppliedInvQty)

        rs.AddNew

        rs!OpenOrdRecID = txtRecID
        rs!Cust = txtCustomer_1
        rs!DateFin = #10/10/2020#
        rs!PartN = Me.txtPart_No
        rs!Qty = Me.txtAppliedInvQty
        rs!Comments = "This record is from applied inventory from the Open Order record entry."

        If Me.chkbxSerialize = True Then
            rs!Serial = "ToCome"
            rs!SerialUsed = True
        End If
       
        rs.Update
               
    Next

    rs.Close

    End If
    'End of code to write records to tblFinGoods if part IS serialized and applied qty is >0


    End If

    AllowClose = True

    DoCmd.Close
   
    DoCmd.OpenForm "frmMainMenu"

Exit_cmdMainMenu_Click:
    Exit Sub

Err_cmdMainMenu_Click:
    MsgBox Err.Description
    Resume Exit_cmdMainMenu_Click
   
End Sub
0
Comment
Question by:SteveL13
[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
  • 7
  • 4
12 Comments
 
LVL 61

Expert Comment

by:mbizup
ID: 38783079
Try temporarily removing (commenting out) the "On Error GoTo Err_cmdMainMenu_Click".

That will help you pinpoint exactly which line of code is failing.  Let us know which one it is.
0
 
LVL 77

Expert Comment

by:peter57r
ID: 38783080
You need to identify the exact line for the error.
Comment out the On Error statament in the second line and then run the code to pinpoint the problem.
0
 
LVL 61

Expert Comment

by:mbizup
ID: 38783094
-->> DoCmd.Close

As an aside, I'd recommend changing this line to:

DoCmd.Close acForm, Me.Name

To specify exactly what you're closing.
0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

Author Comment

by:SteveL13
ID: 38783125
After commenting out the Or Error line and running the form and clicking the command button I have a choice to debug.  When I do the line that reads  "rs.Close" right above 'End of code to write records to tblFinGoods if part is NOT serialized and applied qty is >0

is hilighted in yellow...

    rs.Close
    'End of code to write records to tblFinGoods if part is NOT serialized and applied qty is >0
0
 
LVL 61

Expert Comment

by:mbizup
ID: 38783203
Try moving the first rs.close inside the corresponding IF block:


Private Sub cmdMainMenu_Click()
On Error GoTo Err_cmdMainMenu_Click
    
    If Me.chkbxFinGoodsInvApplied = True Then
    
    Dim intLoop As Integer
    Dim rs As dao.Recordset

    'Start of code to write records to tblFinGoods if part is NOT serialized and applied qty is >0
    If Me.chkbxSerialize = False Then

    Set rs = CurrentDb.OpenRecordset("tblFinGoods")

    'For intLoop = 1 To IIf(Me.chkbxSerialize = False And Me.txtAppliedInvQty > 0, 1, Me.txtAppliedInvQty)

        rs.AddNew

        rs!OpenOrdRecID = txtRecID
        rs!Cust = Me.txtCustomer_1
        rs!DateFin = #10/10/2020#
        rs!PartN = Me.txtPart_No
        rs!Qty = Me.txtAppliedInvQty
        rs!Comments = "This record is from applied inventory from the Open Order record entry."
        
        rs.Update


        rs.Close                
    End If


    'End of code to write records to tblFinGoods if part is NOT serialized and applied qty is >0
    
    
    
    'Start of code to write records to tblFinGoods if part IS serialized and applied qty is >0
    If Me.chkbxSerialize = True Then
    
    Set rs = CurrentDb.OpenRecordset("tblFinGoods")

    For intLoop = 1 To IIf(Me.chkbxSerialize = True And Me.txtAppliedInvQty > 0, 1, Me.txtAppliedInvQty)

        rs.AddNew

        rs!OpenOrdRecID = txtRecID
        rs!Cust = txtCustomer_1
        rs!DateFin = #10/10/2020#
        rs!PartN = Me.txtPart_No
        rs!Qty = Me.txtAppliedInvQty
        rs!Comments = "This record is from applied inventory from the Open Order record entry."

        If Me.chkbxSerialize = True Then
            rs!Serial = "ToCome"
            rs!SerialUsed = True
        End If
        
        rs.Update
                
    Next

    rs.Close

    End If
    'End of code to write records to tblFinGoods if part IS serialized and applied qty is >0


    End If

    AllowClose = True

    DoCmd.Close acform, Me.name
    
    DoCmd.OpenForm "frmMainMenu"

Exit_cmdMainMenu_Click:
    Exit Sub

Err_cmdMainMenu_Click:
    MsgBox Err.Description
    Resume Exit_cmdMainMenu_Click
    
End Sub

Open in new window

0
 

Author Comment

by:SteveL13
ID: 38783261
I probaly have this all messed up.  Now I get "Compile error: Duplicate declaration in current scope."  And this is highlighted in blue..."rs As dao.Recordset"

Here is my latest code:  (with the first rs.close inside the corresponding IF block per your latest recommendation.

Private Sub cmdMainMenu_Click()
On Error GoTo Err_cmdMainMenu_Click
   
    If Me.chkbxFinGoodsInvApplied = True Then
   
    Set rs = CurrentDb.OpenRecordset("tblFinGoods")
   
    Dim intLoop As Integer
    Dim rs As dao.Recordset

    'Start of code to write records to tblFinGoods if part is NOT serialized and applied qty is >0
    If Me.chkbxSerialize = False Then

        rs.AddNew

        rs!OpenOrdRecID = txtRecID
        rs!Cust = Me.txtCustomer_1
        rs!DateFin = #10/10/2020#
        rs!PartN = Me.txtPart_No
        rs!Qty = Me.txtAppliedInvQty * -1
        rs!Comments = "This record is from applied inventory from the Open Order record entry."
       
        rs.Update
               
    End If

    'rs.Close
    'End of code to write records to tblFinGoods if part is NOT serialized and applied qty is >0
   
   
   
    'Start of code to write records to tblFinGoods if part IS serialized and applied qty is >0
    If Me.chkbxSerialize = True Then
   
    Set rs = CurrentDb.OpenRecordset("tblFinGoods")

    For intLoop = 1 To IIf(Me.chkbxSerialize = True And Me.txtAppliedInvQty > 0, 1, Me.txtAppliedInvQty)

        rs.AddNew

        rs!OpenOrdRecID = txtRecID
        rs!Cust = txtCustomer_1
        rs!DateFin = #10/10/2020#
        rs!PartN = Me.txtPart_No
        rs!Qty = 1 * -1
        rs!Comments = "This record is from applied inventory from the Open Order record entry."
        rs!Serial = "ToCome"
       
        rs.Update
               
    Next

    rs.Close

    End If
    'End of code to write records to tblFinGoods if part IS serialized and applied qty is >0


    End If

    AllowClose = True

    DoCmd.Close
   
    DoCmd.OpenForm "frmMainMenu"

Exit_cmdMainMenu_Click:
    Exit Sub

Err_cmdMainMenu_Click:
    MsgBox Err.Description
    Resume Exit_cmdMainMenu_Click
   
End Sub
0
 
LVL 61

Expert Comment

by:mbizup
ID: 38783339
It looks like you moved your declarations too.  No need to do that, and that is what's causing the most recent error:

Private Sub cmdMainMenu_Click()
On Error GoTo Err_cmdMainMenu_Click

    Dim intLoop As Integer
    Dim rs As dao.Recordset
    
    If Me.chkbxFinGoodsInvApplied = True Then
    
    Set rs = CurrentDb.OpenRecordset("tblFinGoods")
    


    'Start of code to write records to tblFinGoods if part is NOT serialized and applied qty is >0
    If Me.chkbxSerialize = False Then

        rs.AddNew

        rs!OpenOrdRecID = txtRecID
        rs!Cust = Me.txtCustomer_1
        rs!DateFin = #10/10/2020#
        rs!PartN = Me.txtPart_No
        rs!Qty = Me.txtAppliedInvQty * -1
        rs!Comments = "This record is from applied inventory from the Open Order record entry."
        
        rs.Update
        'rs.Close         
    End If

    rs.Close
    'End of code to write records to tblFinGoods if part is NOT serialized and applied qty is >0
    
    
    
    'Start of code to write records to tblFinGoods if part IS serialized and applied qty is >0
    If Me.chkbxSerialize = True Then
    
    Set rs = CurrentDb.OpenRecordset("tblFinGoods")

    For intLoop = 1 To IIf(Me.chkbxSerialize = True And Me.txtAppliedInvQty > 0, 1, Me.txtAppliedInvQty)

        rs.AddNew

        rs!OpenOrdRecID = txtRecID
        rs!Cust = txtCustomer_1
        rs!DateFin = #10/10/2020#
        rs!PartN = Me.txtPart_No
        rs!Qty = 1 * -1
        rs!Comments = "This record is from applied inventory from the Open Order record entry."
        rs!Serial = "ToCome"
        
        rs.Update
                
    Next

    rs.Close

    End If
    'End of code to write records to tblFinGoods if part IS serialized and applied qty is >0


    End If

    AllowClose = True

    DoCmd.Close acForm, Me.Name
    
    DoCmd.OpenForm "frmMainMenu"

Exit_cmdMainMenu_Click:
    Exit Sub

Err_cmdMainMenu_Click:
    MsgBox Err.Description
    Resume Exit_cmdMainMenu_Click
    
End Sub

Open in new window

0
 
LVL 61

Expert Comment

by:mbizup
ID: 38783377
Try this too...

Private Sub cmdMainMenu_Click()
On Error GoTo Err_cmdMainMenu_Click

    Dim intLoop As Integer
    Dim rs As DAO.Recordset
    
    If Me.chkbxFinGoodsInvApplied = True Then
    
    
    
    'Start of code to write records to tblFinGoods if part is NOT serialized and applied qty is >0
    If Me.chkbxSerialize = False Then
        Set rs = CurrentDb.OpenRecordset("tblFinGoods")
        rs.AddNew
        rs!OpenOrdRecID = txtRecID
        rs!cust = Me.txtCustomer_1
        rs!DateFin = #10/10/2020#
        rs!PartN = Me.txtPart_No
        rs!Qty = Me.txtAppliedInvQty * -1
        rs!Comments = "This record is from applied inventory from the Open Order record entry."        
        rs.Update
        rs.Close
    End If    
    'End of code to write records to tblFinGoods if part is NOT serialized and applied qty is >0


   
    'Start of code to write records to tblFinGoods if part IS serialized and applied qty is >0
    If Me.chkbxSerialize = True Then    
        Set rs = CurrentDb.OpenRecordset("tblFinGoods")
        For intLoop = 1 To IIf(Me.chkbxSerialize = True And Me.txtAppliedInvQty > 0, 1, Me.txtAppliedInvQty)
            rs.AddNew
            rs!OpenOrdRecID = txtRecID
            rs!cust = txtCustomer_1
            rs!DateFin = #10/10/2020#
            rs!PartN = Me.txtPart_No
            rs!Qty = 1 * -1
            rs!Comments = "This record is from applied inventory from the Open Order record entry."
            rs!Serial = "ToCome"        
            rs.Update                
        Next
        rs.Close
    End If
    'End of code to write records to tblFinGoods if part IS serialized and applied qty is >0


    End If


    AllowClose = True
    DoCmd.Close acForm, Me.Name    
    DoCmd.OpenForm "frmMainMenu"

Exit_cmdMainMenu_Click:
    Exit Sub

Err_cmdMainMenu_Click:
    MsgBox Err.Description
    Resume Exit_cmdMainMenu_Click
    
End Sub

Open in new window

0
 

Author Comment

by:SteveL13
ID: 38783513
Nice!  The errors are gone.  But now it is not looping and create "x" number of records if Me.chkbxSerialize = True

It is only creating one record.

???
0
 
LVL 61

Accepted Solution

by:
mbizup earned 500 total points
ID: 38783564
Ok - I saw you posted a new question and deleted it just as I was posting my response, so I lost most of what I wrote.   :(

<<
    For intLoop = 1 To IIf(Me.chkbxSerialize = True And Me.txtAppliedInvQty > 0, 1, Me.txtAppliedInvQty)
>>

This is a little redundant since you are already checking for Serialize = true in your IF statement.

You can simplify that line to:

    For intLoop = 1 To IIf( NZ(Me.txtAppliedInvQty, 0) > 0,  Me.txtAppliedInvQty, 1)

Open in new window


I've also corrected it by flipping the IIF arguments... I think your conditions were backwards.
0
 

Author Closing Comment

by:SteveL13
ID: 38783582
I wish I could give you an A+ and 5,000 points!  Thank you very much.
0
 
LVL 61

Expert Comment

by:mbizup
ID: 38783793
Glad to help out, Steve!
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Suggested Solutions

A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
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.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

733 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