+ Reply to Thread
Results 1 to 4 of 4

Validation Of Textboxes In A Form

Hybrid View

  1. #1
    Registered User
    Join Date
    02-06-2007
    Posts
    7

    Validation Of Textboxes In A Form

    Hey, im looking to have some data validation on the 3 text boxes of my form.
    Text box 1 is Item name and i wish for that text box to only accept Text up to 30 characters.
    Text box 3 is Product code and i wish for it to only accept 10 chracters max, the first two of which must be text and the last ones must be number. eg CA04124 or EK123456, there must be at least 2 numbers after the first two letters but no more than 6, there must be two letters before the numbers also.
    Text box 4 is Price which i want to only accept number.

    I understand the text box numbering isnt even, i dont know why not. Here is the code i am working with. Is the only way for this validation to be done by editting the code? If so i would be very grateful for someone to do this for me or tell me how to.

    Private Sub CommandButton1_Click()
       Dim LastRow As Object
    
       Set LastRow = Sheet1.Range("a65536").End(xlUp)
    
       LastRow.Offset(1, 0).Value = TextBox1.Text
       LastRow.Offset(1, 1).Value = TextBox3.Text
       LastRow.Offset(1, 2).Value = TextBox4.Text
    
       MsgBox "One item added to Main Page"
    
       response = MsgBox("Do you want to enter add another item?", _
          vbYesNo)
    
       If response = vbYes Then
          TextBox1.Text = ""
          TextBox3.Text = ""
          TextBox4.Text = ""
        
          TextBox1.SetFocus
    
       Else
          Unload Me
       End If
    
    End Sub
    Many thanks, Ben.

  2. #2
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    Use the MaxLength property to set the maximum length allowed in each text box


    This code checks for required info before adding to the sheet.

    Private Sub CommandButton1_Click()
       Dim LastRow As Object
    
       Select Case LCase(Left(TextBox3.Text, 2))
       Case "aa" To "zz"
       Case Else
          MsgBox "Invalid Entry", vbInformation
          Exit Sub
       End Select
       If Len(TextBox3.Text) < 4 Then
          MsgBox "Invalid Entry", vbInformation
          Exit Sub
       End If
       If Not IsNumeric(TextBox4.Text) Then
          MsgBox "Invalid Entry", vbInformation
          Exit Sub
       End If
       
       Set LastRow = Sheet1.Range("a65536").End(xlUp)
    
       LastRow.Offset(1, 0).Value = TextBox1.Text
       LastRow.Offset(1, 1).Value = TextBox3.Text
       LastRow.Offset(1, 2).Value = TextBox4.Text
    
       MsgBox "One item added to Main Page"
    
       response = MsgBox("Do you want to enter add another item?", _
          vbYesNo)
    
       If response = vbYes Then
          TextBox1.Text = ""
          TextBox3.Text = ""
          TextBox4.Text = ""
        
          TextBox1.SetFocus
    
       Else
          Unload Me
       End If
    
    End Sub

  3. #3
    Registered User
    Join Date
    02-06-2007
    Posts
    7
    Thanks mudraker, that codes works great.
    Could you show me the code that would validate TextBox1 (Item name) so that something written in there is less than 50 characters but more than 0. Thanks a lot, a quick reply would be much appreciated.

    Thanks, Ben.

  4. #4
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    Ben

    If you use the MaxLength property set to 50 then you cane use

    If TextBox.Text = vbNullString Then
       MsgBox "Invalid Entry", vbInformation
       Exit Sub
    end if
    Otherwise use this one

    Select Case Len(TextBox.Text)
    Case Is < 1, Is >= 50
       MsgBox "Invalid Entry", vbInformation
       Exit Sub
    End Select

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1