问题
I have created a data entry form on a spreadsheet using ActiveX TextBoxes, these are coupled with a submit details button and a clear data button, both at the end of execution set the text boxes back to blank to allow them to be refilled by the next user of the tool. However I ave noticed that after the boxes are set to "", when the user proceeds to begin filling the form in again each time the user selects one of the text boxes the previous entry details very briefly flash in the text box then disappear again. This is quite a frustrating issue as I am trying to make it so the previous users details are hidden.
I have searched many articles on the best way to set text boxes to empty and it seems that the main way is to simply set the .text property to be "", there seems to be no mention of anyone experiencing this behavior so I am not sure what the cause is. The code i have is very simplistic as it is done as a side project for an upcoming careers fair to allow a nicer tool for students entering there contact details.
Sub clearForm()
' Worksheets("Student Data").userName.Text = ""
' Worksheets("Student Data").emailAddr.Text = ""
' Worksheets("Student Data").contactNum.Text = ""
' Worksheets("Student Data").Course.Text = ""
' Worksheets("Student Data").gradDate.Text = ""
' Worksheets("Student Data").addComment.Text = ""
textbox1clear
End Sub
Sub submitData()
'Code here that takes users entered data and outputs to hidden and protected sheet
'After data copied text boxes are cleared of users information, same as above sub
Worksheets("Student Data").userName.Text = ""
Worksheets("Student Data").emailAddr.Text = ""
Worksheets("Student Data").contactNum.Text = ""
Worksheets("Student Data").Course.Text = ""
Worksheets("Student Data").gradDate.Text = ""
Worksheets("Student Data").addComment.Text = ""
End Sub
Sub textbox1clear()
Dim sel
sel = Selection.Address
Sheet1.userName.Activate
Application.SendKeys ("Test ")
Sheet1.userName.Value = ""
Range(sel).Activate
End Sub
Expected - Text boxes should be clear of data ready for next user.
Actual - Text boxes are clear until user selects them to begin entering data at which point previous entry briefly appears in the box and disappears again.
回答1:
This is common textbox behaviour. Unless the textbox is selected again, the changes aren't actually completed. I can see how this is inconvenient if used by other users and data entered is confidential. After numerous tests, the only working way I found around this is to trigger this flicker after clearing by making VBA select and deselect the input box, and simulating new user input with sendkeys.
Sub textbox1clear()
Dim sel
sel = Selection.Address
Sheet1.TextBox1.Activate
Application.Sendkeys(" ")
Sheet1.TextBox1.Value = ""
Range(sel).Activate
End Sub
I have built in a selection recall to make it (slightly) more seamless if the sub is called with a button. However if this is omitted and no selection is made after this sub is ran, the text will flicker again when the textbox is deselected.
Also note this should always be called manually, if it is called from a Textbox_LostFocus
like I did, it will enter a continuous loop as long as the textbox isn't selected.
It's not a very elegant solution, but if data protection is priority, it will do the trick.
If this is not sufficient, you might want to hide your input altogether if this is an option.
回答2:
you could use a quite long string made of "spaces" and then substitute them with a single "space"
Public Sub clearForm()
Worksheets("Student Data").userName.Text = " " 'vbNullString
Worksheets("Student Data").userName.Text = vbNullString
End Sub
make sure that quite long is enough for your textbox to be covered
Or
you could use a "dummy/informative" text to be removed right after entering the textbox for a subsequent input
Public Sub clearForm()
Worksheets("Student Data").userName.Text = "cleared"
…. (rest of your code
End Sub
Private Sub userName_GotFocus()
With Worksheets("Student Data").userName
If .Text = "cleared" Then .Text = vbNullString
End With
End Sub
Private Sub emailAddr_GotFocus()
With Worksheets("Student Data").emailAddr
If .Text = "cleared" Then .Text = vbNullString
End With
End Sub
… (and so on for all your textboxes)
来源:https://stackoverflow.com/questions/58266970/after-setting-activex-textbox-to-empty-value-previous-text-briefly-appears-in-b