• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Textbox Text Disappear on Text Entry Excel VBA

Kmahraz

Member
Hi:
I have this code that I found in the link below:
http://stackoverflow.com/questions/21536192/textbox-text-disappear-on-text-entry-excel-vba.
This code does exactly what I need with one exception, I have one section where I need to clear the userform and have it ready for next entry after the Add-button is pressed
So what's happening is that the textbox get cleared VS defaulting into "Please Enter Name Here"

Any suggestions?

Code:
Private Sub UserForm_Initialize()
    TxtWPN1.ForeColor = &HC0C0C0 '<~~ Grey Color
    TxtWPN1.Text = "Please Enter Name Here"
 End Sub 
Private Sub TxtWPN1_Enter()
    With TxtWPN1
        If .Text = "Please Enter Name Here" Then
            .ForeColor = &H80000008 '<~~ Black Color
            .Text = ""
        End If
    End With
End Sub
Private Sub TxtWPN1_AfterUpdate()
    With TxtWPN1
        If .Text = "" Then
            .ForeColor = &HC0C0C0
            .Text = "Please Enter Name Here"
        End If
    End With
End Sub
Thxs,

K
 
Hi Narayan,
Please find the attached file, you will notice that the code provided cover only the first textbox under the part number list from the second tab as shown below:
Note: The text boxes won't be activated till you check the checkboxes:p
upload_2016-1-4_22-41-57.png
Best,
K
 

Attachments

  • Test file .xlsm
    54.5 KB · Views: 19
Hello Narayan,
Thank you so much,
That's exactly what I needed to get this project moving.
Greatly appreciated
K@rim
 
Is there a way I can apply the code provided to all these textboxes without having to duplicate the code again and again.
Can someone help?
TxtWPN1, TxtWPN2, TxtWPN3, TxtWPN4, TxtWPN5, TxtWPN6, TxtWPN7, TxtWPN8, TxtWPN9, TxtWPN10 ?
Code:
Private Sub UserForm_Initialize()
    TxtWPN1.ForeColor = &HC0C0C0 '<~~ Grey Color
   TxtWPN1.Text = "Please Enter Name Here"
    End Sub
Code:
Private Sub TxtWPN1_Enter()
    With TxtWPN1
        If .Text = "Please Enter Name Here" Then
            .ForeColor = &H80000008 '<~~ Black Color
           .Text = ""
        End If
    End With
End Sub
Code:
Private Sub cmdAdd1_Click()
'Other code
With TxtWPN1
     .ForeColor = &HC0C0C0
     .Text = "Please Enter Name Here"
End With
End Sub
Best,
K@rim
 
Last edited:
Hi: @vletm
I've worked with userforms in VBA a bit and know some of the tricks for looping through all controls. However, I'm running into issues with this one, I need to apply this code to several textboxes and need a way to solve this without having to duplicate the code 100 time.
Any help will be greatly appreciated!
Code:
PrivateSub UserForm_Initialize()
    TxtWPN1.ForeColor = &HC0C0C0 '<~~ Grey Color  
TxtWPN1.Text = "Please Enter Name Here"
EndSub
PrivateSub TxtWPN1_Enter()
   With TxtWPN1
       If .Text = "Please Enter Name Here"Then
            .ForeColor = &H80000008 '<~~ Black Color          
.Text = ""
       EndIf
   EndWith
EndSub
PrivateSub cmdAdd1_Click()
'Other code
   With TxtWPN1
       If .Text = ""Then
            .ForeColor = &HC0C0C0
            .Text = "Please Enter Name Here"
       EndIf
   EndWith
EndSub
 
@vletm
Thank you and I appreciate the help,
I found this code that loop through only specific Controls on a specified page of a MultiPage Control.

Can you help me to use this with what I provided previously, Thanks:oops::rolleyes::(

Code:
Dim cCont As Control

For Each cCont In Me.MultiPage1.Pages(0).Controls

If TypeName(cCont) = "TextBox" Then

'DO STUFF HERE

End If

Next cCont



[B]End Su[/B]
 
Last edited:
Guys,
I had a chance to try several solutions with not much luck, can someone assist with my request please?
NARAYANK991 solution works; but I was hoping I can get some help to apply the solution to several textboxes, I can duplicate the code and get it to work; but I feel they must be a better way to achieve this.

Best,
K@rim
 
:DD@NARAYANK991
This is great and I thank you so much, there's only one thing that's not happening, the default text "Please Enter Name Here" doesn't disappear when the user click in the texboxes as it was in the previous code.

Regards
K@rim
 
Hi Narayan,
I also find out that when i press the Add-button in the second tab the data get saved but it overlap, please see picture below
In addition the text doesn't change to color to Black when you start typing into the textboxes.
Thank you Narayan
upload_2016-1-6_9-30-3.png
 
Last edited:
Hi Karim ,

No. I will , today.

As far as data saving when the ADD button is clicked , there is absolutely no data verification to see whether the textboxes even contain data ; such data validation will have to be added before it is saved to the worksheet.

I am sorry but I do not have the time to do all this.

As far as any specific problem arising out of the code I posted is concerned , I will help.

Narayan
 
Hello Narayan,
I really appreciate all you have done to assist me with this project, you provided me with a lot knowledge and tricks and I am very greatful and I appreciate any help I can get.
Regards,
Karim
 
Hi @vletm
I was hoping that you may be able to assist me with the code in the Test file that Narayan provided,
I Just need some help to figure out the two below items below:
  1. "Please Enter Name Here" doesn't disappear when the user click in the texboxes.
  2. Text need to change to black when you click into the textboxes
Regards,
Mahraz:)
 
@Kmahraz
There is no action for those! like ...
Private Sub TxtWPN3_Enter()
' code
End Sub
You case would need at least one Macro for '_Enter()'
which can solve which object are You clicking ... (Did You get an idea?)
and
same action should be used with all other same kind on objects!
If that 'Test file.xlsm' is ONLY 'Test file.xlsm' You have to do it to the REAL file too.
Or where is that kind of part of code?
I found one Macro, which sets text for set of those objects, but not for all.
You need something #7 Reply, do You?
It's like You have 5 buttons - one Macro - 5 different action, depends which button do You press..
Ideas ... Questions?
 
Hi @vletm
I believe I understand what you explained, in reply 11 from NARAYAN, he created the this codes:
Code:
Private Sub UserForm_Initialize()
            Dim cCont As Control
           
            Application.EnableEvents = False
           
            For Each cCont In Me.MultiPage1.Pages("Page2").Controls
                With cCont
                     If .Name Like "Txt?PN" & "*" Then
                        .Object.ForeColor = &H80000011
                        .Object.Text = "Please Enter Name Here"
                     End If
                End With
            Next
           
            Application.EnableEvents = True
End Sub
Code:
Private Sub CommandButton4_Click()
            Dim cCont As Control
i = Sheet5.Cells(Rows.Count, "A").End(xlUp).Row + 2
Sheet5.Range("A" & i) = Sheet1.Range("M" & Sheet1.Cells(Rows.Count, "M").End(xlUp).Row)
Application.EnableEvents = False
            For Each cCont In Me.MultiPage1.Pages("Page2").Controls
                With cCont
                     If .Name Like "Txt?PN" & "*" Then
                        .Object.ForeColor = &H80000011
                        .Object.Text = "Please Enter Name Here"
                     End If
                End With
            Next
Application.EnableEvents = True
End Sub
I believe this is what you referencing?
 

Attachments

  • Chandoo New test file.xlsm
    43.5 KB · Views: 14
@Kmahraz
You should have next Macro for 'disappear' & 'black' with Enter-act:
Code:
Sub Txt_Enter(tmp)
    Application.EnableEvents = False
    With frmDataInput.MultiPage1.Pages("Page2").Frame5.Object(tmp)
        .ForeColor = &H80000008
        .Text = ""
    End With
    Application.EnableEvents = True
End Sub

... and for every needed object (~100*) next Macro,
pre-'TxtWPN_1'-varies depend object:
Code:
Private Sub TxtWPN1_Enter()
    tmp = frmDataInput.MultiPage1.Pages("Page2").Frame5.ActiveControl.Name
'    Left(tmp,3) = where ever needs actions
    If Left(tmp, 3) = "Txt" Then Txt_Enter (tmp)
End Sub

Ideas ... Questions?
 
Last edited:
@Kmahraz
The 2nd code ... "minimal version"
This as many times as needed
for every object just 'TxtWPN1'-part has to modify!
Code:
PrivateSub TxtWPN1_Enter()
    Txt_Enter ("TxtWPN1")
EndSub
 
@vletm
Just wanted to thank you so much for taking the time, I will give it a try and let you know how it went.
Regards,
Mahraz
 
Back
Top