CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Aug 2000
    Posts
    49

    Inserting Null Values Through INSERT INTO Statement

    I've got this problem, that I can't get around. I'm sending a null value through an SQL "INSERT INTO" statement using ADO, but the database does not accept the value in either date or numeric data type field. Even it's allow null is set to true. I've tested this problem with SQL Server and Microsoft Access. is there any solution for this problem, I need to do it with Insert into statement because I'm using Data Environment. (The field is optional, some
    time I have to send values and some time not. So it is necessary to mention the name of field in the query, and obviously I can't make two queries for the one optional parameter because what if I have 2 or more optional parameters???)
    This problem is also with command object.
    I dont want to use ..

    Rs.AddNew
    Rs.fields("m_ID") = null
    Rs.Update




    I've tried everything, and it seems impossible through vb. You may accept it as a challenge.

    Remember.. INSERT INTO STATEMENT .. NULL VALUES.


  2. #2
    Join Date
    Aug 2000
    Location
    England
    Posts
    185

    Re: Inserting Null Values Through INSERT INTO Statement

    have you tried passing it as a literal, i.e

    Rs.fields("m_ID") = 'NULL'

    Andrew




  3. #3
    Join Date
    Sep 2001
    Location
    Montreal Canada
    Posts
    1,080

    Re: Inserting Null Values Through INSERT INTO Statement

    What about doing this..

    If there are multiple fields in your Database, then insert only those that are needed ( example the primary key )

    rs.addnew
    rs.Fields("PrimaryKey") = "Key"
    rs.update

    Null will be in the values that aren't needed.

    Nic

    Nicolas Bohemier

  4. #4
    Join Date
    Aug 2000
    Posts
    49

    Re: Inserting Null Values Through INSERT INTO Statement

    Thanx Andrew, but as I already said, I don't want to use
    the following

    Rs.AddNew
    Rs.fields("Fieldname") = 'Value'
    Rs.Update



    Because I'm using "INSERT INTO STATEMENT".


  5. #5
    Join Date
    Aug 2000
    Location
    England
    Posts
    185

    Re: Inserting Null Values Through INSERT INTO Statement

    Sorry, misunderstood that.

    You could actually use that in a insert statement

    strSql = "INSERT INTO TABLE (<fields&gt"
    strSQL = strSQl & " VALUES (<value_1>, 'NULL', <value_3>,..,<value_n&gt
    rs.Sql = strSql
    rs.Requery

    Andrew



  6. #6
    Join Date
    Jan 2000
    Location
    Olen, Belgium
    Posts
    2,477

    Re: Inserting Null Values Through INSERT INTO Statement


    INSERT INTO SomeTable (SomeField, SomeOtherField)
    VALUES ('SomeValue',null)



    will insert Null value in second field

    Tom Cannaerts
    [email protected]

    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning -- Rich Cook
    Tom Cannaerts
    email: [email protected]
    www.tom.be (dutch site)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured