Setting Tag Property of UserForm TextBox












0















I need some help with setting the Tag Property of a TextBox in a UserForm when the form activates and then checking to make sure that the Text Boxes with that Tag match the conditions when the Save Button is clicked. I believe the issue to be with how I am setting the tag property when the UserForm activates, but I wouldnt be surprised if there is something wrong in the logic of the code when the UserForm Activates. Thank you in advance for your assistance.



Code for when the UserForm Activates:



Private Sub UserForm_Activate()
Dim i As Long

For Each ctrl In UWchecklist.Controls
Select Case TypeName(ctrl)
Case Is = "ComboBox"
If ctrl.Tag = "yesno" Then
ctrl.RowSource = "YesNo"
End If
ctrl.Font.Size = 10
Case Is = "TextBox"
ctrl.Font.Size = 10
For i = 1 To 30
If ctrl.Name = ("uwtxTB" & i) Then
ctrl.Tag = "Check"
End If
Next i
End Select
Next ctrl
End Sub


Code for when the Save Button is Clicked:



Private Sub SandCont_Click()

For Each ctrl In UWchecklist.Controls
If TypeName(ctrl) = "TextBox" Then
If ctrl.Tag = "Check" Then
If Not ctrl.Value = vbNullString And Not ctrl.Value = "X" Or Not ctrl.Value = "NA" Then
ctrl.Value = vbNullString
ctrl.BackColor = vbRed
MsgBox "Only a value of " & "X " & "or " & "NA " & "is allowed.", 0, "DATA ENTRY ERROR"
End If
End If
End If
Next ctrl

End Sub









share|improve this question


















  • 1





    Your If condition is completely tortured. Based on the MsgBox, it should be more like If ctrl.Value <> "X" And ctrl.Value <> "NA" Then.

    – Comintern
    Nov 14 '18 at 21:14











  • Yeah I noticed that and changed it completely. See answer below. Thanks.

    – Zack E
    Nov 14 '18 at 21:55
















0















I need some help with setting the Tag Property of a TextBox in a UserForm when the form activates and then checking to make sure that the Text Boxes with that Tag match the conditions when the Save Button is clicked. I believe the issue to be with how I am setting the tag property when the UserForm activates, but I wouldnt be surprised if there is something wrong in the logic of the code when the UserForm Activates. Thank you in advance for your assistance.



Code for when the UserForm Activates:



Private Sub UserForm_Activate()
Dim i As Long

For Each ctrl In UWchecklist.Controls
Select Case TypeName(ctrl)
Case Is = "ComboBox"
If ctrl.Tag = "yesno" Then
ctrl.RowSource = "YesNo"
End If
ctrl.Font.Size = 10
Case Is = "TextBox"
ctrl.Font.Size = 10
For i = 1 To 30
If ctrl.Name = ("uwtxTB" & i) Then
ctrl.Tag = "Check"
End If
Next i
End Select
Next ctrl
End Sub


Code for when the Save Button is Clicked:



Private Sub SandCont_Click()

For Each ctrl In UWchecklist.Controls
If TypeName(ctrl) = "TextBox" Then
If ctrl.Tag = "Check" Then
If Not ctrl.Value = vbNullString And Not ctrl.Value = "X" Or Not ctrl.Value = "NA" Then
ctrl.Value = vbNullString
ctrl.BackColor = vbRed
MsgBox "Only a value of " & "X " & "or " & "NA " & "is allowed.", 0, "DATA ENTRY ERROR"
End If
End If
End If
Next ctrl

End Sub









share|improve this question


















  • 1





    Your If condition is completely tortured. Based on the MsgBox, it should be more like If ctrl.Value <> "X" And ctrl.Value <> "NA" Then.

    – Comintern
    Nov 14 '18 at 21:14











  • Yeah I noticed that and changed it completely. See answer below. Thanks.

    – Zack E
    Nov 14 '18 at 21:55














0












0








0








I need some help with setting the Tag Property of a TextBox in a UserForm when the form activates and then checking to make sure that the Text Boxes with that Tag match the conditions when the Save Button is clicked. I believe the issue to be with how I am setting the tag property when the UserForm activates, but I wouldnt be surprised if there is something wrong in the logic of the code when the UserForm Activates. Thank you in advance for your assistance.



Code for when the UserForm Activates:



Private Sub UserForm_Activate()
Dim i As Long

For Each ctrl In UWchecklist.Controls
Select Case TypeName(ctrl)
Case Is = "ComboBox"
If ctrl.Tag = "yesno" Then
ctrl.RowSource = "YesNo"
End If
ctrl.Font.Size = 10
Case Is = "TextBox"
ctrl.Font.Size = 10
For i = 1 To 30
If ctrl.Name = ("uwtxTB" & i) Then
ctrl.Tag = "Check"
End If
Next i
End Select
Next ctrl
End Sub


