Checking Textbox For Specific Text Entry

Pacman52

Active Member
Joined
Jan 29, 2009
Messages
319
Office Version
  1. 365
Platform
  1. Windows
Hi I have a userform that a user can use to edit/amend a record already entered and then writes any amended data back to the sheet.
The textboxes on the edit form are populated from a worksheet.

I'm having a problem coding a textbox exit routine to check that if a user edits the box and enters something other than the 3 allowed words it fires a msgbox. The code below does actually work but it also fires even if 1 of the 3 words is entered. e.g. If the textbox contains PayPal when the form opens and I then type in Bacs or Stripe is still fires the msgbox.

Would someone be knid enough to let me know what I'm doing wrong please?

Many thanks Paul

VBA Code:
Private Sub textboxPayMeth_Exit(ByVal Cancel As MSForms.ReturnBoolean)

    If (textboxPayMeth.Text) <> "PayPal" Or (textboxPayMeth.Text) <> "Stripe" Or (textboxPayMeth.Text) <> "Bacs" Then
    MsgBox "You have entered an invalid payment method, you can only use Paypal, Stripe or Bacs. Please re-enter the payment method using 1 of these 3 methods.", vbInformation, "Invalid Entry"
    
    Cancel = True
    textboxPayMeth.SetFocus
    textboxPayMeth.Value = ""
    textboxPayMeth.BackColor = RGB(204, 255, 255)

    End If

End Sub
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Hi,
If users can only enter one of three choices then suggest that perhaps consider using a combobox listing available choices which would negate any possible entry error?

Dave
 
Upvote 0
Solution
Hi Dave thanks for the very quick reply, I did think about using a combo and tried on initially but I came up against an issue (I cant quite remember what it was sorry)

Basically on the original form, the user selects the payment method using a checkbox. Depending on which box is ticked a text value is written to a hidden textbox which in turn is written to the sheet. but saying that as I'm writing this reply I just realised - I don't know why I just dont use a new combo just for the edit form doh!!
 
Upvote 0
Hi
welcome should be quite simple - something like this maybe

VBA Code:
With Me.ComboBox1
    .List = Array("PayPal", "Stripe", "Bacs")
    .Style = fmStyleDropDownList
  End With

Setting the controls Style property should prevent users entering any other values

Dave
 
Upvote 0
Thanks very much again Dave - I must be learning something as I work on VBA as I actually nearly did that although I missed the style bit.
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,953
Members
449,095
Latest member
nmaske

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top