Hi,
I have a VBA userform with various textbox fields like 'Name' and 'Address' and 'Amount'
I would like an input box to pop up that asks the user to 're-enter for verification'
So that they have to type the same thing twice, to protect against typos
How do I code it so the program compares the inputbox to the textbox and passes only if they are identical?
(or even, using two inputboxes instead if that would be easier)
Grateful for any assistance
Andrew
Hello durandal5,
Here is the code for the Textbox control and the macro to verify the input.
Textbox Event Code
Macro Code to Verify InputPrivate Sub TextBox1_AfterUpdate() VerifyInput ActiveControl End Sub Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean) If ActiveControl.Tag = "failed" Then Cancel = True ActiveControl.Tag = "" End If End Sub
Sincerely,Sub VerifyInput(TB As MSForms.TextBox) Dim Text As String Text = InputBox("Please re-enter the text into the box below.") If Text <> "" Then If Text <> TB.Value Then MsgBox "The text doesn't match - try again." TB.Value = "" TB.Tag = "failed" End If End If End Sub
Leith Ross
Thanks, that seems to be right, but there's a problem...
the input box will not pop up after i've entered the text, it only appears if i close the userform, and while it appears to verify like i wanted, when you OK or Cancel the box, there is no form to go back to, the program just ends
the code i devised myself earlier also had this problem with AfterUpdate
perhaps it's a Mac Excel bug. I will try on a windows computer.
aha, the problem seems to be with the enter key
tabbing to the next box or clicking another control brings the inputbox up
i turned the textbox's EnterKeyBehaviour property to True and now it works when you press enter too
(although i'm a bit confused as i thought enter key behaviour was for making new lines in the text box...)
Hello durandl05,
Nice work on catching that property setting. I didn't occur to me to ask if you were using a Mac. I will start doing that. The world isn't exclusively PC!
Sincerely,
Leith Ross
Thanks again. Yes the Mac VBA suite has a few quirks. Last time i spent ages wondering why the Tabbing order wasn't working, no matter how what i set teh 'TabOrder' and 'Autotab' properties too... turned out the code ran fine on Windows.
I have a real basic question now...
i have a bunch of checkboxes for another field, clicking one of them puts a number into a 'Type' box elsewhere
for instance
and so on, which works fine, but some "Types" are combinations of two options, and the following statement wont workIf checkAutomatic=True then txtType=1 If checkDiesel=True then txtType=4
i'm not surprised it doesn't work, but what IS the right way to have more than one condition for an IF statement?If checkAutomatic=True AND checkDiesel=True then txtType=5
Good grief, it was just a typo i'd made when writing the procedure. The code works. Never mind!
Hello again,
When I tried to put this code in a new Userform (same idea, just this one is the real thing, the first one was just a quick demo), I get a 'Error 13, Type Mismatch' bug. Can't work out why this would be as I'm doing exactly the same thing. Any ideas?
Hello durandal05,
If would help to see the code and on which line the error occurs.
Sincerely,
Leith Ross
when it runs i get a Error 13 Type Mismatch message and the 'verifyinput activecontrol' line is highlighted. the error occurs when i hit enter after typing something in the textbox (txtName)
i'm quite baffled!Dim variantComplete(80, 24) As String Dim strComplete As String Dim strSort As String Dim strAccount As String Dim strAddress As String Dim strPropAddress As String Dim strCity As String Dim strPropCity As String Dim strPostcode As String Dim strPropPostcode As String Dim strValuation As String Dim strInspection As String Dim strStart As String Dim strMaturity As String Dim strDatePaid As String Dim strLoanDate As String Dim strSex As String Dim strMarital As String Dim strDOB As String Dim strCustNo As String Dim strNat As String Dim strSO As String Dim strFreq As String Dim strPlan As String Dim strIndex1 As String Dim strIndex2 As String Dim strERC As String Dim strhlc As String Dim strProductCode As String Dim strPayment As String Dim strRef As String Dim strFlex As String Dim strSwitch As String Dim strPurpose As String Dim strOccup As String Dim strRepayment As String Dim strDebit As String Dim strGuarantor As String Dim strLoan As String Dim strShort As String Dim strFtb As String Dim strUnen As String Dim strName As String Dim strSic As String Private Sub chbHLC_Change() If chbHLC = True Then strhlc = 12 Else strhlc = 0 End Sub Private Sub chbHLC_Click() End Sub Private Sub cmbFlex_Change() If cmbFlex = 3 Then txtRef = 999.9999 End Sub Private Sub cmbMarital_Change() strMarital = cmbMarital.Text End Sub Private Sub cmbSex_Change() strSex = cmbSex.Value End Sub Private Sub cmmClear_Click() Unload Me Load formAO formAO.Show End Sub Private Sub cmmClose_Click() Unload Me End Sub Private Sub cmmTransfer_Click() Set dataComplete = New DataObject txtComplete.Text = _ strShort + vbLf + strName + vbLf + strAddress End Sub Private Sub CommandButton4_Click() formProduct.Show End Sub Private Sub CommandButton7_Click() formPurpose.Show End Sub Private Sub CommandButton9_Click() formSwitch.Show End Sub Private Sub FA_Click() LoanType End Sub Private Sub frmDebit_Click() End Sub Private Sub frmLoan_Click() End Sub Private Sub HER_Click() Dim msg, style, title, response msg = "Is the property unencumbered? (check charge register on Land Registry Office Copy)" style = vbYesNoCancel + vbDefaultButton2 title = "" If HER.Value = True Then response = MsgBox(msg, style, title) If response = vbYes Then chbunen = True ElseIf response = vbNo Then chbunen = False ElseIf response = vbCancel Then txtPurp = "" HER = False Else End If Else: txtPurp = "" End If If HER = False Then chbunen = False LoanType End Sub Private Sub IHL_Click() If IHL = True Then txtOccup = 2 Else txtOccup = 0 LoanType End Sub Private Sub optCB_Click() End Sub Private Sub optStaff_Change() If optStaff = True Then strSic = "071" End Sub Private Sub optStaff_Click() End Sub Private Sub PUR_Click() Dim msg, style, title, response msg = "Is it a First Time Buy? (top of OSI Page 2)" style = vbYesNoCancel + vbDefaultButton2 title = "" If PUR.Value = True Then response = MsgBox(msg, style, title) If response = vbYes Then chbftb = True ElseIf response = vbNo Then chbftb = False ElseIf response = vbCancel Then txtPurp = "" PUR = False Else End If Else: txtPurp = "" End If If PUR = False Then chbftb = False LoanType End Sub Private Sub RMG_Click() LoanType End Sub Private Sub SWP_Click() If SWP.Value = True Then RMG.Value = True strSwitch = inputbox("Please enter the switching fee letter from SAM") txtSwitch.Value = strSwitch Else: txtSwitch.Value = "" End If LoanType End Sub Private Sub TOT_Click() LoanType End Sub Private Sub TPD_Click() LoanType End Sub Private Sub txtAccount_Change() strAccount = txtAccount.Text End Sub Private Sub txtAddress1_AfterUpdate() txtProperty1 = txtAddress1 End Sub Private Sub txtAddress1_Change() strAddress = txtAddress1 + vbLf + txtAddress2 + vbLf + txtAddress3 End Sub Private Sub txtAddress2_AfterUpdate() txtProperty2 = txtAddress2 End Sub Private Sub txtAddress2_Change() strAddress = txtAddress1 + vbLf + txtAddress2 + vbLf + txtAddress3 End Sub Private Sub txtAddress3_AfterUpdate() txtProperty3 = txtAddress3 End Sub Private Sub txtAddress3_Change() strAddress = txtAddress1 + vbLf + txtAddress2 + vbLf + txtAddress3 End Sub Private Sub txtCity_AfterUpdate() txtPropCity = txtCity End Sub Private Sub txtCity_Change() strCity = txtCity.Text End Sub Private Sub txtComplete_Change() End Sub Private Sub txtCustNo_Change() strCustNo = txtCustNo.Text End Sub Private Sub txtDatePaid_Change() strDatePaid = txtDatePaid End Sub Private Sub txtDebit1_Change() strDebit = txtDebit1 + txtDebit2 + txtDebit3 + txtDebit4 + txtDebit5 End Sub Private Sub txtDebit2_Change() strDebit = txtDebit1 + txtDebit2 + txtDebit3 + txtDebit4 + txtDebit5 End Sub Private Sub txtDebit3_Change() strDebit = txtDebit1 + txtDebit2 + txtDebit3 + txtDebit4 + txtDebit5 End Sub Private Sub txtDebit4_Change() strDebit = txtDebit1 + txtDebit2 + txtDebit3 + txtDebit4 + txtDebit5 End Sub Private Sub txtDebit5_Change() strDebit = txtDebit1 + txtDebit2 + txtDebit3 + txtDebit4 + txtDebit5 End Sub Private Sub txtDob_Change() strDOB = txtDob.Text End Sub Private Sub txtERC_Change() strERC = txtERC.Text End Sub Private Sub txtIndex1_Change() strIndex1 = txtIndex1.Text End Sub Private Sub txtIndex2_Change() strIndex2 = txtIndex2.Text End Sub Private Sub txtInspection_Change() strInpection = txtInspection.Text End Sub Private Sub txtLoan_AfterUpdate() If txtLoan.Value > 3999999 Then MsgBox "Are you sure the loan is over £4 million?" End Sub Private Sub txtLoan_Change() strLoan = txtLoan.Text End Sub Private Sub txtLoan_Exit(ByVal Cancel As MSForms.ReturnBoolean) End Sub Private Sub txtLoanDate_Change() strLoanDate = txtLoanDate.Text End Sub Private Sub txtMaturity_Change() strMaturity = txtMaturity.Text End Sub Private Sub txtName_AfterUpdate() VerifyInput ActiveControl End Sub Private Sub txtName_Exit(ByVal Cancel As MSForms.ReturnBoolean) If ActiveControl.Tag = "failed" Then Cancel = True ActiveControl.Tag = "" End If End Sub Sub VerifyInput(TB As MSForms.TextBox) Dim Text As String Text = inputbox("Please re-enter the text") If Text <> "" Then If Text <> TB.Value Then MsgBox "The text doesn't match - try again" TB.Value = "" TB.Tag = "failed" End If End If End Sub Private Sub txtNat_Change() strNat = txtNat.Text End Sub Private Sub txtPayment_Change() strPayment = txtPayment End Sub Private Sub txtPlan_Change() strPlan = txtPlan.Text End Sub Private Sub txtPost1_AfterUpdate() txtPropPost1 = txtPost1 End Sub Private Sub txtPost1_Change() strPostcode = txtPost1.Text + txtPost2.Text End Sub Private Sub txtPost2_AfterUpdate() txtPropPost2 = txtPost2 End Sub Private Sub txtPost2_Change() strPostcode = txtPost1.Text + txtPost2.Text End Sub Private Sub txtProductCode_Change() strProductCode = txtProductCode.Text End Sub Private Sub txtPropCity_Change() strPropCity = txtPropCity.Text End Sub Private Sub txtProperty1_Change() strPropAddress = txtProperty1 + vbLf + txtProperty2 + vbLf + txtProperty3 End Sub Private Sub txtProperty2_Change() strPropAddress = txtProperty1 + vbLf + txtProperty2 + vbLf + txtProperty3 End Sub Private Sub txtProperty3_Change() strPropAddress = txtProperty1 + vbLf + txtProperty2 + vbLf + txtProperty3 End Sub Private Sub txtPropPost1_Change() strPropPostcode = txtPropPost1 + " " + txtPropPost2 End Sub Private Sub txtPropPost2_Change() strPropPostcode = txtPropPost1 + " " + txtPropPost2 End Sub Private Sub txtPurp_Change() If txtPurp.Text = "22" Then strFtb = 22 Else strFtb = "" If txtPurp.Text = "25" Then strUnen = 25 Else strUnen = "" strPurpose = txtPurp.Text End Sub Private Sub txtRef_Change() strRef = txtRef.Text End Sub Private Sub txtShort_Change() strShort = txtShort.Text End Sub Private Sub txtSort_Change() strSort = txtSort.Text End Sub Private Sub txtStart_Change() strStart = txtStart.Text End Sub Private Sub txtSwitch_Change() strSwitch = txtSwitch.Text End Sub Private Sub txtValuation_Afterupdate() If txtValuation <> "" Then If txtLoan.Value / txtValuation.Value > 0.9 Then chbHLC = True Else chbHLC = False Else: chbHLC = False End If On Error Resume Next strValuation = txtValuation.Text End Sub Private Sub UserForm_Click() End Sub Private Sub UserForm_Initialize() formAO.SetDefaultTabOrder Me.Height = 700 Me.ScrollBars = fmScrollBarsVertical Me.ScrollHeight = 900 With cmbSex .AddItem "M" .AddItem "F" End With With cmbMarital .AddItem "U" .AddItem "M" .AddItem "S" End With With cmbFlex .AddItem "1" .AddItem "3" End With strSic = "071" End Sub Public Sub LoanType() If FA.Value = True Then txtPurp.Text = 2 If RMG.Value = True Then txtPurp.Text = 3 End Sub
i'm having some luck with the following code: in the textbox afterupdate even i store the text in a variable strTemp and call a subroutine called 'verify'. only problem is i can't get the focus/cursor to stay in the textbox if the user fails the verification. probably an easy solution to this, would you happen to know what it is? (or alternatively, what the problem is with the code you gave me originally, which has been bugging me for hours!) Cheers
Private Sub txtName_AfterUpdate() strTemp = txtName If txtName <> "" Then Call verify End Sub Sub verify() Dim Text As String Text = inputbox("Please re-enter text to verify)") If Text <> strTemp Then MsgBox "Text did not match - try again" txtName = "" txtName.SetFocus Else txtShort.SetFocus End If End Sub
Hello ,
You need to use the the TextBox_Exit() event to return the user back to the text box if the verification fails.
Sincerely,Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean) If Verify = False Then Cancel = True TextBox1.Value = "" End If End Sub
Leith Ross
Ok got it working. Thanks for all your help
(to anyone who wants to use this - the above will leave the original input in the box, which is better for my purposes. a line like txtname="" will clear it)Private Sub txName_Exit (ByVal Cancel As MSForms.ReturnBoolean) If strVerify = "fail" Then Cancel = True End If End Sub
after working on this for hours...
'Path/File error'
document lost
*shoots laptop*
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks