question

EuniceH avatar image
EuniceH asked

insert time into a time(0) data type

I have a MS Access 2000 frontend and a SQL Server 2008 Express front end. I'm trying to insert a new record, and the time(0) data type always gives me problems. If I type this in directly into SQL Server Mgmt Studio query, it works. But the same thing doesn't work in Access. INSERT INTO [GCTest05].[dbo].[tblBAIIDActivity] (... ,[ActivityTime] ... ) VALUES (...,'8:00:00 AM',...) The [ActivityTime] column is a time(0) data type.
sql-server-2008microsoft-accesstime
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
sqlnubi avatar image
sqlnubi answered
Access must not use that function. Could you not use a datetime column and chose getdate()? I don't know much about access.
8 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Oleg avatar image Oleg commented ·
This is correct. Access does not have an equivalent of **time** data type. However, SQL Server will happily accept a datetime input from your Access query and discard the date part when inserting the **time** value. For example,
declare @t time(0);
set @t = getdate();
select @t;
works just fine Please try formatting your query such that the time value looks like a well formatted datetime (or use can use the **now** function).
1 Like 1 ·
Oleg avatar image Oleg commented ·
@EuniceH Since implicit conversion from time to varchar is allowed, please try to use string but do not specify the AM/Pm part, and format the string as hh:mm:ss meaning include leading zero if needed for any part that is less than 10. For example, to represent 5 past 8 in the morning use "08:05:00" and to represent quarter till 5 in the afternoon use "16:45:00"
1 Like 1 ·
EuniceH avatar image EuniceH commented ·
I've tried this and always get error 3155 : ODBC--insert on a linked table 'tblBAIIDActivity' failed. I've also used the ActivityTime and dteTime as a Date and that doesn't work either, same error. Here's a portion of my code. If I comment out the "!ActivityTime = dteTime" then the insert works. Public Type BAIIDEvent ... ActivityTime As String ... End Type Public Sub AddBAIIDEvent(udtBAIIDEvent As BAIIDEvent, Optional fSuccessful As Boolean) Dim rst As DAO.Recordset Dim dteTime As String ... dteDate = udtBAIIDEvent.ActivityDate dteTime = udtBAIIDEvent.ActivityTime ... Set rst = gdbTables.OpenRecordset("select * from tblBAIIDActivity where BAIIDID = " & udtBAIIDEvent.BAIIDID & " AND ActivityDate = #" & dteDate & "# AND BAIIDEventID = " & lngEventID, dbOpenDynaset, dbSeeChanges) With rst If .BOF And .EOF Then .AddNew ... !ActivityTime = dteTime .Update ... End If End With
0 Likes 0 ·
EuniceH avatar image EuniceH commented ·
plus I forgot to add that I tried assigning !ActivityTime = Now() and that gave the same error.
0 Likes 0 ·
EuniceH avatar image EuniceH commented ·
that worked! I'll have to mess around with it, but at last there is a way to insert! Thanks much Oleg!
0 Likes 0 ·
Oleg avatar image Oleg commented ·
@EuniceH From reading [this article in BOL][1] it appears that **hh:mm:ss** format should work and both **hh:mm:ssAM** and **hh:mm:ss AM** should work as well. This means that including AM or PM with or without space is allowed though not necessary in favour of military format and should work provided that every portion of time (hour, minute, second) is formatted as 2 digits (left padded with zero if needed). This appears to be a critical requirement. In other words, if you original query in question would have '08:00:00 AM' instead of '8:00:00 AM' then it would just work :) Please drop a line here to confirm, this could be very a useful knowledge to other users. [1]: http://msdn.microsoft.com/en-us/library/bb677243.aspx
0 Likes 0 ·
EuniceH avatar image EuniceH commented ·
for me "14:45:25" worked, but "08:00:00 AM" did not work.
0 Likes 0 ·
Oleg avatar image Oleg commented ·
@EuniceH This is very good to know, thank you. At least the **hh:mm:ss** works. I will have to remember this one.
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.