Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
Help with code restricting TextBox entry
1st off, I can't believe all this can't be handled within the TextBox
Properties selections. I've looked, but can't see anything on this. This is something I’ve had help on, but I’m trying to tweak it to get just what I want. What I’m trying to do is to require the user to enter a number in TextBox6, and the user cannot leave it blank. Here’s what I have so far to help in this: ‘This takes care to not allow an alpha to be entered in TextBox6. This works fine. Private Sub TextBox6_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) Select Case KeyAscii Case 8, 9, 27, 45, 46, 48 To 57 Case Else Beep KeyAscii = 0 End Select End Sub This part below is what I’m having a problem with. This makes sure TextBox6 is not left blank. It works fine, except that I would like to let the user Cancel the whole UserForm routine if he wants. Maybe he doesn’t have a number to enter for TextBox6 at the time, and would rather start over after getting the required data. For right now, this code goes in a continuous loop that requires the user to enter a number in this box before it will allow the user to do ANYTHING. Even the Cancel button (see below) will not work until a number is entered in TextBox6. The only way to get out of the loop is to enter any number in the box, THEN the Cancel button will work. ‘HOWEVER, if I take out the line Cancel=True, or change it to False, then the message comes up when TextBox6 is left blank, but, ‘after hitting OK for the MsgBox reminder, the cursor goes to the next box as if this whole restriction never applied. ‘In short, I need the requirement that the user enter a number in TextBox6, but the user must be able to click the Cancel button, too. Private Sub TextBox6_Exit(ByVal Cancel As MSForms.ReturnBoolean) ‘Keeps user from leaving TextBox6 blank. With Worksheets("Official list") If TextBox6.Text = "" Then MsgBox "This box must have a numeric value. .“ TextBox6.Text = Clear Cancel = True End If End With End Sub This is the Cancel button referenced above. Private Sub CommandButton2_Click() 'CANCEL button. Unload UserForm4 Worksheets("Menu").Activate End Sub NOTE: I would rather handle all this with code specific to instructions for TextBox6 I’m going to set up similar restrictions (but not the same) for other textboxes in this userform. So, I would rather the coding for the OK and Cancel buttons be left out of all this, if that makes any sense. I hope this is clear enough for someone to help me. I appreciate it. Thanks j.o. |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Restricting entry | Excel Worksheet Functions | |||
Restricting Duplicate Entry | Excel Worksheet Functions | |||
Restricting entry in B1 on the basis of entry in A1 | Excel Worksheet Functions | |||
Restricting entry in B1 on the basis of entry in A1 | Excel Worksheet Functions | |||
Text Box - Restricting Entry | Excel Programming |