Results 1 to 9 of 9
  1. #1
    tylerg11 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Sep 2011
    Posts
    200

    Error: You tried to assign NULL value to a variable that is not a Variant data type

    Hello,

    I've been encountering this issue and have attempted many different things and have not had success. Here's my problem. I do not allow NULL values to be entered into my SQL database (Zero-length strings are used instead). So I have a bound form containing data. If the data is cleared from a bound field and a user attempts to save the form, the following error locks everything up.

    Start with this:

    Click image for larger version. 

Name:	not blank.jpg 
Views:	34 
Size:	2.4 KB 
ID:	14262

    After clearing the data:

    Click image for larger version. 

Name:	blank.jpg 
Views:	35 
Size:	1.8 KB 
ID:	14263

    The following error locks up.

    Click image for larger version. 

Name:	error.jpg 
Views:	34 
Size:	18.0 KB 
ID:	14261

    I know why this happens. What I need to figure out is how to replace a NULL submission to the database with a zero-string. I've tried using Lost Focus and Before Update and have not had any success with the methods I've constantly tried.

    Help would be appreciated!



    Thanks much.

  2. #2
    AlexHedley's Avatar
    AlexHedley is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Apr 2013
    Location
    England
    Posts
    180

    Error: You tried to assign NULL value to a variable that is not a Variant dat...

    Can you not set a Default Value for the control?

  3. #3
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    DefaultValue won't help once user has edited the field.

    What code have you tried?

    Try in the control's BeforeUpdate or AfterUpdate event:

    If IsNull(Me.controlname) Then Me.controlname = ""
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  4. #4
    AlexHedley's Avatar
    AlexHedley is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Apr 2013
    Location
    England
    Posts
    180

    Error: You tried to assign NULL value to a variable that is not a Variant dat...

    Correct me if I'm wrong but if it's a Default Value then it'll be changed from Null so even if removed later it'll not go back to Null but instead an empty string unless explicitly set?

  5. #5
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    I have the opposite situation. I don't allow empty strings but Null is good. This means if I try to input empty string (typing "" will do that) into bound textbox, Access gets upset and gives me an error message. I can't leave the textbox until I fix it. I can ESC to remove the "" and textbox is Null, ESC again and return the textbox to its original value (the default if new record).

    That is with an Access backend, I have never done data entry to an SQL database. Access has nothing against Null and if the SQL table rejects Null, apparently Access cannot 'catch' the error the way it does the empty string in my Access table and results in lockup error.

    I don't know if SQL table has property (or any properties at table level) for DefaultValue. DefaultValue is triggered only for a new record, not an existing record, and therefore will not help with this issue for an existing record. Existing or new record, the invalid entry must be corrected before attempt to save record. Hopefully my suggested code will work for either case, if not, perhaps an error handler would, maybe in the form BeforeUpdate event because it has a Cancel parameter.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  6. #6
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows XP Access 2003
    Join Date
    Aug 2013
    Posts
    7,862
    Quote Originally Posted by tylerg11 View Post
    .....What I need to figure out is how to replace a NULL submission to the database with a zero-string. I've tried using Lost Focus and Before Update and have not had any success with the methods I've constantly tried......

    Try

    In the before update event

    If isnull(Me.txtField.Value) then

    me.txtfield = ""

    end if

    I guess you can use the nz function too

    Anyway, I did a test with default values in 2010 compared to 2003. There have been instances (in the past) where I would use empty strings as default values for text boxes in A2003. I tried it in A2010 and the value becomes Null when creating a new record. Not sure if the error you are getting is related to your no Null's allowed thing. But, it seems that assigning empty strings in Access 2010 as default is a thing of the past.

    I was able to assign "" via VBA or type it into the bound control. The value will return as empty string. Default "" returns Null in 2010. At least in the tests I did. Same test in 2003, different result. 2003 default "" returns empty string. It does not return Null.

    Edit: I thought I would add.... Default value is default value. After the default value is changed, ie a new record is created and then the value is changed, the default value becomes inconsequential. Default value is only for new records.

  7. #7
    AlexHedley's Avatar
    AlexHedley is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Apr 2013
    Location
    England
    Posts
    180

    Error: You tried to assign NULL value to a variable that is not a Variant dat...

    Very interesting find, thanks for sharing.

  8. #8
    deepaksharma is offline Competent Performer
    Windows 7 32bit Access 2016
    Join Date
    Jul 2023
    Location
    india
    Posts
    389
    got any solution....?

  9. #9
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Instead of hi-jacking and resurrecting a 10-year-old thread, really should start your own. A solution was offered to the original poster but no feedback. Did you try it?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. type error if sub report has null vallue
    By tagteam in forum Access
    Replies: 5
    Last Post: 10-20-2013, 09:45 PM
  2. Assign value to variable on other form
    By tylerg11 in forum Access
    Replies: 3
    Last Post: 08-20-2013, 07:48 AM
  3. Replies: 1
    Last Post: 04-25-2013, 02:52 PM
  4. data type variable constants
    By markjkubicki in forum Programming
    Replies: 2
    Last Post: 06-20-2012, 09:57 PM
  5. Replies: 2
    Last Post: 09-27-2010, 02:17 PM

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