Return focus to placeholder in text box - vba

I have a login form. I have a username and password text box with a login and close button.
I would like:
Text saying "username" in the username and "password" in the pass field.
When clicked on the text disappears.
If clicked on and you focus on something else without typing anything, it will return back to the placeholder.
I tried this:
Private Sub Form_Load()
txt_username.Value = "Username"
txt_password.Value = "Password"
End Sub
Private Sub txt_username_Click()
If txt_username.Value = "Username" Then
txt_username.Value = ""
End If
End Sub
Private Sub txt_username_LostFocus()
If txt_username.Value = "" Then
txt_username.Value = "Username"
End If
End Sub
My issue is when a change is made, i.e. you type something, then delete it, after you do something else the placeholder doesn't return. Typing breaks my code.

You were close. Try using "GotFocus" instead of "Click":
Option Explicit
Private Sub Form_Load()
txt_username.Text = "Username"
txt_password.Text = "Password"
End Sub
Private Sub txt_username_GotFocus()
If Trim(txt_username.Text) = "Username" Then
txt_username.Text = ""
End If
End Sub
Private Sub txt_username_LostFocus()
If Trim(txt_username.Text) = "" Then
txt_username.Text = "Username"
End If
End Sub

Related

Word VBA: Target multiple text input fields and change Value on _Enter() and _AfterUpdate()

I am working with the Office365 version of Word. I have a VBA user form that I've created that contains text boxes with helper text as their intial value. I'm using code as below to clear out the values on user entry into the text field and repopulating the helper text if they leave the field empty:
Private Sub txtCount_Enter()
'When the user enters the field, the value is wiped out
With txtCount
If .Text = "Count No" Then
.ForeColor = &H80000008
.Text = ""
End If
End With
End Sub
Private Sub txtCount_AfterUpdate()
'If the user exits the field without entering a value, re-populate the default text
With txtCount
If .Text = "" Then
.ForeColor = &HC0C0C0
.Text = "Count No"
End If
End With
End Sub
My form has a dozen or so of these fields. I know I can somehow access a collection of Text Boxes in the form, but can I then call an action on them? Could someone provide me with an example if this is possible?
My other answer was focused on looping through all controls. To switch the default text, by textbox as they enter and exit the control (without a loop). I'd suggest a single function, based on the previous answer.
You will still need to populate the default text in both the .Tag & .Text properties
Private Sub ToggleControl(ByRef TB As Control, ByVal Hide As Boolean)
If Hide Then
If TB.Text = TB.Tag Then
TB.Text = ""
TB.ForeColor = &H80000008
End If
Else
If TB.Text = "" Then
TB.Text = TB.Tag
TB.ForeColor = &HC0C0C0
End If
End If
End Sub
Private Sub TextBox1_AfterUpdate()
Call ToggleControl(TextBox1, False)
End Sub
Private Sub TextBox1_Enter()
Call ToggleControl(TextBox1, True)
End Sub
Place the default value for each textbox in the .text property and the .tag property for this code to work.
When you call ControlToggle(Boolean) it will look through all the controls (but only target TextBoxes). If you passed True, it will hide the text in the control if the value of the textbox is the default value (located in the .tag property). If you pass False, it will find any blank fields and re-populate it with the default field.
Private Sub ControlToggle(ByVal Hide As Boolean)
Dim oControl As Control
For Each oControl In Me.Controls
If TypeName(oControl) = "TextBox" Then
If Hide Then
If oControl.Text = oControl.Tag Then
oControl.Text = ""
oControl.ForeColor = &H80000008
End If
Else
If oControl.Text = "" Then
oControl.Text = oControl.Tag
oControl.ForeColor = &HC0C0C0
End If
End If
End If
Next
End Sub

How to stop a private sub from running with a password

