Validate a TextBox before the _Change event is fired












0














I've got a form that has 3 TextBox controls on it: stock code, quantity, certificate number. The stock code TextBox is set to focus automatically when the form is loaded.



I've also attached a bar code scanner to my PC, as the user wants to be able to either scan a bar code to populate the TextBox, or manually type the data in.



The labels being scanned contain two bar codes. One is a certificate number and the other a stock code.



The stock bar code has a prefix of "SBC/", whilst a certificate bar code is prefixed with "C/".



When the user scans a bar code, if the TextBox in focus is the stock code TextBox, then I want to run a check as below.



Private Sub txtStockCode_Change()

On Error GoTo errError1

If Len(txtStockCode.Text) >= 5 Then
If bChangeCode Then
If Left(txtStockCode.Text, 2) = "C/" Then
msgbox "You have scanned the certificate barcode; please scan the stock barcode."
txtStockCode.Text = ""
Else
bChangeCode = False
txtStockCode.Text = Replace(txtStockCode.Text, "SBC/", "")
txtStockCode.Text = Replace(txtStockCode.Text, "*", "")
End If
End If
End If

Exit Sub


Let's say the focus is currently on the stock code TextBox.



If the stock bar code is scanned, the following should happen:




  • Stock code length is greater than 5


  • Left 5 characters do not = "C/", so correct code has been scanned


  • TextBox text value is updated to remove all * and the prefix of "SBC/"



E.g. "SBC/A12-TR0*" becomes "A12-TRO"



and




  • Certificate number length is greater than 5


  • Left 5 characters do = "C/", so incorrect code has been scanned


  • MsgBox to user


  • TextBox value is reset to ""



However, no matter which code is scanned into the stock code TextBox, the value is never validated.



E.g. "SBC/A12-TR0*" remains as "SBC/A12-TR0*" and "C/29760" remains as "C/29760"



As the validation code is the same in the certificate TextBox, the same pattern is repeated vice versa.



Why are my values not updating, or how can I validate the input before the _Change is fired?



EDIT



I've now changed my code to



Private Sub txtStockCode_Change

If txtStockCode.Text <> "" Then
txtStockCode.Text = Replace(txtStockCode.Text, "SBC/", "")
txtStockCode.Text = Replace(txtStockCode.Text, "*", "")
End If

End Sub


But it still displays the prefix of SBC/, yet is removing the two * characters (at the start and end of the barcode as is required for the scanner to read it as a barcode)










share|improve this question
























  • In your code, it looks like you are trying to remove the wrong prefix.
    – Brian M Stafford
    Nov 20 '18 at 13:16










  • @BrianMStafford oops, typo. Corrected.
    – David
    Nov 20 '18 at 14:20










  • Somewhere else in code have you set bChangeCode to True?
    – Brian M Stafford
    Nov 20 '18 at 14:39






  • 1




    The issue is because the Change event is firing once for every character of the bar code. The bChangeCode gets set to False and short-circuits your logic.
    – Brian M Stafford
    Nov 20 '18 at 15:18






  • 1




    I would expect to see that behavior with your existing code. You may have better luck moving the logic to the Validate event, though this would require programming your scanner to append a Tab key.
    – Brian M Stafford
    Nov 20 '18 at 16:12


















0














I've got a form that has 3 TextBox controls on it: stock code, quantity, certificate number. The stock code TextBox is set to focus automatically when the form is loaded.



I've also attached a bar code scanner to my PC, as the user wants to be able to either scan a bar code to populate the TextBox, or manually type the data in.



The labels being scanned contain two bar codes. One is a certificate number and the other a stock code.



The stock bar code has a prefix of "SBC/", whilst a certificate bar code is prefixed with "C/".



When the user scans a bar code, if the TextBox in focus is the stock code TextBox, then I want to run a check as below.



Private Sub txtStockCode_Change()

On Error GoTo errError1

If Len(txtStockCode.Text) >= 5 Then
If bChangeCode Then
If Left(txtStockCode.Text, 2) = "C/" Then
msgbox "You have scanned the certificate barcode; please scan the stock barcode."
txtStockCode.Text = ""
Else
bChangeCode = False
txtStockCode.Text = Replace(txtStockCode.Text, "SBC/", "")
txtStockCode.Text = Replace(txtStockCode.Text, "*", "")
End If
End If
End If

Exit Sub


Let's say the focus is currently on the stock code TextBox.



If the stock bar code is scanned, the following should happen:




  • Stock code length is greater than 5


  • Left 5 characters do not = "C/", so correct code has been scanned


  • TextBox text value is updated to remove all * and the prefix of "SBC/"



E.g. "SBC/A12-TR0*" becomes "A12-TRO"



