Trying to set a date field in a access databse

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tedpottel@gmail.com

    Trying to set a date field in a access databse

    Hi,
    I cannot get the following code to work

    import win32com.client
    import time

    engine = win32com.client .Dispatch("DAO. DBEngine.36")
    db=engine.OpenD atabase(r"testd ate2.mdb")
    access = db.OpenRecordse t("select * from test")

    access.AddNew()
    access.Fields(" test").value=ti me.strptime('10 :00AM', '%I:%M%p')
    access.Update()

    wherer test is a datetime field,
    How can I do this???????
    -Ted

  • Peter Otten

    #2
    Re: Trying to set a date field in a access databse

    tedpottel@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.OpenD atabase(r"testd ate2.mdb")
    access = db.OpenRecordse t("select * from test")
    >
    access.AddNew()
    access.Fields(" test").value=ti me.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.OpenData base("testdate2 .mdb")
    access = db.OpenRecordse t("select * from test")

    access.MoveFirs t()
    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

    you will see something like

    <type 'float'0.416666 666667

    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(1 0, 0, 0)
    access.Update()

    Peter

    Comment

    • Roger Upole

      #3
      Re: Trying to set a date field in a access databse

      You should be able to pass a PyTime or datetime.dateti me object.

      Roger

      <tedpottel@gmai l.comwrote in message
      news:12d9f626-9e5c-484a-94e4-120122a41214@40 g2000prx.google groups.com...
      Hi,
      I cannot get the following code to work
      >
      import win32com.client
      import time
      >
      engine = win32com.client .Dispatch("DAO. DBEngine.36")
      db=engine.OpenD atabase(r"testd ate2.mdb")
      access = db.OpenRecordse t("select * from test")
      >
      access.AddNew()
      access.Fields(" test").value=ti me.strptime('10 :00AM', '%I:%M%p')
      access.Update()
      >
      wherer test is a datetime field,
      How can I do this???????
      -Ted
      >
      --

      >


      Comment

      Working...