views:

155

answers:

5

i cannot see why this would be an issue. i use the msgbox to display my strSQL, but it looks perfectly fine.

Dim strSQL As String
strSQL = "INSERT INTO Jobs (Date, RefNo, ProjectID, PManager, PDesc, PONo, Src_Qty, WC_Qty, Rate, Total, Note, Company) "
strSQL = strSQL & "VALUES (" & JobDate.Value & ", '" _
                            & Text41.Value & "', '" _
                            & ProjectID.Value & "', '" _
                            & PManager.Value & "', '" _
                            & PDesc.Value & "', " _
                            & Text43.Value & ", " _
                            & Src_Qty.Value & ", " _
                            & WC_Qty.Value & ", " _
                            & newRate.Value & ", " _
                            & ProjTotal.Value & ", '" _
                            & Text38.Value & "', '" _
                            & newCompany.Value _
                            & "');"
MsgBox (strSQL)
DoCmd.RunSQL strSQL
+7  A: 

If I remember right you will need to wrap your date with the pound for Access.

"VALUES (#" & JobDate.Value & "#, '" _

And I don't think you need the semicolon at the end....

klabranche
+1 for likeliest solution without any error message or clearer OP
Jonathan Fingland
I agree Jonathan. :)
klabranche
This link would seem to confirm your memory: http://bytes.com/topic/access/insights/575420-literal-datetimes-their-delimiters
Daniel Pryden
semicolon at the end is optional in Access
Tony Toews
+3  A: 

Add a line:

Debug.Print strSQL

after

MsgBox (strSQL)

Go to the Immediate Window (Ctrl+g) and copy the completed SQL statement. Then create a new query, switch to SQL View, and paste in the SQL statement. Figure out why the statement fails. I think klabranche is on the money if the "Date" field in your Jobs table is Date/Time data type. If you're unable to identify the problem, edit your question to show the failing SQL statement, and tell us the data types of the Jobs fields.

Also, your Jobs table includes 3 fields whose names are reserved words: Date; Rate; and Note. (See http://allenbrowne.com/AppIssueBadWord.html) If you keep those field names, at least surround them with square brackets to avoid confusing Access:

strSQL = "INSERT INTO Jobs ([Date], RefNo, ...
HansUp
+1 Good catch on the reserved words HansUp
klabranche
oh wow, youre a genius. that solved it. thank you!
aZn137
+2  A: 

In addition to using # delimeters around the date as mentioned by klabranche you should also use an unambiguous date format. Either use the ISO standard of yyyy-mm-dd by using format(Jobdate,"yyyy-mm-dd") or see Return Dates in US #mm/dd/yyyy# format

Double check you are using quotes around test fields and no quotes around numeric fields. ProjectID might be numeric or might be text.

I would rename the text43 and test48 fields on the form to be clearer as to what they are. Assuming this is a bound form you can set them to the same name as the field.

You don't need to use the .Value property.

The problem with DoCmd.RunSQL is that it ignores any errors. Either of the following will display any error messages received by the query. If using DAO, use Currentdb.Execute strSQL,dbfailonerror.. For ADO use CurrentProject.Connection.Execute strCommand, lngRecordsAffected, adCmdText You can then remove the docmd.setwarnings lines if any.

If you're going to use docmd.setwarnings make very sure you put the True statement in any error handling code as well. Otherwise weird things may happen later on especially while you are working on the app. For example you will no longer get the "Do you wish to save your changes" message if you close an object. This may mean that unwanted changes, deletions or additions will be saved to your MDB.

Also performance can be significantly different between the two methods. One posting stated currentdb.execute took two seconds while docmd.runsql took eight seconds. As always YMMV.

HansUp has an excellen point on using reserved field names. Visit Tony's Table and Field Naming Conventions

Tony Toews
A: 

I agree with klabranche that the most likely culprit is you need to put hash marks ("#") around the date value. However as an addendum to that thought, if you use CurrentDb.Execute instead of DoCmd.RunSQL you will be given an informative error message what will tell you specifically is wrong. This will make debugging much easier. Hope that helps!

Oorang
A: 

after changing my field name, this is what my strSQL says:

INSERT INTO Jobs (JobDate, RefNo, ProjectID, PManager, PDesc, PONo, Src_Qty, WC_Qty, JobRate, JobTotal, Note, Company) VALUES (#2009-09-20#, '1', '3', 'Jean Jou', '4', 2, 5, 6, 7, 42, '9', 'Magnus')

i have checked my text fields multiple times, they are correct.

iam getting "Run-time error: 3134, Syntax error in INSERT INTO statement", regardless of Do.Cmd or CurrentDb.Execute

thanks guys.

aZn137
Are RefNo and ProjectID text data type fields? Is any of the fields an autonumber data type?
HansUp
Following up on HansUp comments. Double check that all fields with quotes on the values are indeed text fields in the table and the number values are indeed number fields in the table.
Tony Toews