Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 397
  • Last Modified:

Getting "Object variable or WITH block variable not set"

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
SteveL13
Asked:
SteveL13
  • 7
  • 4
1 Solution
 
mbizupCommented:
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
 
peter57rCommented:
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
 
mbizupCommented:
-->> 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
SteveL13Author Commented:
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
 
mbizupCommented:
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
 
SteveL13Author Commented:
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
 
mbizupCommented:
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
 
mbizupCommented:
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
 
SteveL13Author Commented:
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
 
mbizupCommented:
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
 
SteveL13Author Commented:
I wish I could give you an A+ and 5,000 points!  Thank you very much.
0
 
mbizupCommented:
Glad to help out, Steve!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 7
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now