I keep getting a syntax error

JASONW10

Board Regular
Joined
Mar 11, 2005
Messages
231
I keep getting a syntax error on This lline Call rs.Open(strsql, strConnectionString)
for my strsql string My string equals "INSERT INTO Usage (eDates) Values (#05/06/2011#);"
My access Data base Edates is set to be a date/time value. Does anyone know what I am doing wrong?

public counta as integer
Sub Insertinga()


'stay
counta = 4
While Range("a" & counta) <> ""
Application.Run ("senddata")
counta = counta + 1
Wend


End Sub
Sub senddata()
Dim rs As Recordset
Set rs = New Recordset


Dim n As Date
Dim edates As Date
Dim etimes As Date
Dim Peak As String
Dim KWh As Double
Dim KWd As Double
Dim KWa As Double
Dim KVar As Double
Dim PF As Double


edates = Range("h" & counta)
etimes = Range("i" & counta)
emonths = Range("j" & counta)
Peak = Range("b" & counta)
KWh = Range("c" & counta)
KWd = Range("d" & counta)
KWa = Range("e" & counta)
KVar = Range("f" & counta)
PF = Range("g" & counta)






'strsql = "INSERT INTO Usage (Dates, Times, Peak, KWh, Kwd, KVa, KVar, PF, Months) VALUES (" & "#" & dates & "#, #" & times & "#, " & Peak & ", " & KWh & ", " & KWd & ", " & KVa & ", " & KVar & ", " & PF & ", #" & edates & "#);"
strsql = "INSERT INTO Usage (eDates) Values (#" & edates & "#);"
'Adjust Filepath to Access in this string as needed
Const strConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & _
"\\pvnt2\home\Manufacturing\JWallace\Accessdatabases\Energyusage.mdb" & _
";Persist Security Info=False"
Call rs.Open(strsql, strConnectionString)
'rs.MoveFirst 'Header
'rs.MoveNext 'Actual first record

If (rs.State And ObjectStateEnum.adStateOpen) Then rs.Close
If Not rs Is Nothing Then Set rs = Nothing


End Sub
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
You don't Call rs.Open, you just rs.Open.

rs.Open isn't a procedure: .Open is a method of the object rs.
 
Upvote 0
I tried it without the CALL but then it reds out the intire line. Furthermore, I used the call method many times before. Therefore, I think there is something else I am doing wrong, for it only tells my that I have a syntax error in my slq statment.
 
Upvote 0
Try to execute that exact same query directly in your Access database. Access will tell you if there is an error in the query, Excel won't...
 
Upvote 0
As well as removing Call remove the parentheses.
 
Upvote 0
I used the call method many times before.
Good grief, you're right - that's a new on on me! My apologies.

Next step is to use Debug.Print to display the contents of strsql. If you've obtained this value from a worksheet cell, it may well contain an Excel date serial, rather than a string of characters representing a date which SQL understands and expects.

What's in strsql? If it's something like 40,500, you'll need to format it explicitly as a date:-
Code:
strsql = "INSERT INTO Usage (eDates) Values (#" & [COLOR=red][B]format([/B][/COLOR]edates[COLOR=red][B],"dd-mmm-yyyy)[/B][/COLOR] & "#);"

(I always use dd-mmm-yyyy format because then I'm sure which way round the day and the month are, but you could also use mm/dd/yyyy.)
 
Upvote 0
I figured it out USage is a resive word in sql, so I changed it to EUsage for my table name. now everything works fine. Thanks for all your help
 
Upvote 0

Forum statistics

Threads
1,224,597
Messages
6,179,808
Members
452,944
Latest member
2558216095

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top