and




  • Certificate number length is greater than 5


  • Left 5 characters do = "C/", so incorrect code has been scanned


  • MsgBox to user


  • TextBox value is reset to ""



However, no matter which code is scanned into the stock code TextBox, the value is never validated.



E.g. "SBC/A12-TR0*" remains as "SBC/A12-TR0*" and "C/29760" remains as "C/29760"



As the validation code is the same in the certificate TextBox, the same pattern is repeated vice versa.



Why are my values not updating, or how can I validate the input before the _Change is fired?



EDIT



I've now changed my code to



Private Sub txtStockCode_Change

If txtStockCode.Text <> "" Then
txtStockCode.Text = Replace(txtStockCode.Text, "SBC/", "")
txtStockCode.Text = Replace(txtStockCode.Text, "*", "")
End If

End Sub


But it still displays the prefix of SBC/, yet is removing the two * characters (at the start and end of the barcode as is required for the scanner to read it as a barcode)










share|improve this question
























  • In your code, it looks like you are trying to remove the wrong prefix.
    – Brian M Stafford
    Nov 20 '18 at 13:16










  • @BrianMStafford oops, typo. Corrected.
    – David
    Nov 20 '18 at 14:20










  • Somewhere else in code have you set bChangeCode to True?
    – Brian M Stafford
    Nov 20 '18 at 14:39






  • 1




    The issue is because the Change event is firing once for every character of the bar code. The bChangeCode gets set to False and short-circuits your logic.
    – Brian M Stafford
    Nov 20 '18 at 15:18






  • 1




    I would expect to see that behavior with your existing code. You may have better luck moving the logic to the Validate event, though this would require programming your scanner to append a Tab key.
    – Brian M Stafford
    Nov 20 '18 at 16:12
















0












0








0







I've got a form that has 3 TextBox controls on it: stock code, quantity, certificate number. The stock code TextBox is set to focus automatically when the form is loaded.



I've also attached a bar code scanner to my PC, as the user wants to be able to either scan a bar code to populate the TextBox, or manually type the data in.



The labels being scanned contain two bar codes. One is a certificate number and the other a stock code.



The stock bar code has a prefix of "SBC/", whilst a certificate bar code is prefixed with "C/".



When the user scans a bar code, if the TextBox in focus is the stock code TextBox, then I want to run a check as below.



Private Sub txtStockCode_Change()

On Error GoTo errError1

If Len(txtStockCode.Text) >= 5 Then
If bChangeCode Then
If Left(txtStockCode.Text, 2) = "C/" Then
msgbox "You have scanned the certificate barcode; please scan the stock barcode."
txtStockCode.Text = ""
Else
bChangeCode = False
txtStockCode.Text = Replace(txtStockCode.Text, "SBC/", "")
txtStockCode.Text = Replace(txtStockCode.Text, "*", "")
End If
End If
End If

Exit Sub


Let's say the focus is currently on the stock code TextBox.



If the stock bar code is scanned, the following should happen:




  • Stock code length is greater than 5


  • Left 5 characters do not = "C/", so correct code has been scanned


  • TextBox text value is updated to remove all * and the prefix of "SBC/"



E.g. "SBC/A12-TR0*" becomes "A12-TRO"



and




  • Certificate number length is greater than 5


  • Left 5 characters do = "C/", so incorrect code has been scanned


  • MsgBox to user


  • TextBox value is reset to ""



However, no matter which code is scanned into the stock code TextBox, the value is never validated.



E.g. "SBC/A12-TR0*" remains as "SBC/A12-TR0*" and "C/29760" remains as "C/29760"



As the validation code is the same in the certificate TextBox, the same pattern is repeated vice versa.



Why are my values not updating, or how can I validate the input before the _Change is fired?



EDIT



I've now changed my code to



Private Sub txtStockCode_Change

If txtStockCode.Text <> "" Then
txtStockCode.Text = Replace(txtStockCode.Text, "SBC/", "")
txtStockCode.Text = Replace(txtStockCode.Text, "*", "")
End If

End Sub


But it still displays the prefix of SBC/, yet is removing the two * characters (at the start and end of the barcode as is required for the scanner to read it as a barcode)










share|improve this question















I've got a form that has 3 TextBox controls on it: stock code, quantity, certificate number. The stock code TextBox is set to focus automatically when the form is loaded.



I've also attached a bar code scanner to my PC, as the user wants to be able to either scan a bar code to populate the TextBox, or manually type the data in.



The labels being scanned contain two bar codes. One is a certificate number and the other a stock code.



The stock bar code has a prefix of "SBC/", whilst a certificate bar code is prefixed with "C/".



