1
votes

In Ms Access, I have two unbound combo-boxes: StateBox and DVPCBox. StateBox is simply a list of U.S. states and DVPCBox contains Employee Names from a query based on the value of StateBox.

I'm trying to set the value of DVPCBox equal to the first item in its list. Since the list of Employees is based on the value of StateBox, I need the value of DVPCBox to update every time StateBox changes. I tried the following:

Private Sub StateBox_AfterUpdate()
    Me.DVPCBox.Requery
    If (Me.DVPCBox.ListCount = 1) Then
        Me.DVPCBox.SetFocus
        Me.DVPCBox.ListIndex = 0 //<-Error here
    End If
End Sub

But I got runtime error 2115 - The macro or function set to the BeforeUpdate or ValidationRule property for this field is preventing Microsoft Office Access from saving the data in the field.

The strangest thing to me is that I'm not even using the BeforeUpdate Event or ValidationRule (as far as I'm aware.)

1

1 Answers

1
votes

ItemData(0) is the first combo box value. So set the combo equal to that.

Private Sub StateBox_AfterUpdate()
    Me.DVPCBox.Requery
    If (Me.DVPCBox.ListCount >= 1) Then
        Me.DVPCBox.SetFocus
        'Me.DVPCBox.ListIndex = 0 //<-Error here
        Me.DVPCBox = Me.DVPCBox.ItemData(0)
    End If
End Sub

I also changed ListCount >= 1 because I assumed you wanted to do the same thing when the combo includes 2 or more rows.