Today I have tried very common experiments with "Textbox Tool" in Userform.
I'm working on some serious Login system for the VBA Applications which will authenticate the user's credentials by matching it from the external database(Google Sheet). If it matched with the source strings, the program will allow the user to access the remaining features of the application. This Login system will be required the active internet connection.
But, that is not the subject of the discussion here.
We have already seen on many websites and on applications, the default text imprinted on the textboxes. Let's say, you are on the login page of any website. In the Username Box, you could see the default text like "USERNAME" or "TYPE YOUR USER ID HERE" etc....
And When you click the textbox, the default text will be disappeared and textbox gets blank and it will allow the user to write their user id. And if you left the textbox empty, it will again show the Default text on the box.
So How we can code the same in Excel VBA Textboxes?
I have used two events of Textbox
1. Enter Event - The Enter event occurs before a control actually receives the focus from a control on the same form or report.
2. AfterUpdate Event - The AfterUpdate event occurs after changed data in a control or record is updated.
---------
Private Sub UserForm_Initialize()
'Assigning the DEFAULT values to the Textboxes
UserForm1.TextBox1.Value = "USERNAME"
'the Forecolor of the Textbox when userform initializes
UserForm1.TextBox1.ForeColor = &HC0C0C0 'light gray
End Sub
---------
Private Sub TextBox1_Enter()
UserForm1.TextBox1.Value = ""
UserForm1.TextBox1.ForeColor = &H0& 'black
End Sub
---------
Private Sub TextBox1_AfterUpdate()
'AFTER UPDATING THE TEXTBOX IF THERE'S NO STRING
If UserForm1.TextBox1.Value = "" Then
UserForm1.TextBox1.Value = "USERNAME"
UserForm1.TextBox1.ForeColor = &HC0C0C0
End If
End Sub
----------
It's work like Pro! Lol...
Try urself with this code
Thank you
- Kamal Bharakhda
No comments:
Post a Comment