Null if still Focus still on Textbox

wrightyrx7

Registered User.
Local time
Today, 16:49
Joined
Sep 4, 2014
Messages
104
Hi all,

I am trying to make a couple of mandetory fields via VBA.

Code:
If IsNull(Me.Name) Or Me.Name = "" Then
    MsgBox "Unable to save without a Name", vbOKOnly, "Missing Data"
    Exit Sub
End If

However, when I was testing it the textbox kept coming back as NULL even when it contained avalue. After playing around to try and correct it i noticed that when i removed focus from the textbox in question, it was picking up the value.

The code runs when the save button is pressed.

So is there a way to correct this? Or do I removed focus from the textbox before the code above is run?
 
Try running this first
Code:
If Me.Dirty Then
  Me.Dirty = False
End If
 
By definition, when you click the Save button, you have already moved the focus to the button.

Sounds to me like the Name textbox is unbound.
If so, bind it to the Name field so it will automatically save the value

Also simplify your code using

Code:
 if Nz(Me.Name,"")="" Then
 
Hi,

Have you checked the vba for the Name text box (eg on GotFocus or on Click or similar event procedures) that is making it think it is null? There must be something.

You could just use SetFocus to move focus to another control, but then you'd never know why!
 
Thank you all for taking the time to reply.

ridders - mentioned that i should have already moved focus when clicking the save button.
Orthodox - Said there must be something, and that if I just SetFocus to something else, i will never know why it was doing it.

So I used

Code:
Debug.Print Me.ActiveControl.Name

to see where the focus was when Save was pressed. It was still on the textbox.

Then I realised i had used a LABEL as the Save button (because I didnt want a button i just wanted text). Turns out if you use a label as a button it does not take the focus away from the previous control. So a replaced it with a button and all is working as it should.

LESSON LEARNT - Dont try and use a label as a button. Just make the background of a button transparent.

Thanks again for your replies. And lesson learnt for future projects :)
 
One thing I would *STRONGLY* recommend is renaming that control.

Me.Name should always give you the form's name.

If you don't rename the control, then you should at least switch to either Me!Name or Me.Controls("Name").

Co-opting reserved words as object names causes a LOT of problems.
 
LESSON LEARNT - Dont try and use a label as a button. Just make the background of a button transparent.

I agree though not sure why you think it has to be transparent....

You're at least the 3rd person this week who has had issues using labels for buttons.
 
One thing I would *STRONGLY* recommend is renaming that control.

The actual name of the control is NewStarterName1 i renamed it for example purposes. I probably caused more confusion by trying to create less confusion haha.

I agree though not sure why you think it has to be transparent....

You're at least the 3rd person this week who has had issues using labels for buttons.

I like the look of of just text rather than a button.

Buttons.png


I have learnt my lesson now, and will never use a label as a button again!!!
 

Users who are viewing this thread

Back
Top Bottom