user2002716
user2002716

Reputation: 120

VBA Add combo box values together

I have 3 combo boxes that contain numbers:

Me.Authorized
Me.Officer
Me.Enlisted

What I'm trying to do is add the values of Me.Officer and Me.Enlisted and make sure it equals Me.Authorized. I have all the other statements/commands figured out, but I can't wrap my head around this one.

Upvotes: 1

Views: 49

Answers (1)

HansUp
HansUp

Reputation: 97131

The combo box selected value is a string, even when that string contains only digits. You can use Val() to convert that string to a number.

So your required condition can be expressed as ...

Val(Me.Officer) + Val(Me.Enlisted) = Val(Me.Authorized)

You can enforce that requirement in the form's Before Update event ...

Private Sub Form_BeforeUpdate(Cancel As Integer)
    If (Val(Me.Officer) + Val(Me.Enlisted) <> Val(Me.Authorized)) Then
        MsgBox "Officer plus Enlisted must be equal to Authorized."
        Cancel = True
    End If
End Sub

That event procedure will abort a record save when your requirement is not satisfied.

Upvotes: 2

Related Questions