Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    tylerg11 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Sep 2011
    Posts
    200

    Preventing a Null error

    Due to database design, I am not allowing Null values in my tables. On my main form, the default value for the text boxes is " ". However, if the user removes the zero-length string, they will get a Null error. I've tried many different things to capture the Null entry and convert it using the Nz() function, but I can't get rid of it!

  2. #2
    help_me_with_access is offline help_me_with_excel
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    262
    did you try checking it in visual basic before the rec is entered? try "isnull(control)"

  3. #3
    tylerg11 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Sep 2011
    Posts
    200
    I tried the following code:

    Code:
    Private Sub txtTitle_LostFocus()
        If IsNull(txtTitle) Then
            txtTitle.Text = ""
        End If
    End Sub
    It still returned the "cannot insert Null value into Variant data type" error.

  4. #4
    help_me_with_access is offline help_me_with_excel
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    262
    well you can't change a value on one of the control's events sir. the control can't change when it's got focus.

    how about changing it at the time the record is entered? e.g. - BEFORE the record is entered? what do you have setup as an entry control? button?

  5. #5
    tylerg11 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Sep 2011
    Posts
    200
    I have a seperate form for entering new records, and I can catch any nulls when they hit the "Submit" button. But I am talking about editing existing records. If the field currently contains data, but the users wants to remove it. A zero-string value should be entered if they leave the field blank.

  6. #6
    help_me_with_access is offline help_me_with_excel
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    262
    oh i see....so this is happening on a form for editing purposes, right?

    well I'm not sure but I know there are options. like maybe a validation rule like this?

    Code:
    =iif(me!control = null, "", me!control)
    or maybe a query that runs after the form is closed? like something to update all nulls in the table to ""? I think the default is null if someone just delets out data in a field? isn't it?

    maybe you could try using the nz() function in the actual validation rule? try all of that stuff, something is bound to work. obviously sql would work, but if you don't have form locks for multiple users set up that's not a good idea as you're asking for corruption.

  7. #7
    tylerg11 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Sep 2011
    Posts
    200
    Ok. I'll probably keep messing with it. Thanks.

  8. #8
    Rod is offline Expert
    Windows Vista Access 2007
    Join Date
    Jun 2011
    Location
    Metro Manila, Philippines
    Posts
    679
    Please excuse me butting in.

    tylerg11: What are you doing testing the Text property? You need to test the Value (default) property and I suggest it is best to do this is in the BeforeUpdate event because it occurs before anything is changed in the underlying recordset or database and because you can if you wish cancel the event. Hence:

    Code:
    Private Sub txtTitle_BeforeUpdate(Cancel as Integer)
        If IsNull(txtTitle) Then
            txtTitle= ""
        End If
    End Sub
    or pedantically

    Code:
    Private Sub txtTitle_BeforeUpdate(Cancel as Integer)
        If IsNull(txtTitle.Value) Then  txtTitle.Value = ""
    End Sub

  9. #9
    tylerg11 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Sep 2011
    Posts
    200
    Still not working, sir. My DB is linked to SQL tables. Could this also be part of the issue. The error says, "You tried to assign the Null value to a variable that is not a Variant data type." Is there a different way this needs to be addressed?

  10. #10
    John_G is offline VIP
    Windows XP Access 2003
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Hi -

    When do you get the error? As soon as you leave the text field, or as soon as you leave the record? There is a big difference. You will only get the error when you leave the record, and MS Access tries to update the record in the database. Try using the AfterUpdate event of the Text Box:

    [txtTitle] = nz(txtTitle,"")

    Rod: You have the right idea, but trying to change the contents of a control in the Before_Update event of that same control will give you an error. All you can do in the Before Update is check the value, and cancel the update if the value isn't valid.

    John

  11. #11
    tylerg11 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Sep 2011
    Posts
    200
    It is immediately after I leave the field, not the record.

    If I clear the contents of the field, and hit the Tab key, I get the error.

  12. #12
    tylerg11 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Sep 2011
    Posts
    200
    I tried what you suggested, but I still get the error.

  13. #13
    John_G is offline VIP
    Windows XP Access 2003
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    You asked earlier if using a SQL Server database might be the issue - well, maybe. What is the data type of the field in the SQL database that is causing the problem?

    In MS Asscess 2003 help, I found this:

    "...that is connected to a Microsoft SQL Server database, you can enter a zero-length string in a field whose data type is varchar or nvarchar."

    I'm not all that familiar with SQL Server databases yet - but you might want to look into that aspect.

    John

  14. #14
    tylerg11 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Sep 2011
    Posts
    200
    Thanks for the help. Yes, the field is nvarchar. Not sure why I'm having so much trouble with it. If I manually enter a zero-length string and leave the field it works perfectly. Just trying to do it with code and it is just not working..

  15. #15
    John_G is offline VIP
    Windows XP Access 2003
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    I may have found it. Put the [txtTitle] = nz(txtTitle,"") in the ON Exit event of the text box.

    It seems to work with an MS Access table - here's hoping it good for SQL as well.

    John

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 6
    Last Post: 05-10-2012, 10:57 AM
  2. Error 94: Invalid Use of Null
    By athomas8251 in forum Forms
    Replies: 3
    Last Post: 11-09-2011, 11:46 AM
  3. DLookUp function giving invalid use of null error
    By shubhamgandhi in forum Programming
    Replies: 4
    Last Post: 07-21-2011, 06:04 PM
  4. Replies: 2
    Last Post: 03-29-2011, 01:26 PM
  5. Runtime error in preventing duplicates code
    By emilyrogers in forum Access
    Replies: 10
    Last Post: 10-07-2010, 08:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums