Results 1 to 5 of 5
  1. #1
    bigal.nz is offline Novice
    Windows 7 64bit Access 2013 32bit
    Join Date
    Jun 2016
    Posts
    11

    Lightbulb INSERT SQL statement from Form

    Hello,



    I have 6 combo boxes on a form and want to construct a insert statement from the values on the boxes, via VBA. This works fine as long as all the boxes have a value selected, but if some boxes are empty I understandably get a error.

    I would like a elegant way to handle nulls where no value was selected in the combo box.

    Here is the code:

    Code:
    Private Sub cmd_submit_Click()
    ' *** A FUCNTION ON CLICK TO UPDATE THE MTG TABLE WITH PERSONS PRESENT - ON SUBMIT
    
    
    Dim strSQL As String
    Dim rs As Recordset
    Dim db As DAO.Database
    Dim contr As Control
    Set db = CurrentDb
    
    
    
    
    ' *** GET THE ID NUMBERS FROM THE HIDDEN COLUMS IN DROPDOWN LIST
    str_facilitator_ID = Me.combo_facilitator.Column(1)
    str_admin_ID = Me.combo_admin.Column(1)
    str_HB_ID = Me.combo_HB.Column(1)
    str_YF_ID = Me.combo_YF.Column(1)
    str_corr_ID = Me.combo_corr.Column(1)
    str_port_ID = Me.combo_port.Column(1)
    ' CONSTRUCT A STRING OF ID NUMBERS TO PUT INTO THE INSERT SQL STATEMENT
    str_IDs = str_corr_ID & "," & str_HB_ID & "," & str_YF_ID & "," & str_facilitator_ID & "," & str_admin_ID & "," & str_port_ID
    
    
    
    
    ' *** INSERT THE ID VALUES OF PEOPLE PRESENT FROM THE FORM AND INSERT INTO MTG TABLE
    strSQL = "INSERT INTO tbl_mtg (corr_person_ID, HB_person_ID, yf_person_ID, facilitator_ID, admin_ID, port_ID, Comments) VALUES (" & str_IDs & ",'" & txt_comments & "');"
    Debug.Print (strSQL)
    ' *** EXECUTE THE UPDATE
    db.Execute strSQL, dbFailOnError
    
    
    ' *** TIDY UP
    strSQL = ""
    db.Close
    Set db = Nothing
    
    
    End Sub


    -Al

  2. #2
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    My suggestion would be to use 0 for the ID where none was selected. All you need to do is change your 6 str... statements
    from this:
    Me.combo_facilitator.Column(1)

    to this:

    nz(Me.combo_facilitator.Column(1),0)


    The Nz function is used to "convert" Nulls to meaningful values. The second parameter specifies the value to return if the first parameter
    is Null, otherwise it returns the value in the first parameter.

    If you don't want to use zeros to replace nulls, then your code will be a lot trickier, since you will have
    to change the Select statement field lists and value lists to account for the Nulls.

  3. #3
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    An alternative is using the AddNew method of a recordset, which wouldn't care about the Nulls. The Nz() method John mentioned is simpler if appropriate.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  4. #4
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,407
    If you can't tolerate zeros in the table, you can use something like the code below to build the query. The query will not include verbiage for null fields.

    Code:
    Dim strSQL As String
    Dim strInsert As String
    Dim strValue As String
    strInsert = "INSERT INTO tbl_mtg ("
    strValue = " Values ("
    If Len(txt_comments & vbNullString) = 0 Then
        txt_comments = " "   'space
    End If
    If Not IsNull(.combo_corr.Column(1)) Then
        strInsert = strInsert & "str_corr_ID, "
        strValue = strValue & "corr_person_ID, "
    End If
    If Not IsNull(Me.combo_HB.Column(1)) Then
        strInsert = strInsert & "str_HB_ID, "
        strValue = strValue & "HB_person_ID, "
    End If
    If Not IsNull(Me.combo_YF.Column(1)) Then
        strInsert = strInsert & "str_YF_ID, "
        strValue = strValue & "yf_person_ID, "
    End If
    If Not IsNull(Me.combo_facilitator.Column(1)) Then
        strInsert = strInsert & "str_facilitator_ID, "
        strValue = strValue & "facilitator_ID, "
    End If
    If Not IsNull(Me.combo_admin.Column(1)) Then
        strInsert = strInsert & "str_admin_ID, "
        strValue = strValue & "admin_ID, "
    End If
    If Not IsNull(Me.combo_port.Column(1)) Then
        strInsert = strInsert & "str_port_id, "
        strValue = strValue & "Port_ID, "
    End If
    strInsert = strInsert & "comments) "
    strValue = strValue & "'" & txt_comments & "');"
    strSQL = strInsert & strValue
    Debug.Print strSQL

  5. #5
    bigal.nz is offline Novice
    Windows 7 64bit Access 2013 32bit
    Join Date
    Jun 2016
    Posts
    11
    Quote Originally Posted by John_G View Post
    My suggestion would be to use 0 for the ID where none was selected. All you need to do is change your 6 str... statements
    from this:
    Me.combo_facilitator.Column(1)

    to this:

    nz(Me.combo_facilitator.Column(1),0)


    The Nz function is used to "convert" Nulls to meaningful values. The second parameter specifies the value to return if the first parameter
    is Null, otherwise it returns the value in the first parameter.

    If you don't want to use zeros to replace nulls, then your code will be a lot trickier, since you will have
    to change the Select statement field lists and value lists to account for the Nulls.
    Thanks for that - I didnt know about the "nz" function. Very handy.

    I did also come up with :

    Code:
    Private Sub cmd_submit_Click()
    ' *** A FUCNTION ON CLICK TO UPDATE THE MEETING TABLE WITH PERSONS PRESENT - ON SUBMIT
    
    
    Dim strSQL As String
    Dim rs As Recordset
    Dim db As DAO.Database
    Dim contr As Control
    Set db = CurrentDb
    
    
    'strtoday = Date
    'Debug.Print (strtoday)
    
    
    
    
    ' *** GET THE ID NUMBERS FROM THE HIDDEN COLUMS IN DROPDOWN LIST
    For Each contr In Me.Controls
        If TypeName(contr) = "ComboBox" Then
            If contr.Column(1) <> 0 Then
                strValues = contr.Column(1) & "," & strValues
                Else: strValues = 0 & "," & strValues
            End If
        End If
    Next
    
    
    Debug.Print (strValues)
    Which does the same as what "nz"

    I now need to store these values against a corresponding fieldname in the table.

    I could through the field names and create a list of field names paired with values?

    Cheers

    -Al

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

Similar Threads

  1. INSERT INTO statement
    By berderder in forum Programming
    Replies: 4
    Last Post: 06-03-2016, 06:00 PM
  2. Insert into with a WHERE statement
    By hazeleyre23 in forum Access
    Replies: 10
    Last Post: 04-06-2016, 08:28 AM
  3. INSERT statement
    By GraeagleBill in forum Programming
    Replies: 2
    Last Post: 03-29-2013, 12:53 PM
  4. Insert Into statement
    By TimMoffy in forum Programming
    Replies: 7
    Last Post: 07-13-2012, 07:10 AM
  5. Insert statement
    By crowegreg in forum Programming
    Replies: 2
    Last Post: 08-19-2011, 02:20 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