When the user scans a bar code, if the TextBox in focus is the stock code TextBox, then I want to run a check as below.



Private Sub txtStockCode_Change()

On Error GoTo errError1

If Len(txtStockCode.Text) >= 5 Then
If bChangeCode Then
If Left(txtStockCode.Text, 2) = "C/" Then
msgbox "You have scanned the certificate barcode; please scan the stock barcode."
txtStockCode.Text = ""
Else
bChangeCode = False
txtStockCode.Text = Replace(txtStockCode.Text, "SBC/", "")
txtStockCode.Text = Replace(txtStockCode.Text, "*", "")
End If
End If
End If

Exit Sub


Let's say the focus is currently on the stock code TextBox.



If the stock bar code is scanned, the following should happen:




  • Stock code length is greater than 5


  • Left 5 characters do not = "C/", so correct code has been scanned


  • TextBox text value is updated to remove all * and the prefix of "SBC/"



E.g. "SBC/A12-TR0*" becomes "A12-TRO"



and




  • Certificate number length is greater than 5


  • Left 5 characters do = "C/", so incorrect code has been scanned


  • MsgBox to user


  • TextBox value is reset to ""



However, no matter which code is scanned into the stock code TextBox, the value is never validated.



E.g. "SBC/A12-TR0*" remains as "SBC/A12-TR0*" and "C/29760" remains as "C/29760"



As the validation code is the same in the certificate TextBox, the same pattern is repeated vice versa.



Why are my values not updating, or how can I validate the input before the _Change is fired?



EDIT



I've now changed my code to



Private Sub txtStockCode_Change

If txtStockCode.Text <> "" Then
txtStockCode.Text = Replace(txtStockCode.Text, "SBC/", "")
txtStockCode.Text = Replace(txtStockCode.Text, "*", "")
End If

End Sub


But it still displays the prefix of SBC/, yet is removing the two * characters (at the start and end of the barcode as is required for the scanner to read it as a barcode)







string validation textbox vb6






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 13:03

























asked Nov 20 '18 at 13:00









David

1,55221536




1,55221536












  • In your code, it looks like you are trying to remove the wrong prefix.
    – Brian M Stafford
    Nov 20 '18 at 13:16










  • @BrianMStafford oops, typo. Corrected.
    – David
    Nov 20 '18 at 14:20










  • Somewhere else in code have you set bChangeCode to True?
    – Brian M Stafford
    Nov 20 '18 at 14:39






  • 1




    The issue is because the Change event is firing once for every character of the bar code. The bChangeCode gets set to False and short-circuits your logic.
    – Brian M Stafford
    Nov 20 '18 at 15:18






  • 1




    I would expect to see that behavior with your existing code. You may have better luck moving the logic to the Validate event, though this would require programming your scanner to append a Tab key.
    – Brian M Stafford
    Nov 20 '18 at 16:12




















  • In your code, it looks like you are trying to remove the wrong prefix.
    – Brian M Stafford
    Nov 20 '18 at 13:16










  • @BrianMStafford oops, typo. Corrected.
    – David
    Nov 20 '18 at 14:20










  • Somewhere else in code have you set bChangeCode to True?
    – Brian M Stafford
    Nov 20 '18 at 14:39






  • 1




    The issue is because the Change event is firing once for every character of the bar code. The bChangeCode gets set to False and short-circuits your logic.
    – Brian M Stafford
    Nov 20 '18 at 15:18






  • 1




    I would expect to see that behavior with your existing code. You may have better luck moving the logic to the Validate event, though this would require programming your scanner to append a Tab key.
    – Brian M Stafford
    Nov 20 '18 at 16:12


















In your code, it looks like you are trying to remove the wrong prefix.
– Brian M Stafford
Nov 20 '18 at 13:16




In your code, it looks like you are trying to remove the wrong prefix.
– Brian M Stafford
Nov 20 '18 at 13:16












@BrianMStafford oops, typo. Corrected.
– David
Nov 20 '18 at 14:20




@BrianMStafford oops, typo. Corrected.
– David
Nov 20 '18 at 14:20












Somewhere else in code have you set bChangeCode to True?
– Brian M Stafford
Nov 20 '18 at 14:39




Somewhere else in code have you set bChangeCode to True?
– Brian M Stafford
Nov 20 '18 at 14:39




1




1




The issue is because the Change event is firing once for every character of the bar code. The bChangeCode gets set to False and short-circuits your logic.
– Brian M Stafford
Nov 20 '18 at 15:18




The issue is because the Change event is firing once for every character of the bar code. The bChangeCode gets set to False and short-circuits your logic.
– Brian M Stafford
Nov 20 '18 at 15:18




1




