Results 1 to 10 of 10
  1. #1
    anthgav is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Posts
    86

    VBA looking up Row Source incorrectly

    Hi

    I have a form called 'frmBin' which has a sub form called 'frmBin subform'.
    Within the subform I have some VBA which looks up some values and updates the database when 'binType' control is updated.



    Here is the VBA...
    Code:
    'Calculates the bin width based on product dimensions or bin type dimensions
    Private Sub recalculateBinSize()
            Dim requiredBinWidthByProduct As Long
            Dim requiredBinWidthByBinType As Integer
            Dim updateBinWidthMethod As String
            Dim binID As Integer
            Dim updateBinWidthByProduct As String
            Dim updateBinWidthByBinType As String
            
            requiredBinWidthByProduct = Nz([allocatedBinId].[Column](10), 0)
            Debug.Print "requiredBinWidthByProduct " & requiredBinWidthByProduct
            requiredBinWidthByBinType = Nz([allocatedBinId].[Column](6), 0)
            Debug.Print "requiredBinWidthByBinType " & requiredBinWidthByBinType
            updateBinWidthMethod = Nz([allocatedBinId].[Column](5), 0)
            Debug.Print "updateBinWidthMethod " & updateBinWidthMethod
            binID = Nz([allocatedBinId].[Column](4), 0)
            Debug.Print "binID " & binID
            updateBinWidthByProduct = "UPDATE tblBin SET [bin_width_mm] ='" & (requiredBinWidthByProduct) & "' WHERE [bin_id] = " & (binID)
            updateBinWidthByBinType = "UPDATE tblBin SET [bin_width_mm] ='" & (requiredBinWidthByBinType) & "' WHERE [bin_id] = " & (binID)
            
            If updateBinWidthMethod = -1 Then
                CurrentDb.Execute updateBinWidthByProduct
            ElseIf updateBinWidthMethod = 0 Then
                CurrentDb.Execute updateBinWidthByBinType
            End If
    End Sub
    
    
    Private Sub binType_AfterUpdate()
        recalculateBinSize
    End Sub
    The VBA looks up it's values from the Row Source attached to 'allocatedBinId' control.
    Everytime I update 'binType' the VBA runs but the values being pulled from the Row Source are not correct, I think it seems to pull the values from the original Row Source from when the page loads or the first location that's loaded, I tried different things to make the Row Source provide the correct information but I'm struggling to make it work correctly. I am able to identify this as I've added some debug lines and are viewing the numbers in the immediate window.

    I've attached a sample database with my form.

    PlannerBinForm.zip

    I would be greatful for some assistance on this.

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,772
    First, don't see need to include lookup tables in the subform RecordSource, although shouldn't hurt if join types are correct. However, join between tblBin and tblAllocatedBin in qryBinSubFormA appears backwards. Just use tblAllocatedBin as RecordSource.

    Second, also don't see need to run UPDATE action, just set value of textbox on main form: Me.Parent.binWidthBox = requiredBinWidthByProduct

    Also, [bin_width_mm] is a number type field which means do not use apostrophe delimiters.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    anthgav is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Posts
    86

    Post

    Quote Originally Posted by June7 View Post
    First, don't see need to include lookup tables in the subform RecordSource, although shouldn't hurt if join types are correct. However, join between tblBin and tblAllocatedBin in qryBinSubFormA appears backwards. Just use tblAllocatedBin as RecordSource.

    Second, also don't see need to run UPDATE action, just set value of textbox on main form: Me.Parent.binWidthBox = requiredBinWidthByProduct

    Also, [bin_width_mm] is a number type field which means do not use apostrophe delimiters.
    Thanks

    I've changed the rowsource directly to the table and updated the VBA to the below, I've also added Me.allocatedBinId.Requery to try and update the rowsource after a change to binType.
    Code:
    'Calculates the bin width based on product dimensions or bin type dimensions
    Private Sub recalculateBinSize()
            Dim requiredBinWidthByProduct As Long
            Dim requiredBinWidthByBinType As Integer
            Dim updateBinWidthMethod As String
            Dim binID As Integer
            Dim updateBinWidthByProduct As String
            Dim updateBinWidthByBinType As String
            
            requiredBinWidthByProduct = Nz([allocatedBinId].[Column](10), 0)
            Debug.Print "requiredBinWidthByProduct " & requiredBinWidthByProduct
            requiredBinWidthByBinType = Nz([allocatedBinId].[Column](6), 0)
            Debug.Print "requiredBinWidthByBinType " & requiredBinWidthByBinType
            updateBinWidthMethod = Nz([allocatedBinId].[Column](5), 0)
            Debug.Print "updateBinWidthMethod " & updateBinWidthMethod
            
            If updateBinWidthMethod = -1 Then
                Me.Parent.binWidthBox = requiredBinWidthByProduct
            ElseIf updateBinWidthMethod = 0 Then
                Me.Parent.binWidthBox = requiredBinWidthByBinType
            End If
    End Sub
    
    
    Private Sub binType_AfterUpdate()
        Me.allocatedBinId.Requery
        recalculateBinSize
    End Sub
    The rowsource still doesn't seem to be updating, what else do I need to do?

    See updated file below.
    PlannerBinForm(2).zip
    Last edited by anthgav; 07-21-2018 at 11:20 AM.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,772
    I said subform RecordSource needed fixing. I did not mention a RowSource.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    anthgav is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Posts
    86
    Quote Originally Posted by June7 View Post
    I said subform RecordSource needed fixing. I did not mention a RowSource.
    Sorry, so you did, here is my updated version with RecordSource changed.

    PlannerBinForm(3).zip

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,772
    You have combobox bound to autonumber allocated_bin_ID. This makes no sense. Should probably be allocated_bin field.

    Exactly what records are you trying to list in combobox? If changing bin type impacts the RowSource then record edit must be committed to table before combobox is requeried. Record is committed when: 1. Close table/query/form or 2. move to another record or 3. run code to save.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    anthgav is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Posts
    86
    Ok, I've changed that. Any idea why the RowSource doesn't update the new details from the binType?
    PlannerBinForm(4).zip

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,772
    I edited previous post possibly after you read it. Have you added code to save record then Requery combobox?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  9. #9
    anthgav is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Posts
    86
    The RowSource for Allocated Bin combo box was added purely to provide my values for the VBA instead of doing numerous DLookups, the box will actually be hidden once I get this working. The only changeable value on the sub form is the Bin Type combo box, I need to be able to change the Bin Type and then set the Bin Width mm in the parent form once this has been changed. All the other fields are there so I can see what is in that Bin Type but are intentionally not changeable as that's done in another form. I added the Requery and renamed the Allocated Bin control after you asked me to change it but forgot to change the name in the VBA which I've now corrected.

    You were correct, the form needs to save before the RowSource can be updated, I added 'DoCmd.RunCommand acCmdSaveRecord' into the AfterUpdate and it is working how I want it to now.

    Code:
    'Calculates the bin width based on product dimensions or bin type dimensions
    Private Sub recalculateBinSize()
            Dim requiredBinWidthByProduct As Long
            Dim requiredBinWidthByBinType As Integer
            Dim updateBinWidthMethod As String
            Dim binID As Integer
            Dim updateBinWidthByProduct As String
            Dim updateBinWidthByBinType As String
            
            updateBinWidthMethod = Nz([allocatedBin].[Column](5), 0)
            Debug.Print "updateBinWidthMethod " & updateBinWidthMethod
            
            If updateBinWidthMethod = -1 Then
                requiredBinWidthByProduct = Nz([allocatedBin].[Column](10), 0)
                Debug.Print "requiredBinWidthByProduct " & requiredBinWidthByProduct
                Me.Parent.binWidthBox = requiredBinWidthByProduct
            ElseIf updateBinWidthMethod = 0 Then
                requiredBinWidthByBinType = Nz([allocatedBin].[Column](6), 0)
                Debug.Print "requiredBinWidthByBinType " & requiredBinWidthByBinType
                Me.Parent.binWidthBox = requiredBinWidthByBinType
            End If
    End Sub
    
    
    Private Sub binType_AfterUpdate()
        DoCmd.RunCommand acCmdSaveRecord
        Me.allocatedBin.Requery
        recalculateBinSize
    End Sub
    Is this method OK or is there a better way of doing it?

    New file
    PlannerBinForm(5).zip

  10. #10
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,772
    If it works then method is OK. Not going to second guess your design. Just glad it's working now.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

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

Similar Threads

  1. Replies: 19
    Last Post: 06-26-2018, 07:13 AM
  2. What am I doing incorrectly?
    By Lou_Reed in forum Access
    Replies: 4
    Last Post: 07-28-2015, 01:12 PM
  3. Data Repeating Incorrectly
    By user9 in forum Forms
    Replies: 5
    Last Post: 08-21-2014, 03:48 PM
  4. Replies: 5
    Last Post: 08-20-2014, 12:37 PM
  5. This expression is typed incorrectly
    By Ray67 in forum Queries
    Replies: 9
    Last Post: 01-03-2012, 12:53 PM

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