Results 1 to 6 of 6
  1. #1
    shabbaranks is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Oct 2011
    Posts
    162

    Arguement not optional - INSERT INTO sql command

    Hi guys,



    Im all a kuffufel trying to get my form data into a table with my sql. Am I right in thinking insert into will append the form values to the selected table - or am I doing it incorrectly?

    Code:
    Private Sub AddCont_btn_Click()
        Dim Dbtelnum As Double
        Dim strEmailAdd As String
        Dim mySQL As String
        Dim strFirstname As String
        Dim strLastName As String
        Dim lngCompanyID As Long
        
        lngCompanyID = Nz(DLookup("ID", "CompanyInfo_tbl", "[Company]= '" & [NewContact_Combo] & "'"), 0)
        strFirstname = Me.FirstName_txtbx
        strLastName = Me.LastName_txtbx
            
        If Me.telnumb_txtbx = "" Or IsNull(Forms![NewContact_Frm]![telnumb_txtbx]) Then
        Dbtelnum = "0"
        Me.telnumb_txtbx = Dbtelnum
        End If
        
        If Me.emailadd_txtbx = "" Or IsNull(Forms![NewContact_Frm]![emailadd_txtbx]) Then
        strEmailAdd = "unknown"
        Me.emailadd_txtbx = strEmailAdd
        End If
        
        If Me.FirstName_txtbx = "" Or IsNull(Forms![NewContact_Frm]![FirstName_txtbx]) Then
            MsgBox "Please Add The Persons First Name", vbInformation
            Me.FirstName_txtbx.SetFocus
        Exit Sub
    End If
            
    If Me.LastName_txtbx = "" Or IsNull(Forms![NewContact_Frm]![LastName_txtbx]) Then
            MsgBox "Please Add The Persons Last Name", vbInformation
            Me.LastName_txtbx.SetFocus
        Exit Sub
    End If
    If Me.emailadd_txtbx = "" Or IsNull(Forms![NewContact_Frm]![emailadd_txtbx]) Then
    strEmailAdd = "unknown"
    End If
    DoCmd.SetWarnings False
    DoCmd.RunSQL = "INSERT INTO (CompanyContact_tbl FirstName, Surname, TelNumber, CompanyID, EmailAddress " & _
    "Values '" & strFirstname & "', '" & strLastName & "', Dbtelnum, lngCompanyID, '" & strEmailAdd & "' )"
    DoCmd.SetWarnings True
    Me!NewContact_Frm.Form.Requery
    Clearfield
    End Sub
    Thanks for any help

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,530
    The correct syntax:

    INSERT INTO TableName(Field1, Field2,...)
    VALUES(Value1, Value2,...)

    you also need to concatenate each value, with the appropriate delimiters. This may help:

    http://www.baldyweb.com/BuildSQL.htm
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    shabbaranks is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Oct 2011
    Posts
    162
    Thanks helped me to no extent

    Code:
    DoCmd.RunSQL "INSERT INTO CompanyContact_tbl ([FirstName], [SURNAME], [TelNumber], [CompanyID], [EmailAddress]) VALUES ('" & strFirstname & "', '" & strLastName & "', '" & Dbtelnum & "', " & lngCompanyID & ", '" & strEmailAdd & "')"

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,530
    Happy to not help then.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    shabbaranks is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Oct 2011
    Posts
    162
    haha... Could you explain to me how you should seperate sql into lines properly please? Ive never really understood how you should do it but I would say (based on the error the code is red) that I have written it incorrectly.

    Code:
        DoCmd.RunSQL "INSERT INTO CompanyContact_tbl ([Company], [CompanyID], [Adress1], [Adress2], [Adress3], [Adress4] " & _
        "[Adress5], [Adress6], [PostCode], [MainPhoneNumber], [Website] " & _
        "VALUES '" & strCompanyName & "', " & lngNewID & ", '" & strADDL1 & "', '" & strADDL2 & "', '" & strADDL3 & "', '" & strADDL4 & "', '" & strADDL5 & "' & _
        " & strADDL6 & "', '" & strPostCode & "', " & DMainNum & ", '" & strWebSite & "')"
    Thanks

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,530
    For starters, breaking to the last line you've got a line break in the middle of a string. Try keeping literal strings intact.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. error argument not optional
    By slimjen in forum Programming
    Replies: 10
    Last Post: 03-20-2013, 09:31 PM
  2. Replies: 2
    Last Post: 10-23-2012, 12:18 PM
  3. Replies: 5
    Last Post: 03-04-2012, 01:21 PM
  4. Yes/No Box based on arguement
    By wfeandsig in forum Database Design
    Replies: 4
    Last Post: 12-10-2011, 07:42 PM
  5. multiple optional criteria
    By TheShabz in forum Programming
    Replies: 7
    Last Post: 07-05-2011, 05:13 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