1




I would expect to see that behavior with your existing code. You may have better luck moving the logic to the Validate event, though this would require programming your scanner to append a Tab key.
– Brian M Stafford
Nov 20 '18 at 16:12






I would expect to see that behavior with your existing code. You may have better luck moving the logic to the Validate event, though this would require programming your scanner to append a Tab key.
– Brian M Stafford
Nov 20 '18 at 16:12














1 Answer
1






active

oldest

votes


















0














You could try to set the barcode reader to return Enter key at the end of the scanned barcode and then use the Keypress event to check it and make your changes.



Sub txtStockCode_KeyPress(KeyAscii As Integer)
If KeyAscii = vbKeyReturn Then
If Len(txtStockCode.Text) >= 5 Then
If bChangeCode Then
If Left(txtStockCode.Text, 2) = "C/" Then
msgbox "You have scanned the certificate barcode; please scan the stock barcode."
txtStockCode.Text = ""
Else
bChangeCode = False
txtStockCode.Text = Replace(txtStockCode.Text, "SBC/", "")
txtStockCode.Text = Replace(txtStockCode.Text, "*", "")
End If
End If
End If
End If
End Sub





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%2f53393564%2fvalidate-a-textbox-before-the-change-event-is-fired%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














    You could try to set the barcode reader to return Enter key at the end of the scanned barcode and then use the Keypress event to check it and make your changes.



    Sub txtStockCode_KeyPress(KeyAscii As Integer)
    If KeyAscii = vbKeyReturn Then
    If Len(txtStockCode.Text) >= 5 Then
    If bChangeCode Then
    If Left(txtStockCode.Text, 2) = "C/" Then
    msgbox "You have scanned the certificate barcode; please scan the stock barcode."
    txtStockCode.Text = ""
    Else
    bChangeCode = False
    txtStockCode.Text = Replace(txtStockCode.Text, "SBC/", "")
    txtStockCode.Text = Replace(txtStockCode.Text, "*", "")
    End If
    End If
    End If
    End If
    End Sub





    share|improve this answer


























      0














      You could try to set the barcode reader to return Enter key at the end of the scanned barcode and then use the Keypress event to check it and make your changes.



      Sub txtStockCode_KeyPress(KeyAscii As Integer)
      If KeyAscii = vbKeyReturn Then
      If Len(txtStockCode.Text) >= 5 Then
      If bChangeCode Then
      If Left(txtStockCode.Text, 2) = "C/" Then
      msgbox "You have scanned the certificate barcode; please scan the stock barcode."
      txtStockCode.Text = ""
      Else
      bChangeCode = False
      txtStockCode.Text = Replace(txtStockCode.Text, "SBC/", "")
      txtStockCode.Text = Replace(txtStockCode.Text, "*", "")
      End If
      End If
      End If
      End If
      End Sub





      share|improve this answer
























        0












        0








        0






        You could try to set the barcode reader to return Enter key at the end of the scanned barcode and then use the Keypress event to check it and make your changes.



        Sub txtStockCode_KeyPress(KeyAscii As Integer)
        If KeyAscii = vbKeyReturn Then
        If Len(txtStockCode.Text) >= 5 Then
        If bChangeCode Then
        If Left(txtStockCode.Text, 2) = "C/" Then
        msgbox "You have scanned the certificate barcode; please scan the stock barcode."
        txtStockCode.Text = ""
        Else
        bChangeCode = False
        txtStockCode.Text = Replace(txtStockCode.Text, "SBC/", "")
        txtStockCode.Text = Replace(txtStockCode.Text, "*", "")
        End If
        End If
        End If
        End If
        End Sub





        share|improve this answer












        You could try to set the barcode reader to return Enter key at the end of the scanned barcode and then use the Keypress event to check it and make your changes.



        Sub txtStockCode_KeyPress(KeyAscii As Integer)
        If KeyAscii = vbKeyReturn Then
        If Len(txtStockCode.Text) >= 5 Then
        If bChangeCode Then
        If Left(txtStockCode.Text, 2) = "C/" Then
        msgbox "You have scanned the certificate barcode; please scan the stock barcode."
        txtStockCode.Text = ""
        Else
        bChangeCode = False
        txtStockCode.Text = Replace(txtStockCode.Text, "SBC/", "")
        txtStockCode.Text = Replace(txtStockCode.Text, "*", "")
        End If
        End If
        End If
        End If
        End Sub






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 3 '18 at 0:31









        TufanDsl

        12




        12






























            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2f53393564%2fvalidate-a-textbox-before-the-change-event-is-fired%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







            Popular posts from this blog

            "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

            Alcedinidae

            Origin of the phrase “under your belt”?