help with sql inserting

JASONW10

Board Regular
Joined
Mar 11, 2005
Messages
231
I am currently trying to write a script that will take a table out of excel and place it into access automatically, by a press of a button. So far everything works except when I try to send data to my Date Field and my Time Field. I have these both set in excel as text and in access as text as well. my field names in access are Date and Time. Here is my script any and all help would be grateful

Sub Inserting()
Dim rs As Recordset
Set rs = New Recordset
Dim n As Date
Dim ID As String
Dim Genpart As String
Dim Amount As Integer
Dim Location As String
Dim Datea As String
Dim Timea As String
Dim Typea As String
Count = 1
While Worksheets("sheet1").Range("a" & Count) <> ""
ID = Worksheets("sheet1").Range("a" & Count)
Genpart = Worksheets("sheet1").Range("b" & Count)
Amount = Worksheets("sheet1").Range("c" & Count)
Location = Worksheets("sheet1").Range("d" & Count)
Datea = Worksheets("sheet1").Range("e" & Count)
Timea = Worksheets("sheet1").Range("f" & Count)
Typea = Worksheets("sheet1").Range("g" & Count)
'This is my main strsql string
'strSQL = "INSERT INTO Cyclecount (Id, Genpart, Amount, Location, Date, Time, Type) VALUES (" & "'" & ID & "'," & "'" & Genpart & "'," & Amount & "," & "'" & Location & "'," & "'" & Datea & "'," & "'" & Timea & "'" & ", '" & Typea & "'" & ");"

'This is my test strsql string
strSQL = "INSERT INTO cyclecount (Date) VALUES (" & "'" & Datea & "');"

'all this does is send it to my spread sheet to show what I am trying to say in sql
e = "INSERT INTO cyclecount (Date) VALUES (" & "'" & Datea & "');"
Range("i1") = e





'Adjust Filepath to Access in this string as needed
Const strConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & _
"L:\Manufacturing\JWallace\cyclecount\Cyclecount.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
Count = Count + 1
Wend
End Sub
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
I'm having trouble updating a field named "Date" - that's a reserved word in VB...maybe the cause of the problem?


see if this works:
INSERT INTO cyclecount ([Date])
 
Upvote 0
I will give it a try, but I did solve it by naming date to dates and time to times. I think vb was confusing it with one of it own objects who knows.
thanks
 
Upvote 0

Forum statistics

Threads
1,224,598
Messages
6,179,818
Members
452,946
Latest member
JoseDavid

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