Consulting

Results 1 to 3 of 3

Thread: Forcing a value in textbox before allowing closing?

  1. #1
    VBAX Regular MUSASHI's Avatar
    Joined
    Oct 2004
    Location
    Corpus Christi, Texas (Very South)
    Posts
    44

    Forcing a value in textbox before allowing closing?

    Im trying to stop the cells in worksheet being allowed to be filled if an employee number is not entered, which is inputted in tbEmpNumber. I have its default value set to "Required!". How can I make sure a value other than the default "Required!" is entered before it places the other data/any data in it.

    Hope that added up...

    Private Sub btn_EmpOK_Click()
        Dim LastRow As Long
    LastRow = Worksheets("Employee").Range("A65536").End(xlUp).Row + 1
        Cells(LastRow, 1).Value = tb_LastName.Value
        Cells(LastRow, 2).Value = tbFirstName.Value
        Cells(LastRow, 4).Value = cb_EmpPosition.Value
        Cells(LastRow, 5).Value = tbAddress.Value
        Cells(LastRow, 6).Value = tbCity.Value
        Cells(LastRow, 7).Value = tbState.Value
        Cells(LastRow, 8).Value = tbZip.Value
        Cells(LastRow, 9).Value = tbPhone1.Value
        Cells(LastRow, 10).Value = tbPhone2.Value
        Cells(LastRow, 11).Value = DateSerial(SpinButton1.Value, SpinButton2.Value, SpinButton3.Value)
        Cells(LastRow, 12).Value = tbSocial.Value
        Cells(LastRow, 3).Value = tbEmpNumber.Value
        Cells(LastRow, 13).Value = DateSerial(SpBtn_Year.Value, SpBtn_Month.Value, SpBtn_Day.Value)
        If opbtn_NewHire.Value Then
            Cells(LastRow, 14).Value = opbtn_NewHire.Caption
        Else
            Cells(LastRow, 13).Value = opbtn_ReHire.Caption
        End If
        Range("A3:A" & LastRow).Name = "EmpList"
          'make sure employee number is entered
    Unload Me
    End Sub
    Thanks Mus

  2. #2
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Hi there,

    Right after you Dim statement, throw this in:

    Select case tbEmpNumber.text
         case is = "Required!"
         msgbox "Sorry, but you must put in an employee number!
         exit sub
         case is = "" 
         msgbox "Sorry, but you must put in an employee number!
         exit sub
         case else
         'do nothing
         end select
    End if
    FYI, also tests to make sure your user just doesn't wipe out the text box and use no number.

    HTH,
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  3. #3
    VBAX Regular MUSASHI's Avatar
    Joined
    Oct 2004
    Location
    Corpus Christi, Texas (Very South)
    Posts
    44
    Thanks!!! I was trying various things, this works perfect!!

    Appreciate the help.

    Mus

Posting Permissions

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