Code for when the Save Button is Clicked:



Private Sub SandCont_Click()

For Each ctrl In UWchecklist.Controls
If TypeName(ctrl) = "TextBox" Then
If ctrl.Tag = "Check" Then
If Not ctrl.Value = vbNullString And Not ctrl.Value = "X" Or Not ctrl.Value = "NA" Then
ctrl.Value = vbNullString
ctrl.BackColor = vbRed
MsgBox "Only a value of " & "X " & "or " & "NA " & "is allowed.", 0, "DATA ENTRY ERROR"
End If
End If
End If
Next ctrl

End Sub









share|improve this question














I need some help with setting the Tag Property of a TextBox in a UserForm when the form activates and then checking to make sure that the Text Boxes with that Tag match the conditions when the Save Button is clicked. I believe the issue to be with how I am setting the tag property when the UserForm activates, but I wouldnt be surprised if there is something wrong in the logic of the code when the UserForm Activates. Thank you in advance for your assistance.



Code for when the UserForm Activates:



Private Sub UserForm_Activate()
Dim i As Long

For Each ctrl In UWchecklist.Controls
Select Case TypeName(ctrl)
Case Is = "ComboBox"
If ctrl.Tag = "yesno" Then
ctrl.RowSource = "YesNo"
End If
ctrl.Font.Size = 10
Case Is = "TextBox"
ctrl.Font.Size = 10
For i = 1 To 30
If ctrl.Name = ("uwtxTB" & i) Then
ctrl.Tag = "Check"
End If
Next i
End Select
Next ctrl
End Sub


Code for when the Save Button is Clicked:



Private Sub SandCont_Click()

For Each ctrl In UWchecklist.Controls
If TypeName(ctrl) = "TextBox" Then
If ctrl.Tag = "Check" Then
If Not ctrl.Value = vbNullString And Not ctrl.Value = "X" Or Not ctrl.Value = "NA" Then
ctrl.Value = vbNullString
ctrl.BackColor = vbRed
MsgBox "Only a value of " & "X " & "or " & "NA " & "is allowed.", 0, "DATA ENTRY ERROR"
End If
End If
End If
Next ctrl

End Sub






vba excel-2013






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 21:03









Zack EZack E

9910




9910








  • 1





    Your If condition is completely tortured. Based on the MsgBox, it should be more like If ctrl.Value <> "X" And ctrl.Value <> "NA" Then.

    – Comintern
    Nov 14 '18 at 21:14











  • Yeah I noticed that and changed it completely. See answer below. Thanks.

    – Zack E
    Nov 14 '18 at 21:55














  • 1





    Your If condition is completely tortured. Based on the MsgBox, it should be more like If ctrl.Value <> "X" And ctrl.Value <> "NA" Then.

    – Comintern
    Nov 14 '18 at 21:14











  • Yeah I noticed that and changed it completely. See answer below. Thanks.

    – Zack E
    Nov 14 '18 at 21:55








1




1





Your If condition is completely tortured. Based on the MsgBox, it should be more like If ctrl.Value <> "X" And ctrl.Value <> "NA" Then.

– Comintern
Nov 14 '18 at 21:14





Your If condition is completely tortured. Based on the MsgBox, it should be more like If ctrl.Value <> "X" And ctrl.Value <> "NA" Then.

– Comintern
Nov 14 '18 at 21:14













Yeah I noticed that and changed it completely. See answer below. Thanks.

– Zack E
Nov 14 '18 at 21:55





Yeah I noticed that and changed it completely. See answer below. Thanks.

– Zack E
Nov 14 '18 at 21:55












1 Answer
1






active

oldest

votes


















0














I figured it out; the issue wasn't in the UserForm Activate. It was in the Save and Cont Code. I dont think I was looking at enough of the If conditions, so I set up a Select Case and it works now. It's probably not the most elegant solution.



For Each ctrl In UWchecklist.Controls
If ctrl.Tag = "Check" Then
Select Case ctrl.Value
Case Is = ""
ctrl.BackColor = vbWhite
Case Is = "X"
ctrl.BackColor = vbWhite
ctrl.Text = "X"
Case Is = "NA"
ctrl.BackColor = vbWhite
ctrl.Text = "NA"
Case Is <> "X"
ctrl.Value = vbNullString
ctrl.BackColor = vbRed
Case Is <> "NA"
ctrl.Value = vbNullString
ctrl.BackColor = vbRed
End Select
End If
Next ctrl

