Reputation: 446
I have a textbox on an access form that I would like to enable for edditing if the textbox is blank. I am new to VBA, and am uncertain of the best way to go about it. Using a double-click event would be fine with me, but I would prefer to do it on load.
I have tried the following code, but can't seem to get it to work. Any help would be appreciated.
Private Sub EmpID_DblClick(Cancel As Integer)
If EmpID.Text = "" Then
Me.EmpID.Enabled = True
End If
End Sub
Upvotes: 1
Views: 2973
Reputation: 15384
You are running into the Null problem
Null
is not the same as ""
Try this instead
If EmpID.Text Is Null Then
Me.EmpID.Enabled = True
End If
Upvotes: 1
Reputation: 91336
For things that should happen on a per record basis, you are best off using the current event. Do not use the .Text property for controls. If you must use a property, use .Value. The .Text property is only available when the control has focus and is generally only used in special cases such as the Change event.
Private Sub Form_Current()
If IsNull(EmpID) Then
Me.EmpID.Enabled = True
End If
End Sub
In a normal set-up, there is no chance of the control being equal to a zero-length string ("")
You can cover all bases by saying:
Trim(EmpID & "") = ""
Upvotes: 5