Results 1 to 6 of 6
  1. #1
    JoeFootball91 is offline Novice
    Windows XP Access 2007
    Join Date
    May 2013
    Posts
    3

    Add a single date to all rows

    I want create a query that adds a field called DATE to my existing data. I want every record to have a date of 1/1/2011 populated in it. I need it to have date formatting so that I can add/subtract other dates from it.

    DATE
    1/1/2011
    1/1/2011
    1/1/2011

    Can someone please show how to do this?

    Thanks!



    Joe

  2. #2
    redbull's Avatar
    redbull is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Mar 2012
    Location
    Missouri
    Posts
    480
    Assuming your table already has the column for the date.... here ya go

    Code:
    Public Function RecordLoops()
    Dim r As DAO.Recordset
     
    Set r = CurrentDb.OpenRecordset("Select * from yourtablename") ' Query or Table records to loop th
      'Check to see if the recordset actually contains rows
         If Not (r.EOF And r.BOF) Then
           r.MoveFirst
         r.Edit
       Do Until r.EOF = True
    'Start code for each record to do here
        r.Edit
          r![ColumnName] = "whatever you want "
            r![ColumnName] = Date ' This will put in today's date... incase you needed it.
          r.Update
        r.MoveNext
      Loop
    Else
        ' if there are no records in the table, you can place something here if you'd like
    End If
    
    
    r.Close
    Set r = Nothing
    End Function
    This will loop thru each record and allow you to add in whatever you'd like on the table. I have to give June7 a big shout out here, he helped me a lot of with this.

    I have this as a function just so I can call it from a macro that runs daily.

  3. #3
    JoeFootball91 is offline Novice
    Windows XP Access 2007
    Join Date
    May 2013
    Posts
    3
    Thanks redbull. Your code is way more advanced than what I'm used to, so I'm not really following it.

    Below is the code I was trying. It populates every record with 1/1/2011, however, it's text format instead of a date format.

    Select
    TABLE01.*,
    '1/1/2011' as DTE
    From TABLE01;

    Do you know I could alter it so the DTE column is formatted as a date?

  4. #4
    redbull's Avatar
    redbull is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Mar 2012
    Location
    Missouri
    Posts
    480
    Go to the table properties and change the column format to date

  5. #5
    JoeFootball91 is offline Novice
    Windows XP Access 2007
    Join Date
    May 2013
    Posts
    3
    Yep, that works. Thanks!

    Just out of curiosity, is there a way to change the format to date in SQL? I was trying the code below, but it keeps resulting in text format.

    Select
    TABLE01.*,
    Format(#1/1/2011#,"Short Date") as DTE
    From TABLE01;

  6. #6
    redbull's Avatar
    redbull is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Mar 2012
    Location
    Missouri
    Posts
    480
    I am not sure... might want to spin off into a new thread. Glad I could help.

    From looking at it... it might have something to do with DD/MM/YYYY but im sketchy about it lol

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

Similar Threads

  1. Replies: 8
    Last Post: 02-28-2013, 02:21 PM
  2. Replies: 5
    Last Post: 04-10-2012, 08:53 AM
  3. Replies: 5
    Last Post: 12-01-2011, 05:38 PM
  4. Relate rows in a single table
    By HPG in forum Access
    Replies: 6
    Last Post: 11-07-2011, 10:10 AM
  5. Access to Excel transferring multiple rows to single row
    By peter_lawton in forum Import/Export Data
    Replies: 10
    Last Post: 09-23-2009, 10:16 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