MsgBox "Only a value of " & "X " & "or " & "NA " & "is allowed.", 0, "DATA ENTRY ERROR"





share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53308668%2fsetting-tag-property-of-userform-textbox%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    I figured it out; the issue wasn't in the UserForm Activate. It was in the Save and Cont Code. I dont think I was looking at enough of the If conditions, so I set up a Select Case and it works now. It's probably not the most elegant solution.



    For Each ctrl In UWchecklist.Controls
    If ctrl.Tag = "Check" Then
    Select Case ctrl.Value
    Case Is = ""
    ctrl.BackColor = vbWhite
    Case Is = "X"
    ctrl.BackColor = vbWhite
    ctrl.Text = "X"
    Case Is = "NA"
    ctrl.BackColor = vbWhite
    ctrl.Text = "NA"
    Case Is <> "X"
    ctrl.Value = vbNullString
    ctrl.BackColor = vbRed
    Case Is <> "NA"
    ctrl.Value = vbNullString
    ctrl.BackColor = vbRed
    End Select
    End If
    Next ctrl

    MsgBox "Only a value of " & "X " & "or " & "NA " & "is allowed.", 0, "DATA ENTRY ERROR"





    share|improve this answer






























      0














      I figured it out; the issue wasn't in the UserForm Activate. It was in the Save and Cont Code. I dont think I was looking at enough of the If conditions, so I set up a Select Case and it works now. It's probably not the most elegant solution.



      For Each ctrl In UWchecklist.Controls
      If ctrl.Tag = "Check" Then
      Select Case ctrl.Value
      Case Is = ""
      ctrl.BackColor = vbWhite
      Case Is = "X"
      ctrl.BackColor = vbWhite
      ctrl.Text = "X"
      Case Is = "NA"
      ctrl.BackColor = vbWhite
      ctrl.Text = "NA"
      Case Is <> "X"
      ctrl.Value = vbNullString
      ctrl.BackColor = vbRed
      Case Is <> "NA"
      ctrl.Value = vbNullString
      ctrl.BackColor = vbRed
      End Select
      End If
      Next ctrl

      MsgBox "Only a value of " & "X " & "or " & "NA " & "is allowed.", 0, "DATA ENTRY ERROR"





      share|improve this answer




























        0












        0








        0







        I figured it out; the issue wasn't in the UserForm Activate. It was in the Save and Cont Code. I dont think I was looking at enough of the If conditions, so I set up a Select Case and it works now. It's probably not the most elegant solution.



        For Each ctrl In UWchecklist.Controls
        If ctrl.Tag = "Check" Then
        Select Case ctrl.Value
        Case Is = ""
        ctrl.BackColor = vbWhite
        Case Is = "X"
        ctrl.BackColor = vbWhite
        ctrl.Text = "X"
        Case Is = "NA"
        ctrl.BackColor = vbWhite
        ctrl.Text = "NA"
        Case Is <> "X"
        ctrl.Value = vbNullString
        ctrl.BackColor = vbRed
        Case Is <> "NA"
        ctrl.Value = vbNullString
        ctrl.BackColor = vbRed
        End Select
        End If
        Next ctrl

        MsgBox "Only a value of " & "X " & "or " & "NA " & "is allowed.", 0, "DATA ENTRY ERROR"





        share|improve this answer















        I figured it out; the issue wasn't in the UserForm Activate. It was in the Save and Cont Code. I dont think I was looking at enough of the If conditions, so I set up a Select Case and it works now. It's probably not the most elegant solution.



        For Each ctrl In UWchecklist.Controls
        If ctrl.Tag = "Check" Then
        Select Case ctrl.Value
        Case Is = ""
        ctrl.BackColor = vbWhite
        Case Is = "X"
        ctrl.BackColor = vbWhite
        ctrl.Text = "X"
        Case Is = "NA"
        ctrl.BackColor = vbWhite
        ctrl.Text = "NA"
        Case Is <> "X"
        ctrl.Value = vbNullString
        ctrl.BackColor = vbRed
        Case Is <> "NA"
        ctrl.Value = vbNullString
        ctrl.BackColor = vbRed
        End Select
        End If
        Next ctrl

        MsgBox "Only a value of " & "X " & "or " & "NA " & "is allowed.", 0, "DATA ENTRY ERROR"






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 14 '18 at 22:00

























        answered Nov 14 '18 at 21:54









        Zack EZack E

        9910




        9910






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53308668%2fsetting-tag-property-of-userform-textbox%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Post-Redirect-Get with Spring WebFlux and Thymeleaf

            Xamarin.form Move up view when keyboard appear

            JBPM : POST request for execute process go wrong