Results 1 to 17 of 17

Thread: [RESOLVED] Add data to Access DB

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2007
    Posts
    26

    Resolved [RESOLVED] Add data to Access DB

    I have this code to add data to a database i n access, but i´m getting an error on the SQL statement, can anyone of you help me.

    I´m using ADO code to connect to the database.

    Thanks in advance.

    Code:
    Private Sub Command1_Click()
    Dim sSQL As String
    
    sSQL = "INSERT INTO PPE (ID, Name, Last_Name, Qty_Cov, Qty_Boot, Coverall, Boot, Hardhat, Glasses, Vest, Rig, Date) "
    sSQL = sSQL & "VALUES (#" & Text1.Text & "#,'" & Text2.Text & "','" & Text3.Text & "',#" & Text4.Text & "#,#" & Text5.Text & "#,'" & Combo1.Text & "','" & Combo2.Text & "','" & Combo3.Text & "','" & Combo4.Text & "','" & Combo5.Text & "','" & Combo6.Text & "',#" & Text6.Text & "#)"
    
    cn.Execute sSQL
    
    '======================================================
    'CLEAR DATA FROM CONTROL BOXES & SET TODAY´S DATE AGAIN
    '======================================================
    
        Me.Text1.Text = ""
        Me.Text2.Text = ""
        Me.Text3.Text = ""
        Me.Combo1.Text = ""
        Me.Combo2.Text = ""
        Me.Combo3.Text = ""
        Me.Combo4.Text = ""
        Me.Combo5.Text = ""
        Me.Combo6.Text = ""
        Me.Text4.Text = ""
        Me.Text5.Text = ""
        Me.Text6.Text = ""
        Me.Option1.Value = False
        Me.Option2.Value = False
        Me.Option3.Value = False
        Me.Text1.SetFocus
    Me.Text6.Text = Format(Now(), "MMM-DD-YYYY")
    rs.Close
    End Sub

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Add data to Access DB

    Few things:

    - try not to use reserved words like Name, Date to name any of your data objects (tables, fields, etc)
    - use the "#" (number sign) only for Date data type (and you did use for numeric types as well); but even that isn't necessary - you can use single quotes
    - try not to use default control name - give each some meaningfull name instead (txtLastName, txtQtyBoot, etc) or you will get lost at some point...

    Try sql below instead of yours:
    Code:
        sSQL = "INSERT INTO PPE" & vbNewLine
        sSQL = sSQL & "    (" & vbNewLine
        sSQL = sSQL & "     ID," & vbNewLine
        sSQL = sSQL & "     Name," & vbNewLine
        sSQL = sSQL & "     Last_Name," & vbNewLine
        sSQL = sSQL & "     Qty_Cov," & vbNewLine
        sSQL = sSQL & "     Qty_Boot," & vbNewLine
        sSQL = sSQL & "     Coverall," & vbNewLine
        sSQL = sSQL & "     Boot," & vbNewLine
        sSQL = sSQL & "     Hardhat," & vbNewLine
        sSQL = sSQL & "     Glasses," & vbNewLine
        sSQL = sSQL & "     Vest," & vbNewLine
        sSQL = sSQL & "     Rig," & vbNewLine
        sSQL = sSQL & "     Date" & vbNewLine
        sSQL = sSQL & "    ) " & vbNewLine
        sSQL = sSQL & "VALUES" & vbNewLine
        sSQL = sSQL & "    (" & vbNewLine
        sSQL = sSQL & "      " & Text1.Text & "," & vbNewLine
        sSQL = sSQL & "     '" & Text2.Text & "'," & vbNewLine
        sSQL = sSQL & "     '" & Text3.Text & "'," & vbNewLine
        sSQL = sSQL & "      " & Text4.Text & "," & vbNewLine
        sSQL = sSQL & "      " & Text5.Text & "," & vbNewLine
        sSQL = sSQL & "     '" & Combo1.Text & "'," & vbNewLine
        sSQL = sSQL & "     '" & Combo2.Text & "'," & vbNewLine
        sSQL = sSQL & "     '" & Combo3.Text & "'," & vbNewLine
        sSQL = sSQL & "     '" & Combo4.Text & "'," & vbNewLine
        sSQL = sSQL & "     '" & Combo5.Text & "'," & vbNewLine
        sSQL = sSQL & "     '" & Combo6.Text & "'," & vbNewLine
        sSQL = sSQL & "     #" & Text6.Text & "#" & vbNewLine
        sSQL = sSQL & "    )"

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Feb 2007
    Posts
    26

    Re: Add data to Access DB

    Thanks for your reply and advises RhinoBull,

    Nut i´m still getting the same error as before. The cn.Execute sSQL is highlighted and error it´s stil the same

  4. #4

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Feb 2007
    Posts
    26

    Re: Add data to Access DB

    This is the print of the Sql insert statement. Can anyone tell me what´s wrong.

    Thanks in advance.


    INSERT INTO PPE (ID, Name, Last_Name, Qty_Cov, QTY_Boot, Coverall, Boot, Hardhat, Glasses, Vest, Rig, Date) VALUES ('13931955','Lionelo','Espina','2','1','36','37','Green New','Dark','Used','P-103','mar-12-2007')

  6. #6
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Add data to Access DB

    If you'd used sample I posted the "printed" sql would be munch easier to read:
    Code:
    INSERT INTO PPE 
    	(
    	ID, 
    	Name, 
    	Last_Name, 
    	Qty_Cov, 
    	QTY_Boot, 
    	Coverall, 
    	Boot, 
    	Hardhat, 
    	Glasses, 
    	Vest, 
    	Rig, 
    	Date
    	) 
    VALUES (
    	'13931955',
    	'Lionelo',
    	'Espina',
    	'2',
    	'1',
    	'36',
    	'37',
    	'Green New',
    	'Dark',
    	'Used',
    	'P-103',
    	'mar-12-2007'
    	)
    Since you did not use what I suggested I will have to ask you the following:

    - are ID, Qty_Cov, QTY_Boot fields numeric?
    If you answer YES then get rid of quotaions (just like posted for you)

    - are Coverall, Boot fields numeric too?
    If YES the do the same...

  7. #7
    Junior Member
    Join Date
    Jan 2007
    Posts
    29

    Re: Add data to Access DB

    Is it giving a data-type mismatch error?

  8. #8
    Fanatic Member onlyGirl's Avatar
    Join Date
    Sep 2006
    Location
    Houston, TX
    Posts
    743

    Re: Add data to Access DB

    get rid of the quotes wherever the datatype is integer
    Using Visual Studio 2008

    Please mark your thread RESOLVED if you no longer need help.

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Feb 2007
    Posts
    26

    Re: Add data to Access DB

    Sorry RhinoBull,

    I pasted wrong because i was trying something else. Hers´s the print when i use your statement.


    INSERT INTO PPE
    (
    ID,
    Name,
    Last_Name,
    Qty_Cov,
    Qty_Boot,
    Coverall,
    Boot,
    Hardhat,
    Glasses,
    Vest,
    Rig,
    Date
    )
    VALUES
    (
    13931955,
    'Lionelo',
    'Espina',
    1,
    1,
    '36',
    '38',
    'Green New',
    'Dark',
    'Used',
    'P-110',
    #mar-13-2007#
    )

  10. #10

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Feb 2007
    Posts
    26

    Re: Add data to Access DB

    Same problem. Same error. cn.execute sSQL highlighted.

    Run Time error '-2147217900 (80040e14)':

    Sintax error on INSERT TO Statement.

  12. #12

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Feb 2007
    Posts
    26

    Re: Add data to Access DB

    As set on th MS Access Table "PPE":

    Line: Autonumeric.
    Id: Numeric
    Name: Text
    Last_Name: Text
    Rig: Text
    Coverall: Text
    Qty_Cov: Numeric
    Boot: Text
    Qty_Boot: Numeric
    Hardhat: Text
    Glasses: Text
    Vest: Text
    Date: Date
    Closed: Boolean
    cls_date: date

    But the only ones i want to change (at this point) are the mentioned in the statement.

  14. #14
    Fanatic Member onlyGirl's Avatar
    Join Date
    Sep 2006
    Location
    Houston, TX
    Posts
    743

    Re: Add data to Access DB

    Try checking your sql in access using the query builder, that would tell you exactly what is wrong in your sql.
    Using Visual Studio 2008

    Please mark your thread RESOLVED if you no longer need help.

  15. #15
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Add data to Access DB

    The problem is that Name and Date are reserved words (as can bee seen here, explanation in this FAQ article), so you should change those field names to something different.

    If you don't have the option of changing the field names, you can put square brackets around the names in your SQL statement - but this will not eliminate all problems that using reserved words can cause (one example is at the top of the MS article).

  16. #16

    Thread Starter
    Junior Member
    Join Date
    Feb 2007
    Posts
    26

    Re: Add data to Access DB

    Thanks for your help everybody!!!!!, Problem Solved, it was the reserved word issue.

  17. #17

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