Trying to set a date field in a access databse

Peter Otten __peter__ at web.de
Wed Nov 5 16:50:50 EST 2008


tedpottel at gmail.com wrote:

> Hi,
> I cannot get the following code to work
> 
> import win32com.client
> import time
> 
> engine = win32com.client.Dispatch("DAO.DBEngine.36")
> db=engine.OpenDatabase(r"testdate2.mdb")
> access = db.OpenRecordset("select * from test")
> 
> access.AddNew()
> access.Fields("test").value=time.strptime('10:00AM', '%I:%M%p')
> access.Update()
> 
> wherer test is a datetime field,
> How can I do this???????

A first step would be to find out what the expected type of the value
attribute is. For that you can put one record into the test table using
Access with the value 10:00AM for the test field and then run 

# all code untested

import win32com.client

engine = win32com.client.Dispatch("DAO.DBEngine.36")
db = engine.OpenDatabase("testdate2.mdb")
access = db.OpenRecordset("select * from test")

access.MoveFirst()
v = access.Fields("test").value
print type(v), v

If you cannot guess what Access expects from the output of the script, post
it here (in this thread, no need to start yet another one). Don't just
say "didn't work", give tracebacks and the exact code you ran.

Judging from 
http://en.wikibooks.org/wiki/JET_Database/Data_types#Dates_and_times
you will see something like

<type 'float'> 0.416666666667

If that's correct you can modify your script

def time_to_float(h, m, s):
    return (h + m/60.0 + s/3600.0)/24.0 

# ...

access.AddNew()
access.Fields("test").value = time_to_float(10, 0, 0)
access.Update()

Peter



More information about the Python-list mailing list