|
-
Mar 11th, 2007, 06:59 PM
#1
Thread Starter
Junior Member
[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
-
Mar 11th, 2007, 07:46 PM
#2
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 & " )"
-
Mar 11th, 2007, 08:15 PM
#3
Thread Starter
Junior Member
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
-
Mar 11th, 2007, 08:53 PM
#4
Re: Add data to Access DB
Print actual statement to debug before you execute and see what's wrong.
If any of string type values contain single quote character you will have to replace it with two single quotes:
Replace(text1.text, "'", "''")
-
Mar 12th, 2007, 07:34 PM
#5
Thread Starter
Junior Member
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')
-
Mar 12th, 2007, 09:16 PM
#6
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...
-
Mar 13th, 2007, 10:20 AM
#7
Junior Member
Re: Add data to Access DB
Is it giving a data-type mismatch error?
-
Mar 13th, 2007, 12:17 PM
#8
Fanatic Member
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.
-
Mar 13th, 2007, 07:43 PM
#9
Thread Starter
Junior Member
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#
)
-
Mar 13th, 2007, 08:01 PM
#10
Re: Add data to Access DB
So, what's the problem now?
-
Mar 13th, 2007, 08:17 PM
#11
Thread Starter
Junior Member
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.
-
Mar 13th, 2007, 08:32 PM
#12
Re: Add data to Access DB
Can you poste data fields types (for each field)?
-
Mar 13th, 2007, 08:41 PM
#13
Thread Starter
Junior Member
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.
-
Mar 14th, 2007, 09:32 AM
#14
Fanatic Member
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.
-
Mar 14th, 2007, 10:43 AM
#15
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).
-
Mar 14th, 2007, 07:27 PM
#16
Thread Starter
Junior Member
Re: Add data to Access DB
Thanks for your help everybody!!!!!, Problem Solved, it was the reserved word issue.
-
Mar 14th, 2007, 08:32 PM
#17
Re: Add data to Access DB
 Originally Posted by lyilaly
...Problem Solved, it was the reserved word issue.
Which was pointed out 3 days ago in post #2.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|