Results 1 to 3 of 3
  1. #1
    VAer's Avatar
    VAer is offline Competent Performer
    Windows 10 Access 2013 32bit
    Join Date
    Sep 2016
    Location
    USA
    Posts
    163

    SQL: How to insert record into database table when there is missing value?

    It is about SQL code in Excel VBA. Maybe people in this forum knows SQL better, so I post it here.



    1. The VBA code works fine if there is no missing value. I have tested on one record and multiple records, the code runs fine.
    2. Please note that I write FieldD and FieldF differently, because these two fields are string, the remaining five fields are integer.
    3. I tested the code on a file with one single record, and if only FieldF is missing, the code runs fine and is able to insert the other 6 fields into database table.
    4. I tested the code on a file with one single record, and if only FieldE is missing, the code does not run, see the attached error message.


    Column A will never be missing, so it is fine for the code to check LastRow.

    Code:
    LastRow = WB.Sheets("Test").Cells(Rows.Count, "A").End(xlUp).Row
    
    Cn.Open "Sybase Database Connection String"
        
        For i = 2 To LastRow    
           
               
            AddRecordQuery = "INSERT    INTO Table_name (FieldA, FieldB, FieldC, FieldD, FieldE, FieldF, FieldG)  VALUES (" _
            & WB.Sheets("Test").Cells(i, 1).Value & ", " _
            & WB.Sheets("Test").Cells(i, 2).Value & ", " _
            & WB.Sheets("Test").Cells(i, 3).Value & ", " _
            & Chr(39) & WB.Sheets("Test").Cells(i, 4).Value & Chr(39) & ", " _
            & WB.Sheets("Test").Cells(i, 5).Value & ", " _
            & Chr(39) & WB.Sheets("Test").Cells(i, 6).Value & Chr(39) & ", " _
            & WB.Sheets("Test").Cells(i, 7).Value & ")"
            
            Set TestRs = CreateObject("ADODB.Recordset")
            
            TestRs.Open AddRecordQuery, Cn
            
            Set TestRs = Nothing
        
        
        Next i
    


    Another question: how to test if a record is already in the database table? The database system will generate error message if the identical record is already in the database table, how should I use VBA to check it then I can skip adding that record.

    Thanks.



    Attached Thumbnails Attached Thumbnails FieldE missing.jpg  

  2. #2
    Bulzie is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    Nov 2015
    Posts
    1,511
    Is FieldE part of the Key or does it have an index on it or an option that says it cannot be null?

    On 2nd questions, in Access you can do a DLookup statement to test for a record in a table.

  3. #3
    Micron is offline Very Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    1) Further to Bulzie's question, if the target table allows Null or zero length strings, use the Nz function to coerce the missing data to either. You should be able to choose "" or Null as a valid substitute.
    2) you trap for the error using a message handler and resume with the next line in sequence or do a lookup as suggested, or run a query and check for a recordset count. Which is better would depend on variables such as is the table native to the db or is it ODBC? Are you going across a network? Could there be one lookup or 1,000? etc.
    Last edited by Micron; 03-10-2017 at 02:25 PM. Reason: added info

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

Similar Threads

  1. Replies: 2
    Last Post: 06-06-2016, 03:43 AM
  2. Replies: 2
    Last Post: 03-02-2015, 03:06 PM
  3. VBA - Insert a record from one table to other
    By Praveenevg in forum Access
    Replies: 1
    Last Post: 08-15-2014, 01:50 AM
  4. Insert a new record in a particular position in a table?
    By accessorizer in forum Programming
    Replies: 7
    Last Post: 04-07-2012, 04:21 PM
  5. Insert Record checks table
    By pfarnell in forum Forms
    Replies: 13
    Last Post: 09-05-2010, 10:47 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