Null if still Focus still on Textbox (1 Viewer)

wrightyrx7

Registered User.
Local time
Yesterday, 20:54
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?
 

bob fitz

AWF VIP
Local time
Today, 04:54
Joined
May 23, 2011
Messages
4,727
Try running this first
Code:
If Me.Dirty Then
  Me.Dirty = False
End If
 

isladogs

MVP / VIP
Local time
Today, 04:54
Joined
Jan 14, 2017
Messages
18,246
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
 

Orthodox Dave

Home Developer
Local time
Today, 04:54
Joined
Apr 13, 2017
Messages
218
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!
 

wrightyrx7

Registered User.
Local time
Yesterday, 20:54
Joined
Sep 4, 2014
Messages
104
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 :)
 

Frothingslosh

Premier Pale Stale Ale
Local time
Yesterday, 23:54
Joined
Oct 17, 2012
Messages
3,276
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.
 

isladogs

MVP / VIP
Local time
Today, 04:54
Joined
Jan 14, 2017
Messages
18,246
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.
 

wrightyrx7

Registered User.
Local time
Yesterday, 20:54
Joined
Sep 4, 2014
Messages
104
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.



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

Users who are viewing this thread

Top Bottom