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

    Automatically replace NULL with zero string


    I don't allow NULL values in my database. So if a textbox is cleared of its datea on a bound form record how do I automatically "switch" this null to a zero string so that is what gets submitted to the database? Otherwise I get a null error. Seems like I need to catch this in the BeforeUpdate event?

  2. #2
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows XP Access 2003
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,544
    Try:
    If IsNull(Me.TextboxName) then
    Me.TextboxName = ""
    EndIf
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  3. #3
    tylerg11 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Sep 2011
    Posts
    200
    I tried that, and that didn't work either.

  4. #4
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows XP Access 2003
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,544
    "didn't work" does not really help us.
    What actually happens.
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  5. #5
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows XP Access 2003
    Join Date
    Aug 2013
    Posts
    7,862
    Since you are trying to validate the field after the user makes a change maybe you can try the AfterUpdate event.

  6. #6
    tylerg11 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Sep 2011
    Posts
    200
    It says it can't update the table with a NULL value. So it obviously isn't catching the Null and changing to a zero string.

    If I check using IsNull(TextboxName) it is checking the bound value which obviously can't be NULL.

  7. #7
    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 view. I don't allow empty strings in tables.

    Did you try the form BeforeUpdate? Textbox BeforeUpdate?
    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.

  8. #8
    tylerg11 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Sep 2011
    Posts
    200
    One of the messages I get is:

    "You tried to assign the Null value to a variable that is not a Variant data type."

  9. #9
    tylerg11 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Sep 2011
    Posts
    200
    Quote Originally Posted by June7 View Post
    I have the opposite view. I don't allow empty strings in tables.

    Did you try the form BeforeUpdate? Textbox BeforeUpdate?
    I just tried the textbox BeforeUpdate. If I used the form BeforeUpdate I would have to identify each control, right?

  10. #10
    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
    Yes. But I don't know that will even work. Never been an issue for me because I like null, don't want empty string.
    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.

  11. #11
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows XP Access 2003
    Join Date
    Aug 2013
    Posts
    7,862
    What about adding a validation rule in the field's property at the table level?

    Maybe
    >0 Or Is Null

  12. #12
    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
    Except OP does not want to allow null, so:

    >0 And Not Is Null

    And maybe set DefaultValue property to: ""

    But how would user input an empty string if they wanted to eliminate other input?

    All this is probably one reason I prefer Null in fields over empty string. Then handle nulls in queries and code.


    Clarify: How are you presently setting the field to not allow Null? Is the Required property set to Yes?


    Null indicates a value is not known. If you want the field set as Required then should not be an empty string either, should be known data input.

    Data control has Value and OldValue properties. The Value is the last entered value. I did a quick test with a textbox set as Required and used its BeforeUpdate event to capture the Null and change to "". It reads Null as the Value but errors and won't allow me to change the textbox or field value to anything. However, AfterUpdate seems to work after all:
    Private Sub TEST_AfterUpdate()
    If IsNull(Me.TEST) Then Me!TEST = ""
    End Sub


    Do you also not allow null in other field types?
    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.

  13. #13
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows XP Access 2003
    Join Date
    Aug 2013
    Posts
    7,862
    June,

    You nailed it. It won't take a value or empty string in the BeforeUpdate event. I tried the same thing, only not until after your post.

    I was able to change the control's control source to a "Temp" field within the same table/query in the Enter and Exit events. The Exit event I would change the source back to the original field. It surprisingly would transfer values between the two fields but, this won't transfer a null value. Might work by changing the ControlSource to Unbound too. Moot point now.

  14. #14
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows XP Access 2003
    Join Date
    Aug 2013
    Posts
    7,862
    Hmmm...... If IsNull(Me.TEST) Then Me!TEST = "" didn't work for me in the afterupdate. Validation rule is at the table.

    The only way I see it maybe working is to change the control properties (entering and exiting) and store the values or non-values public within the form. I guess my original >0 or Is Null validation is THE BEST fix afterall :Laughing

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

Similar Threads

  1. Replies: 2
    Last Post: 09-11-2012, 12:34 PM
  2. Replies: 3
    Last Post: 06-07-2012, 07:05 AM
  3. Issue Using Replace in SQL String
    By starryNight in forum Programming
    Replies: 15
    Last Post: 06-03-2011, 01:24 PM
  4. Replace a string/text in ms-access db
    By anziga in forum Queries
    Replies: 4
    Last Post: 12-31-2010, 06:40 PM
  5. replace characters in a string
    By blazixinfo@yahoo.com in forum Access
    Replies: 6
    Last Post: 08-06-2009, 03:36 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