Solved

Getting "Object variable or WITH block variable not set"

Posted on 2013-01-16
12
391 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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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…

717 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