Skip to content

Instantly share code, notes, and snippets.

@ricdeez
Created February 5, 2014 06:02
Show Gist options
  • Save ricdeez/8818138 to your computer and use it in GitHub Desktop.
Save ricdeez/8818138 to your computer and use it in GitHub Desktop.
Update Access database date/time field to show date at the end of the week
%w(win32ole date time).each { |dep| require dep }
def week_ending(tm)
# The <?Date> object passed in to week_ending is actually an instance of the Time class
new_tm = tm
offset = case new_tm.strftime('%a')
when "Mon"
6
when "Tue"
5
when "Wed"
4
when "Thu"
3
when "Fri"
2
when "Sat"
1
when "Sun"
0
end
new_tm + (offset*60*60*24)
end
cn = WIN32OLE.new('ADODB.Connection')
db = "project timesheets.accdb"
cs = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=#{db};"
cn.Open(cs)
rs = WIN32OLE.new('ADODB.Recordset')
rs.Open('SELECT * FROM Timesheets_T', cn,1,3)
rs.MoveFirst()
while not rs.EOF
# rs.Fields("WeekEnding").Value = week_ending (rs.Fields("Date").Value).to_date
dt = rs.Fields("Date").Value
new_dt = week_ending(dt)
rs.Fields("WeekEnding").Value = new_dt
rs.Update()
rs.MoveNext()
end
cn.Close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment