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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • decafbutter
    New Member
    • Apr 2014
    • 4

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

    Hello,

    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).

    Code:
        If Not IsNull(Me.textbox1.Value) Then
            
            strInput = Me.textbox1.Value
        
            strSQL = "INSERT INTO table1 ([field1]) VALUES ('" & strInput & "')"
        
            db.Execute (strSQL)
        
        Else
            MsgBox "Please enter a value"
            Me.textbox1.SetFocus
            Exit Sub
        End If
    This will trigger the message, but does not set the cursor/focus back to the textbox I want.

    Question:
    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?
  • jimatqsi
    Moderator Top Contributor
    • Oct 2006
    • 1293

    #2
    decafbutter,
    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?

    Jim

    Comment

    • decafbutter
      New Member
      • Apr 2014
      • 4

      #3
      Jim,

      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?

      Click image for larger version

Name:	GVJFeVH.png
Views:	1
Size:	1.6 KB
ID:	5413764

      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?
      [imgnothumb]http://bytes.com/attachments/attachment/7659d1400888110/gvjfevh.png[/imgnothumb]
      Last edited by NeoPa; May 25 '14, 01:34 PM. Reason: Attached image - {NeoPa} Made pic viewable.

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32661

        #4
        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.

        Comment

        Working...