By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,606 Members | 1,784 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,606 IT Pros & Developers. It's quick & easy.

Checking for null value in a textbox before inserting it to table

P: 4

I have a text box on a form, and I want to save the user input value to a table when the user moves to a different control on the form or closes the form.

I placed my code as a LostFocus event, and it is properly inserting the values. However, I also want to be able to check for a null value and return the focus to textbox (see below).

Expand|Select|Wrap|Line Numbers
  1.     If Not IsNull(Me.textbox1.Value) Then
  3.         strInput = Me.textbox1.Value
  5.         strSQL = "INSERT INTO table1 ([field1]) VALUES ('" & strInput & "')"
  7.         db.Execute (strSQL)
  9.     Else
  10.         MsgBox "Please enter a value"
  11.         Me.textbox1.SetFocus
  12.         Exit Sub
  13.     End If
This will trigger the message, but does not set the cursor/focus back to the textbox I want.

What is the best way to check for a null value as soon as the user leaves the textbox and place the cursor back in the textbox?
May 23 '14 #1
Share this Question
Share on Google+
3 Replies

Expert 100+
P: 1,240
You might consider taking advantage of the validation properties that are part of the textbox control. You could add IS Not Null to the Validation Rule property of the text box.

That does not help you much if the user never enters the text box. Do you need additional help to cover that possibility?

May 23 '14 #2

P: 4

Thanks for the tip. I've set the validation properties to the following, but it seems that even when the user enters the text box (by clicking or pressing tab), if no value is entered initially, the validation text does not appear. Once something is entered and deleted completely, the text will then pop up. Is this a normal behavior?


I would like to cover the possibility that you mentioned, and I'm guessing I'll have to use some combination of an event procedure and .SetFocus property. I'm still curious though... is there a reason why my initial code did not work as intended?

May 23 '14 #3

Expert Mod 15k+
P: 31,769
Is there any reason why you're repeating the logic that Access does for you in your form?

Typically, you would handle this with a bound form and set the properties of the field involved to disallow Nulls. Very much simpler that way. It's how Access is designed to work.
May 25 '14 #4

Post your reply

Sign in to post your reply or Sign up for a free account.