Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 863771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T09:21:59+00:00 2026-05-15T09:21:59+00:00

I am trying to validate textbox values during runtime in vb.net I have following

  • 0

I am trying to validate textbox values during runtime in vb.net I have following code which is validating txtno from database table tblmachines. But i have problem with chartype and stringtype. Is there any other solution to fix that problem?

Private Sub txtno_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtno.KeyPress
        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
            e.Handled = True
            If (Me.txtno.Text = "") Then
                Interaction.MsgBox("!!!!!!!!Machine number can not be empty. Please Correct.!!!!!!!!", &H40, "Check Machine Number")
                Me.txtno.Focus()
            ElseIf (Me.txtno.Text = "0") Then
                Me.txtturnover.Focus()
            Else
                Dim table As DataTable = Me.DataSet1.Tables.Item("tblmachines")
                Dim defaultView As DataView = table.DefaultView
                defaultView.RowFilter = ("local_no='" & Me.txtno.Text & "'")
                If ((Char.IsLetter(CharType.FromString(Me.txtno.Text)) Or (defaultView.Count = 0)) Or (StringType.StrCmp(Me.txtno.Text, "", False) = 0)) Then
                    Interaction.MsgBox("This machine is not on database. Please correct machine number.", &H40, "Check Machine Number")
                    Me.txtno.Focus()
                Else
                    Me.txtturnover.Focus()
                End If

            End If
        End If
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T09:21:59+00:00Added an answer on May 15, 2026 at 9:21 am

    I think you’d be better off using validation from the Winforms library than in the KeyPress event. KeyPress is going to cause a lot of validation scripts to be run and will bog down your app.

    I think you should do the validation in the following steps

    1. Check to ensure that there is data in the texbox
    2. Validate that the data entered in the textbox is in the proper format. You can use regular code, or possibly a RegEx for that.
    3. Validate that the number entered is a part of the database. I made it a habit reset the filters to a blank string before applying any new filters to the table.

    The good part about using inbuilt validation is that you do not have to worry about moving focus around.

    The control has a property called CausesValidation – if you set it to true (which it already is) then this control will run the validation code. If you do not want the control to run validation code, you can turn the property to False.

    The validation happens as Focus shifts – so when you focus off a control, it’s validation events are fired.

    The events Validating and Validated are commonly used for this purpose. You can put your code validation in there instead of putting it on the KeyPress.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use a RegularExpressionValidator to validate a TextBox input length. I have
I'm trying to validate an ID. I have this class called ManejadorTickets in which
I am trying to move the following function listView_SelectionChanged away from code-behind and handle
I am trying to find an elegant way to validate two related TextBox values
Im trying to get the values from my datepicker into a new script which
When trying to validate my site, I get the following error: Line 188, column
While trying to validate my forms i get the following error: Expected a {
While trying to validate form data on my page i get the following error:
I am trying to validate a textbox based on a dropdownlist, e.g. if ddl
I am trying to validate a textbox to enter only numbers and set maximum

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.