jarekmos
01-20-2020, 06:40 PM
Hi, I am having trouble with the setfocus command and I have done a bit of searching but my knowledge of vba is very little.
I would like a number value to be entered in TextboxControl and on typing a value if the data is not in the correct format a message will appear with an OK Button. When OK is selected I would like the wrong data to be cleared and the cursor back in the TextboxControl.
The code below is working fine except the SetFocus doesn't appear in the activate control.
Here is the code in Module mode:
Sub OnlyNumbers()
If TypeName(ExpensesForm.ActiveControl) = "TextBox" Then
With ExpensesForm.ActiveControl
If Not IsNumeric(.Value) And .Value <> vbNullString Then
ExpensesForm.ActiveControl.BackColor = &HC0C0FF 'read
MsgBox "Sorry, only numbers allowed"
.Value = vbNullString
ExpensesForm.ActiveControl.BackColor = &HFFFFFF
ExpensesForm.ActiveControl.SetFocus
End If
End With
End If
End Sub
Here is the code in Userform mode:
Private Sub UnitCost_Change()
Call OnlyNumbers
End Sub
Thanks in advance.
I would like a number value to be entered in TextboxControl and on typing a value if the data is not in the correct format a message will appear with an OK Button. When OK is selected I would like the wrong data to be cleared and the cursor back in the TextboxControl.
The code below is working fine except the SetFocus doesn't appear in the activate control.
Here is the code in Module mode:
Sub OnlyNumbers()
If TypeName(ExpensesForm.ActiveControl) = "TextBox" Then
With ExpensesForm.ActiveControl
If Not IsNumeric(.Value) And .Value <> vbNullString Then
ExpensesForm.ActiveControl.BackColor = &HC0C0FF 'read
MsgBox "Sorry, only numbers allowed"
.Value = vbNullString
ExpensesForm.ActiveControl.BackColor = &HFFFFFF
ExpensesForm.ActiveControl.SetFocus
End If
End With
End If
End Sub
Here is the code in Userform mode:
Private Sub UnitCost_Change()
Call OnlyNumbers
End Sub
Thanks in advance.