Results 1 to 12 of 12
  1. #1
    Dadeo is offline Novice
    Windows 10 Access 2016
    Join Date
    Nov 2017
    Posts
    16

    Where did I go wrong HELP

    The below code I thought should work but doesn't, can someone tell me if there is a problem with it? My Inventory is not updating when I add the quantity and listed it as Received.



    Thank you all in Advance

    The Inventory_Qty and ItemCode is in the Inventory table. InOutQty and LineStatus is in the PurchaseDetails table. Both tables are joined by ItemCode which is in both tabels. Inventory table has it list as an Autonumber and the PurchaseDetails table listed as a Number. Both are listed as Primary Keys. Also I am not sure if the 'On Error GoTo InOutQty_error is correct

    Code:
    Private Sub LineStatus_AfterUpdate()
    
    Dim db As DAO.Database
    Dim rst As DAO.Recordset
    Set db = CurrentDb
    
    
    'On Error GoTo InOutQty_error
    
    
    Set rst = db.OpenRecordset("Select Inventory_Qty from Inventory where ItemCode =" & Me.ItemCode)
    
    
    With rst
        .Edit
        If Me.LineStatus.Value = "Received" Then
        
        !Inventory_Qty = !Inventory_Qty + Nz(Me.InOutQty, 0)
        End If
        If Me.LineStatus.Value = "Returned" Then
        
        !Inventory_Qty = !Inventory_Qty - Nz(Me.InOutQty, 0)
        .Update
        End If
    End With
    End Sub

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Well the on error is definitely not correct, as there other mention of it in the sub? However it is commented out?
    Have you walked through the code with F8 to see what the code is actually doing?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    You are updating only the returns, please try this (note the added indentation that should make it easier to follow the code):
    Code:
    Private Sub LineStatus_AfterUpdate()
    
    
    Dim db As DAO.Database
    Dim rst As DAO.Recordset
    Set db = CurrentDb
    
    
    
    
    'On Error GoTo InOutQty_error
    
    
    
    
    Set rst = db.OpenRecordset("Select Inventory_Qty from Inventory where ItemCode =" & Me.ItemCode)
    
    
    
    
    With rst
        .Edit
        	If Me.LineStatus.Value = "Received" Then    
        		!Inventory_Qty = !Inventory_Qty + Nz(Me.InOutQty, 0)
        	ElseIF Me.LineStatus.Value = "Returned" Then    
        		!Inventory_Qty = !Inventory_Qty - Nz(Me.InOutQty, 0
    	End IF
        .Update    
    End With
    
    
    End Sub
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  4. #4
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    Can LineStatus be neither Received or Returned when that control is updated?
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  5. #5
    Dadeo is offline Novice
    Windows 10 Access 2016
    Join Date
    Nov 2017
    Posts
    16
    Returned
    Received
    Confirmed

  6. #6
    Dadeo is offline Novice
    Windows 10 Access 2016
    Join Date
    Nov 2017
    Posts
    16
    The ElseIF fixed it Thank you very much. One more questions how do I get the On Error working my I was told to add the label to the following "On Error GoTo edit_error

    On the form itself I have the following code.

    General Declarations
    Code:
    Option Compare Database
    Option Explicit
    and under Form BeforeInsert I have


    Code:
    Private Sub Form_BeforeInsert(Cancel As Integer)
    On Error GoTo Err_Handler:
    
    
    Me.OrderNumber = Format(Now(), "yyyymmddhhnnss")
    Exit_Here:
     Exit Sub
    
    
    Err_Handler:
        MsgBox Err.Description, vbExclamation, "Error"
            Resume Exit_Here
    End Sub

  7. #7
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    Not what I was thinking (null) but still relevant. In that case FWIW,
    - if Confirmed means do nothing, I wouldn't create objects (rs - which neither yours or the suggested code reclaims that memory space) unless it passed a test for not being "Confirmed"
    - I would always use an error handler when dealing when creating recordset objects.

    how do I get the On Error working
    Don't use colon in the directive, only on the line you want to go to if there is an error
    On Error GoTo Err_Handler, not On Error GoTo Err_Handler:

    and

    Err_Hanlder:
    code here

    The colon makes the line a line label.
    Last edited by Micron; 11-04-2021 at 11:37 AM. Reason: added info
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  8. #8
    Dadeo is offline Novice
    Windows 10 Access 2016
    Join Date
    Nov 2017
    Posts
    16
    This is the new code the revieced works but the returns don't

    Code:
    Private Sub LineStatus_AfterUpdate()
    Dim db As DAO.Database
    Dim rst As DAO.Recordset
    Set db = CurrentDb
    
    
    'On Error GoTo InOutQty_error
    
    
    Set rst = db.OpenRecordset("Select Inventory_Qty from Inventory where ItemCode =" & Me.ItemCode)
    
    
    With rst
        .Edit
            If Me.LineStatus.Value = "Received" Then
                !Inventory_Qty = !Inventory_Qty + Nz(Me.InOutQty, 0)
            ElseIf Me.LineStatus.Value = "Returned" Then
                !Inventory_Qty = !Inventory_Qty - Nz(Me.InOutQty, 0)
            End If
        .Update
    End With
    
    
    End Sub

  9. #9
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    The lack of ElseIf would not be your problem. Doing the update in one or the other IF blocks meant that the update only occurs for the IF that is True. Moving the update should be the fix as Gicu noted. This is why proper indentation of code is extremely important. You probably would have spotted your error if it looked more like first example below. Also, as Welshgasman indicated, stepping through your code is TroubleShooting 101. Surely that would have shown you where the problem was if indentation did not reveal it.

    Code:
    With rst
       .Edit
          If Me.LineStatus.Value = "Received" Then
            !Inventory_Qty = !Inventory_Qty + Nz(Me.InOutQty, 0)
          End If
    
          If Me.LineStatus.Value = "Returned" Then
            !Inventory_Qty = !Inventory_Qty - Nz(Me.InOutQty, 0)
          End If
       .Update
    End With
    Code:
    If Me.LineStatus.Value = "Returned" Then
       !Inventory_Qty = !Inventory_Qty - Nz(Me.InOutQty, 0)
       .Update << will only update if LineStatus is "Returned"
    End If
    Still not working anyway? Well, what does that mean, error message, no result, wrong result, that part of the code is bypassed?
    This could be another approach but I don't expect it would perform any differently if your current code does not perform as expected. Mostly, it could be used to demonstrate a way to deal with "Confirmed":
    Code:
    Private Sub LineStatus_AfterUpdate()
    Dim db As DAO.Database
    Dim rst As DAO.Recordset
    
    On Error GoTo errHandler
    
    If Not Me.LineStatus = "Confirmed" Then
      Set db = CurrentDb
      Set rst = db.OpenRecordset("Select Inventory_Qty from Inventory where ItemCode =" & Me.ItemCode)
      With rst
        .Edit
          If Me.LineStatus = "Received" Then !Inventory_Qty = !Inventory_Qty + Nz(Me.InOutQty, 0)
          If Me.LineStatus = "Returned" Then !Inventory_Qty = !Inventory_Qty - Nz(Me.InOutQty, 0)
        .Update
      End With
    End If
    
    exitHere:
    Set db = Nothing
    Set rs = Nothing
    Exit Sub
    
    errHandler:
    Msgbox "Error " & Err.Number & ": " & Err.Description
    Resume exitHere
    
    End Sub
    That presumes LineStatus can only be 1 of 3 possibilities and nothing is to be done if the update is "Confirmed"
    Last edited by Micron; 11-04-2021 at 12:11 PM. Reason: clarification
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  10. #10
    Dadeo is offline Novice
    Windows 10 Access 2016
    Join Date
    Nov 2017
    Posts
    16
    Sorry there Gicu, the returned was labeled return and that is why it didn't work. Thank you for you help you guys are great.

  11. #11
    Dadeo is offline Novice
    Windows 10 Access 2016
    Join Date
    Nov 2017
    Posts
    16
    This works also just had to change the Returned to Return. I thought being I had the error handling on top it would work on the bottom. You count that towards a NEWBI Error. Thanks so much for you help. I wish all forums were as good as the group here.

  12. #12
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    Quote Originally Posted by Dadeo View Post
    Thanks so much for you help. I wish all forums were as good as the group here.
    Well that's a whole lot nicer than some of the thanks we get.

    If by
    I thought being I had the error handling on top it would work on the bottom.
    you are referring to Returned vs Return, I think it's safe to say that code does not raise errors that arise from logic errors - only incorrect syntax and other invalid statements.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. What am I doing wrong here?
    By ijo68 in forum Programming
    Replies: 7
    Last Post: 10-03-2021, 09:39 PM
  2. what's wrong with this one?
    By markpastoril in forum Access
    Replies: 7
    Last Post: 07-12-2019, 12:56 AM
  3. what is wrong ?
    By dino in forum Access
    Replies: 19
    Last Post: 07-29-2015, 11:13 AM
  4. What's wrong!
    By khalid in forum Programming
    Replies: 15
    Last Post: 06-27-2011, 06:38 AM
  5. What is wrong with this IFF?
    By bburton in forum Reports
    Replies: 2
    Last Post: 03-16-2011, 10:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums