Empty field and Clear field !

achalrikhi

New Member
Joined
Nov 20, 2013
Messages
38
Dear all,

I am working on a form which have several text boxes. I want all the fields to resist when it is left blank. So I have used this code in the next textbox's gotfocus event, for each field :

Private Sub TxtCustAdd_GotFocus()


If IsNull(TxtCustName.Value) Then
MsgBox " Please enter a name "
Me.TxtCustName.SetFocus
End If


End Sub

This works fine. Simultaneously, I want the form with blank text boxes when it loads up. If I use the code to blank the text box like :

Private Sub Form_Load()


TxtCustID.Value = ""
TxtCustName.Value = ""
TxtCustAdd.Value = ""


End Sub



this works fine. But then the first code does not work and it allows to go for next text box allowing the previous one blank.

Please comment and suggest

Regards

Achal
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
It's because "" and Null are not the same thing so your comparison is not what you are expecting. The first is an empty string, which is still 'something' while Null, for lack of a better explanation, isn't anything. Some maintain that it isn't nothing though. Somewhat confusing for sure.
I use a small function in a STANDARD module for this sort of thing because you can refer to it whenever needed. I call it with If HasNoData(Me.cmbBeginDate) Then...
**********
Function HasNoData(vCheckVal As Variant) As Boolean
HasNoData = False
If IsNull(vCheckVal) Or vCheckVal = "" Then HasNoData = True
End Function

If the control value is either Null or "", the function returns True. No big deal, but the default property of controls such as textboxes is .value, so it is not necessary to use it like you are. If TxtCustID = "Roger" is OK. However, do not confuse textbox.value with textbox.text.
 
Upvote 0

Forum statistics

Threads
1,221,851
Messages
6,162,429
Members
451,765
Latest member
craigvan888

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top