0

I have a project dealing with income taxes that has a userform with option buttons. It is set to create a max of 2 records. I am validating the option button selection is correct, i.e. 'Married Filing Jointly' on one record can has to be the same on the other record. There are several other variations of validation. The validation function is ControlValidate in the userform.

On the Save button click event, I am checking to see which option button is set to True and then run ControlValidate to do the scenario testing. Here is the code giving me fits.

    Dim FilingStatus As String
    Dim CheckFor As String
    Dim CurrentControl As Control
    Dim ValidateTaxFiling  As Boolean

        CheckFor = FilingStatus
        With Me.Controls
            ValidateTaxFiling = True
            If Me.obHH = True Then
                FilingStatus = Me.obHH.Caption
                Set CurrentControl = Me.obHH 'option button
                CheckFor = CurrentControl.Name
                Cancel = Not ControlValidate(CheckFor, CurrentControl, ValidateTaxFiling)
                [...a few Elseifs....]
            End If
        End With

My problem is that the line Set CurrentControl = Me.obHH returns the boulean value of the Me.obHH. I need the control name passed to the valdiation function. None of the options I have tried gets me there. Me.obHH.Name gives me a type mismatch (understandably). Presumable, if I can get the name of the option, I can do thisin a loop instead of nested if/elseifs. Any ideas? I appreciate any help.

westinq
  • 29
  • 1
  • 9
  • 1
    This might be relevant https://stackoverflow.com/questions/22765341/vba-get-true-radio-button-val-from-group – CDP1802 Apr 11 '21 at 22:13
  • @CDP1802 I took a look at this link. Yes his code returns the control name. But I already have that in the ```CheckFor``` in my code. The issue is that ```ControlValidate``` has several case statements where I am validating input into textboxes beforeupdate events. With the function working on textboxes, I always assumed that CurrentControl was passing the object, not the value. But when stepped through on a textbox eval, I noticed it is passing the value there as well. I mistakenly thought Option buttons were unique. The loop in the link is a good idea and save many lines of code. Thanks. – westinq Apr 12 '21 at 13:52
  • To further explain, if a textbox fails validation, I have it highlight the textbox (CurrentControl). And given that CurrentControl returns a value, I am not sure how it is working. But now I have learned something. – westinq Apr 12 '21 at 13:55
  • 1
    @westing `value` is probably optional for that object so when you assign the object to a variable you see the value even though it is in fact an object. Use `TypeName(Me.obHH)` to prove it. As `CheckFor` is a property of `CurrentControl` you really only need to pass `CurrentControl` to your function. – CDP1802 Apr 12 '21 at 14:06

0 Answers0