Hi all, I'm in no way an expert on VBA but I do have a good understanding of the basics (mainly thanks to all the support and help on here!)
I am really getting confused and just going round in a loop trying to workout the flow of some textboxes on a Userform. The main purpose of my aim is to have a coded solution to whatever a user inputs into a textbox be it a text value, a numeric value or simply trying to exit a textbox with no value entered and something is required before the user can move on to the next textbox. I know how to format each textbox (currency or dates) and the actual userform is designed how I need it to be with the tab order also correctly numbered.
To me, logically the code seems to follow the flow and covers all possible inputs into txbTotalCost but I keep coming up against errors like 'Block if without if' so I put another 'end if' in and then it errors again but now saying 'end if without block if ' and if I sort these errors out it then seems to set the focus to textboxes that are disabled when the userform is activated??
I really sorry if this thread isn't explaining things very well it may show how confused my brain is right now trying to sort out something I assumed was going to be simply to code lol. I think its because I'm not really understanding what Elseif does and when to use it and if its used do I need an End if ect
Just for a quick summary txbTotalCost can not be left empty it must have a numeric value entered before the user can move on to the next relevant textbox. The value can be either a '0' or a value of '1' or more.
So basically in txbTotalCost (the first active textbox) the user has to enter a numeric value if the enter a text value then I have the following code in the change event
This checks to make sure a numeric value has been entered and if it hasn't prompts the user with a Msgbox and sets the focus back to the textbox.
And in the Exit event I have
I am really getting confused and just going round in a loop trying to workout the flow of some textboxes on a Userform. The main purpose of my aim is to have a coded solution to whatever a user inputs into a textbox be it a text value, a numeric value or simply trying to exit a textbox with no value entered and something is required before the user can move on to the next textbox. I know how to format each textbox (currency or dates) and the actual userform is designed how I need it to be with the tab order also correctly numbered.
To me, logically the code seems to follow the flow and covers all possible inputs into txbTotalCost but I keep coming up against errors like 'Block if without if' so I put another 'end if' in and then it errors again but now saying 'end if without block if ' and if I sort these errors out it then seems to set the focus to textboxes that are disabled when the userform is activated??
I really sorry if this thread isn't explaining things very well it may show how confused my brain is right now trying to sort out something I assumed was going to be simply to code lol. I think its because I'm not really understanding what Elseif does and when to use it and if its used do I need an End if ect
Just for a quick summary txbTotalCost can not be left empty it must have a numeric value entered before the user can move on to the next relevant textbox. The value can be either a '0' or a value of '1' or more.
So basically in txbTotalCost (the first active textbox) the user has to enter a numeric value if the enter a text value then I have the following code in the change event
This checks to make sure a numeric value has been entered and if it hasn't prompts the user with a Msgbox and sets the focus back to the textbox.
VBA Code:
Private Sub txbTotalCost_Change()
If Not IsNumeric (txbTotalCost) Then
MsgBox "It looks like you've entered a text value. Please only enter a numeric value or a 0 (zero). ", vbExclamation, "Invalid Entry"
Cancel = True
txbTotalCost.Value = ""
txbTotalCost.SetFocus
txbTotalCost.BackColor = RGB(204, 255, 255)
End If
End Sub
And in the Exit event I have
VBA Code:
Private Sub txbTotalCost_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If txbTotalCost.Value = "" Then
MsgBox "It looks like you've left this box empty. Please only enter a numeric value or a 0 (zero). ", vbExclamation, "Invalid Entry"
Cancel = True
txbTotalCost.Value = ""
txbTotalCost.SetFocus
txbTotalCost.BackColor = RGB(204, 255, 255)
ElseIf txbTotalCost.Value = "0" Then
txbTotalCost.Value = Format(Textbox1.Value, "£0.00")
txbTotalCost.BackColor = RGB(255, 255, 255)
txbTotalCostDeposit.Value = "0"
txbTotalCostDeposit.Value = Format(txbTotalCostDeposit.Value, "£0.00")
txbTotalCostDepositDate.Enabled = False ' This is actually disabled when the userform is activated so do I need this line?
txbTotalCostBalance.Value = txbTotalCost.Value
txbTotalCostBalance.Value = Format(txbTotalCostBalance.Value, "£0.00")
txbTotalCostBalanceDatePaid.Enabled = False ' Also disabled when the userform is activated so do I need this line?
txbFestivalNotes.SetFocus
Else
If txbTotalCost.Value > "1" Then
txbTotalCost.Value = Format(Textbox1.Value, "£0.00")
txbPitchDeposit.Enabled = True ' Disabled when the userform is activated
txbPitchDeposit.SetFocus
End If
End If
End Sub