Results 1 to 8 of 8
  1. #1
    injanib is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Jan 2011
    Posts
    67

    Writing Time Value into a date field with update query


    Hi

    I have an update query that writes time value from an unbound field into a date data type field of a table. I don't know how to write the statement so the query knows it is only writing the time portion of the date data type into a date field.

    Here is my SQL statement:

    strSQL = ("UPDATE tblClass SET [StartHour]=" & Me.txtStartHour & " WHERE [ID]=" & Me.txtClassID & "")

    [StartHour] is a date field in the table, and txtStartHour has an input mask of (HH:MM AM/PM).

    Thanks in advance.

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    For a date/time field you need the # delimiter:

    strSQL = ("UPDATE tblClass SET [StartHour]=#" & Me.txtStartHour & "# WHERE [ID]=" & Me.txtClassID)
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    injanib is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Jan 2011
    Posts
    67
    Thanks for the response. Well, if I don't use the # delimiter as in my original post, I get:
    Run-time error '3075' - Syntax error (missing operator) in query expression '09:00:00'

    If I do use it the way you have indicated then I get:
    Run-time error '3061 - Too few parameters. Expected 2.

    I have also tried:

    strSQL = ("UPDATE tblClass SET (TimeValue([StartHour]=#" & Me.txtStartHour & "#)) WHERE [ID]=" & Me.txtClassID)

    That gave me:

    Run-time error '3144'
    Syntax error in UPDATE statement.

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    That syntax is certainly wrong, and you wouldn't want the TimeValue() function used like that (perhaps on the value, not on the field). The parameters error usually means something is spelled wrong (table or field name). If not, add this line after the SQL is built but before it's executed:

    Debug.Print strSQL

    which will print the finished SQL out to the VBA Immediate window. You can see if you spot the error there, and if not copy/paste the SQL into a new query and try to run it. You'll often get a more descriptive error there. If you don't find the problem, post the SQL here.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    injanib is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Jan 2011
    Posts
    67
    In addition to this date field, my SQL statement updates a number of text and number fields in the table as well. I only mentioned this field for simplicity purposes and because this is what is causing the problem. If I remove this field from the SQL Statement, it executes fine and updates the other fields with the right data, but as soon as I include this field in the statement, which is the only field with date/time data type, I run into this issue.

    If I include the # delimeter the debug.print gives:
    UPDATE tblClass SET [ClassID] = 5, [InstructorID]= T00001,[Capacity]=25, [StartHour]=#09:00:00#

    If I remove it, it gives:
    UPDATE tblClass SET [ClassID] = 5, [InstructorID]= T00001,[Capacity]=25, [StartHour]=09:00:00,

    I also tried to change the statement as follows:
    strSQL = ("UPDATE tblClass SET [StartHour]=#" & format (Me.txtStartHour, "HH:MM AM/PM") & "# WHERE [ID]=" & Me.txtClassID)

    and the debug.print gave me:
    UPDATE tblClass SET [ClassID] = 5, [InstructorID]= T00001,[Capacity]=25, [StartHour]=#09:00 AM#,

    and that without the # signs if I remove the # delimeter.

    As I mentioned, this only happens when I include this [StartHour] field into the statement.
    number fields and text fields work just fine.

    Thanks for your help.

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    Can you confirm the data type of StartHour in the table? Can you post the db?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    injanib is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Jan 2011
    Posts
    67
    Oh boy! I am such an idiot. I have worked as a help desk analyst for an IT company long enough to know that the problem almost lies between the keyboard and the user.

    So I decided to take it one field at a time. As it turns out, the problem was never the date/time field. It was a text field that I have been treating as a number field, and have been missing to plug it in the statement when testing without the date/time field.

    I am sorry to have wasted your time. I am all set now. Thanks allot.

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    No problem, glad you got it sorted out.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Access 2007 Date/Time Field
    By cdh in forum Access
    Replies: 3
    Last Post: 05-12-2011, 07:11 AM
  2. Access Subquery Max Date/Time field
    By ruci1225 in forum Access
    Replies: 1
    Last Post: 04-08-2011, 07:33 AM
  3. NEW sql update HELP :( (time field problem)
    By ducthang88 in forum Programming
    Replies: 1
    Last Post: 12-05-2010, 12:03 AM
  4. Date/Time entry field on a form
    By Ted C in forum Forms
    Replies: 1
    Last Post: 06-22-2010, 12:28 PM
  5. date/time field
    By James890 in forum Database Design
    Replies: 1
    Last Post: 04-18-2007, 07:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums