Results 1 to 7 of 7
  1. #1
    crimson is offline Novice
    Windows 7 Access 2013 32bit
    Join Date
    Nov 2010
    Posts
    11

    Action after combobox selection

    Hi,
    The problem is a bit strange. On a form I have a combobox cmbZP and 4 text boxes: zp, inpZP, M and MT. The data source for the combobox is a small table with 2 columns: ID and text, containing exacly 4 records.
    Depending on the choice in the combobox, the form performs different calculations. In order to enable the code I have a button with the VBA code attached to it. Here is the code:


    Code:
    Function calculate()
    Select Case cmbZP.ListIndex
    Case Is = 1
        Me.zp.Value = Me.inpZP.Value
    Case Is = 2
        Me.zp.Value = (Me.inpZP.Value * ((100 - Me.M.Value) / (100 - Me.MT.Value)))
    Case Is = 3
        Me.zp.Value = (Me.inpZP.Value * ((100 - Me.M.Value) / 100))
    Case Is = 4
        Me.zp.Value = (Me.inpZP.Value * (((100 - (Me.M.Value + Me.zp.Value)) / 100)))
    Case Else
        Me.zp.Value = Me.inpZP.Value
    End Select
    DoCmd.RunCommand acCmdSaveRecord
    DoCmd.Close acForm, Me.Name
    End Function
    The problem is that when I pick the second entry from the combobox, the form saves data, however does not preform the calculation, thus it acts as if I would pick the first entry. Any idea what may be wrong?

    Thanks in advance
    Regards

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,521
    why are you using listindex, and not the ACTUAL value the user picked?
    you cant assume the listindex is correct, but you can with the value


    Select Case cmbZP
    case "bob"

  3. #3
    crimson is offline Novice
    Windows 7 Access 2013 32bit
    Join Date
    Nov 2010
    Posts
    11
    I did it like this in the original approach, but it didn't work, so I wrote the code from my first post. The listindex will never change. The source table will remain uchanged the whole time.

    Regards

  4. #4
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,389
    Add the red line to see what's going on and you will see that the possible values of the index are 0,1,2,3.

    Code:
    Function calculate()
    debug.print cmbZP.ListIndex
    Select Case cmbZP.ListIndex
    Case Is = 1
        Me.zp.Value = Me.inpZP.Value
    Case Is = 2
        Me.zp.Value = (Me.inpZP.Value * ((100 - Me.M.Value) / (100 - Me.MT.Value)))
    Case Is = 3
        Me.zp.Value = (Me.inpZP.Value * ((100 - Me.M.Value) / 100))
    Case Is = 4
        Me.zp.Value = (Me.inpZP.Value * (((100 - (Me.M.Value + Me.zp.Value)) / 100)))
    Case Else
        Me.zp.Value = Me.inpZP.Value
    End Select
    DoCmd.RunCommand acCmdSaveRecord
    DoCmd.Close acForm, Me.Name
    End Function

  5. #5
    crimson is offline Novice
    Windows 7 Access 2013 32bit
    Join Date
    Nov 2010
    Posts
    11
    Hi,
    After changing the index to 0 - 3, as suggested by davegri, it works correctly only until I pick the first 3 options (0,1,2). After picking the last (index 3), it saves an empty field.
    Any other ideas?

    Regards

  6. #6
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,389
    Your results depend on the data in the table. With random test data, all four options work for me.
    In the formula for option 4, the calculation includes the result field itself in the calculation. Is that correct?
    How is the function called? By a button, by the after_update event of the combobox?
    Post a picture of your table so that I can see your data.

  7. #7
    crimson is offline Novice
    Windows 7 Access 2013 32bit
    Join Date
    Nov 2010
    Posts
    11
    Hi,
    You are right, the problem was in the last formula. Now everything works perfectly. Thank you, problem solved.

    Regards

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

Similar Threads

  1. Replies: 2
    Last Post: 08-22-2014, 01:20 PM
  2. Replies: 1
    Last Post: 09-06-2011, 01:47 PM
  3. Replies: 29
    Last Post: 08-16-2011, 05:52 PM
  4. Replies: 0
    Last Post: 08-24-2010, 06:38 PM
  5. Replies: 0
    Last Post: 12-16-2009, 01:14 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