Results 1 to 6 of 6
  1. #1
    mar_t is offline Advanced Beginner
    Windows XP Access 2002
    Join Date
    Nov 2010
    Posts
    52

    How save Null fields to a table


    Good Day,

    I've created (thanks to Google) an sql statement to INSERT field/s values into my table and it works fine. But, how if a field or fields are empty or Null. As of this writing I'm still searching the web for answers. Any inputs are greatly appreciated.

    Code:
    Private Sub cmdSave_Click()
    sql = "Insert into tblEmployee(LastName,FirstName) Values ('" & LastName & "','" & FirstName & "')"
    DoCmd.RunSQL (sql)
    myList.Requery
    End Sub
    Thank you in advance.

  2. #2
    Rod is offline Expert
    Windows Vista Access 2007
    Join Date
    Jun 2011
    Location
    Metro Manila, Philippines
    Posts
    679
    To answer your question - presumably the table fields are TEXT - I would use the Nz function. This function traps nulls and substitutes the value of your choice.

    Nz(LastName, "") returns the last name or a zero length string if LastName is null. Now you need to place quotation marks around the string. I suggest the values expression should be written as:

    Code:
     
    " ... Values ('" & Nz(LastName, "") & "', '" & Nz(FirstName, "") & "')"

  3. #3
    mar_t is offline Advanced Beginner
    Windows XP Access 2002
    Join Date
    Nov 2010
    Posts
    52
    Hi Rod,

    Thank you, I'll try the code. How if the table field is a number?

  4. #4
    Rod is offline Expert
    Windows Vista Access 2007
    Join Date
    Jun 2011
    Location
    Metro Manila, Philippines
    Posts
    679
    I think I have mislead you somewhat.



    Here's the situation as I know it.
    • All controls may have a value of null. In particular unless you take specific action, unbound text boxes, combo boxes and list boxes are initialised with null.
    • Text and number table fields may also contain null. (You have however specifically to allow a text field to contain a zero-length string. This is where I did not explain properly in my first post.)
    • Only a variant type VBA variable can contain a null.
    Thus using INSERT INTO with null values should cause no problem with either text or number fields, unless the 'Required' property is set to 'Yes' for that field.

  5. #5
    mar_t is offline Advanced Beginner
    Windows XP Access 2002
    Join Date
    Nov 2010
    Posts
    52
    Hi Rod,

    The code works with table field/s with Text as the data type but I received a pop-up message " Microsoft access didn't update 1 field(s) due to a type conversion error..." for null field/s with 'Numbers' as the data type. In my tblEmployee i have a field PhoneNo and the data type is Numbers and the 'Required' property is set to 'No'. Help.

  6. #6
    Rod is offline Expert
    Windows Vista Access 2007
    Join Date
    Jun 2011
    Location
    Metro Manila, Philippines
    Posts
    679
    Hold up a moment. Which code are we talking about?

    Nz(PhoneNumber, "") will not work for numeric table fields because a zero-length string is not numeric. If you don't want a null in your numeric table field the correct syntax is Nz(PhoneNumber,0).

    Code:
    " ... Values ('" & Nz(LastName, "") & "', '" & Nz(FirstName, "") & "', " & Nz(PhoneNumber, 0) & ")"
    I include the above example because the quotation mark syntax is slightly different for numeric fields.

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

Similar Threads

  1. Replies: 9
    Last Post: 01-20-2011, 02:22 PM
  2. Prompt on close and don't save the null record
    By hasanrazaj1 in forum Forms
    Replies: 2
    Last Post: 10-24-2010, 09:24 AM
  3. Combine two fields, Null fields involved
    By yes sir in forum Access
    Replies: 9
    Last Post: 10-03-2010, 09:20 AM
  4. Replies: 5
    Last Post: 03-20-2010, 08:30 AM
  5. Replies: 5
    Last Post: 02-10-2010, 12:27 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