I have two private subs for active x check boxes. When I click one button another button is locked. I want to create a button that would act as an override, where if I type a password then the two private subs won't run.
Private Sub CRM_box_Click()
If CRM_box.Value = True Then
CheckBox14.Value = False
CheckBox14.Enabled = False
Else
CheckBox14.Value = False
CheckBox14.Enabled = True
End If
Private Sub RMP_box_Click()
If RMP_box.Value = True Then
CRM_box.Value = False
CRM_box.Enabled = False
Else
CRM_box.Value = False
CRM_box.Enabled = True
End If
To 'override' the Private Sub for your check boxes, you could do the following:
In your VBE, create a Module (if you don't already have one) and enter Public override As Boolean on the first line.
For each of your Private Subs for the Check Boxes, add the following above your current If statement: If override = False Then Exit Sub like so:
Private Sub CRM_box_Click()
If override = False Then Exit Sub
If CRM_box.Value = True Then
RMP_box.Value = False
RMP_box.Enabled = False
Else
RMP_box.Value = False
RMP_box.Enabled = True
End If
End Sub
For the password part of your question you could create a UserForm.
If you're not firmiliar with UserForms, there are a lot of tutorials and instructions on how to create them by searching something like "How to create a userform in excel" in Google or equivalent.
I made a simple UserForm with a label, textbox and commandbutton. See here.
For the commandbutton, you could use the following code:
Private Sub CommandButton1_Click()
Dim pwrd As String
Dim setPwrd As String
pwrd = UserForm1.TextBox1.Text
setPwrd = "abc" 'Change this string to whatever you want your password to be
If pwrd = setPwrd Then
override = False
Unload UserForm1
Else
MsgBox "Incorrect Password" & vbNewLine & "Please try again.", vbCritical
End If
End Sub
When you enter your password into the textbox and click the commandbutton, the vba compares your input with the variable setPassword. If it's a match it set's the Public variable override to False. If the password doesn't match you get a message to try again.
If override = False it directs your Private Sub for your check boxes to Exit Sub before executing the code - Your code is 'overridden'.
In summary:
Declare a Public Variable as a Boolean.
Set an If statement in each Private Sub for your check boxes.
Create a UserForm to enter a Password.
Write the code to set a password and for what happens when you submit your password.
You may find this answer helpful in understanding ways you can prevent other users from getting around your password form.

Makeshift Cue Banners

I'm trying to create some cue banners for my user form in Word. I got about half way through before I become stuck. I have it where the cue banner will disappear and clear the textbox once it has focus. If the user types their own text, that will be retained as well.
However, if the user doesn't type anything in the textbox once it has been cleared, I want to replace the cue banner along with its attributes (gray text and italicized). I can't seem to get it to work. Here's the code with everything related to this textbox below. The trouble is with the Leave event I think.
Private Sub UserForm_Initialize()
Me.txbShipToName1.Text = "name"
Me.txbShipToName1.Font.Italic = True
Me.txbShipToName1.ForeColor = &H80000006
End Sub
Private Sub txbShipToName1_Enter()
If Me.ActiveControl Is Me.txbShipToName1 And Me.txbShipToName1.Text = "name" Then
txbShipToName1.Font.Italic = False
txbShipToName1.ForeColor = &H80000008
txbShipToName1.Text = ""
End If
End Sub
Private Sub txbShipToName1_Leave()
If Me.ActiveControl Is Not txbShipToName1 And Me.txbShipToName1.Text = "" Then
txbShipToName1.Font.Italic = True
txbShipToName1.ForeColor = &H80000006
txbShipToName1.Text = LCase(txbShipToName1.Text)
txbShipToName1.Text = "name"
End If
End Sub
Private Sub txbShipToName1_Change()
If Me.ActiveControl Is Me.txbShipToName1 And Me.txbShipToName1 <> "name" Then
txbShipToName1.Text = UCase(txbShipToName1.Text)
End If
End Sub
I'm putting this here for anyone else who would like to know the solution. After a few days of messing with it, I finally realize I was over complicating things when I was messing with another piece of code. I eliminated the ActiveControl test because this is essentially already built into the Enter and Exit events. Here's the updated and working code.
Private Sub UserForm_Initialize()
'Populates cue banners
Me.txbShipToName1.Text = "Name"
Me.txbShipToName1.Font.Italic = True
Me.txbShipToName1.ForeColor = &H80000011
End Sub
Private Sub txbShipToName1_Enter()
'Removes cue banner upon entering textbox
If Me.txbShipToName1.Text = "Name" Then
txbShipToName1.Font.Italic = False
txbShipToName1.ForeColor = &H80000012
txbShipToName1.Text = ""
End If
End Sub 'txbShipToName1_Enter()
Private Sub txbShipToName1_Change()
'Converts textbox to uppercase
If Me.txbShipToName1.Text <> "Name" Then
txbShipToName1.Text = UCase(txbShipToName1.Text)
End If
End Sub 'txbShipToName1_Change()
Private Sub txbShipToName1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
'Replaces cue banner upon exiting textbox
If Me.txbShipToName1.Text = "" Then
txbShipToName1.Font.Italic = True
txbShipToName1.ForeColor = &H80000011
txbShipToName1.Text = "Name"
End If
End Sub 'txbShipToName1_Exit(ByVal Cancel As MSForms.ReturnBoolean)

Formatting Text Boxes in a Userform

I have a Userform that includes Text Boxes with multiple formats. I have the Initialize as blank ("") and then format them using afterupdate(). This all works fine, my issues come from the possibility of the user miss keying the data the first go around or just clicking aimlessly on there screen. After you input a value it formats it correctly when you move from the text box. But if you reselect the text box then move away again, it clears the value. And if you do this with the text box that is formatted as a percent it actually bugs out with a mismatch error.
Here is a slice of my current code:
Private Sub UserForm_Initialize()
ValueAnalysisTextBox.Value = ""
CapRateTextBox.Value = ""
End Sub
Private Sub ValueAnalysisTextBox_AfterUpdate()
ValueAnalysisTextBox.Value = Format(Val(ValueAnalysisTextBox.Value), "$#,###")
End Sub
Private Sub CapRateTextBox_AfterUpdate()
CapRateTextBox.Value = Format(Val(CapRateTextBox.Value) / 100, "Percent")
End Sub
Any thoughts on how to clean this up would be great.
Is this what you are trying?
Private Sub ValueAnalysisTextBox_AfterUpdate()
Dim amt As Double
amt = Val(Replace(ValueAnalysisTextBox.Value, "$", ""))
ValueAnalysisTextBox.Value = Format(amt, "$#,###")
End Sub
Private Sub CapRateTextBox_AfterUpdate()
Dim Perct As Double
Perct = Val(Replace(CapRateTextBox.Value, "%", "")) / 100
CapRateTextBox.Value = Format(Perct, "Percent")
End Sub
Note: I am not doing any other error handling. For example, user typing "Blah Blah" or pasting something else in the textbox. I am sure you can handle that.
I'd store the underlying values in the .Tag property of the TextBox, then use it to change the formatting back and forth in the Enter and Exit events:
Private Sub UserForm_Initialize()
ValueAnalysisTextBox.Value = vbNullString
ValueAnalysisTextBox.Tag = vbNullString
End Sub
Private Sub ValueAnalysisTextBox_Enter()
ValueAnalysisTextBox.Value = ValueAnalysisTextBox.Tag
End Sub
Private Sub ValueAnalysisTextBox_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If IsNumeric(ValueAnalysisTextBox.Value) Then
ValueAnalysisTextBox.Tag = Val(ValueAnalysisTextBox.Value)
ValueAnalysisTextBox.Value = Format$(ValueAnalysisTextBox.Tag, "$#,###")
Else
ValueAnalysisTextBox.Tag = vbNullString
End If
End Sub

Adding description into TextBoxes within an Excel VBA form that dissapears once user starts typing inside it

We have a userform with multiple textboxes and we would like to build something similar to the link image below, in terms of showing what the user should input in each text box:
http://d2o0t5hpnwv4c1.cloudfront.net/426_formsBestPractices/comments.png
The "default" text would disappear once the user starts typing (as opposed than once the user "lands" cursor within the textbox.
Also, if nothing gets entered within the textbox the default text would not be submitted and a blank would be used.
Can this be done?
Any suggestions will be greatly appreciated.
Can I ask why you want the default text to dissapear once a user changes the text and not once they enter the textbox?
This is not what most users will expect, I think it will be slightly confusing for some and wouldn't recommend it. The user will most likely try and delete the old text before typing their new text creating extra work.
I would use something like this:
Const sNameDefault As String = "Your Name Here"
Const sEmailDefault As String = "Your Email Here"
Private Sub UserForm_Initialize()
Me.TextBox1.Text = sNameDefault
Me.TextBox2.Text = sEmailDefault
CommandButton1.SetFocus
End Sub
'// TextBox1 - Name
Private Sub TextBox1_Enter()
With Me.TextBox1
If .Text = sNameDefault Then .Text = vbNullString
End With
End Sub
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
With Me.TextBox1
If .Text = vbNullString Then .Text = sNameDefault
End With
End Sub
'// TextBox2 - Email
Private Sub TextBox2_Enter()
With Me.TextBox2
If .Text = sEmailDefault Then .Text = vbNullString
End With
End Sub
Private Sub TextBox2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
With Me.TextBox2
If .Text = vbNullString Then .Text = sEmailDefault
End With
End Sub
Private Sub CommandButton1_Click()
Dim sName As String, sEmail As String
'// Get Name
If Me.TextBox1.Text = sNameDefault Then
sName = vbNullString
Else
sName = Me.TextBox1.Text
End If
'// Get Email
If Me.TextBox2.Text = sEmailDefault Then
sEmail = vbNullString
Else
sEmail = Me.TextBox2.Text
End If
MsgBox ("Your Name: " & sName & vbNewLine & " Your Email:" & sEmail)
Unload Me
End Sub
The above example is simply a userform with two textbox's and a commandbutton. Clicking inside the textbox will clear the default text. If the user enters nothing clicking another textbox or control will cause the default text to be added back. Once the command button is clicked the code will return blank if the default text remains.
Yes it is possible :)
I have created a sample for you. You can download it from here.
http://wikisend.com/download/143478/Sample.xlsm
The trick is to create 2 similar TextBoxes and hide the 'original' one behind the dummy TextBox ("Which has the default text")
When you start typing in the dummy, the text will actually be typed in the textbox which is hidden.
And when you are pulling values, simply pull the values from the 2nd text box so the default data is not considered :)
Hope this helps.
Code Used
Private Sub UserForm_Initialize()
TextBox1.SelStart = 0
TextBox1.SelLength = Len(TextBox1.Text)
End Sub
Private Sub CommandButton1_Click()
MsgBox TextBox2.Text
End Sub
Private Sub TextBox1_Change()
TextBox1.Visible = False
With TextBox2
.Text = Replace(TextBox1.Text, "Please enter your name", "")
.Visible = True
.SetFocus
.SelStart = Len(TextBox2.Text)
End With
End Sub