1 Replies - 269 Views - Last Post: 27 January 2012 - 01:19 AM Rate Topic: -----

Topic Sponsor:

#1 cuatanic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 18-January 12

updating all the records in the database

Posted 26 January 2012 - 07:34 PM

i am making a program that computes the age with the use of their birthday in the database. what i want is when i click the update button, all the age of the record will update. my problem is when i click the update button, the updated age will all be the same.

here is what i have so far

If rstCustomer.EOF = False Then
rstCustomer.MoveFirst

Datenow = Format(Now, "mm/dd/yyyy")
Do
Bday = rstCustomer.Fields("Birthday")
Select Case Month(Bday)
Case Is < Month(Datenow)
Text1.Text = DateDiff("yyyy", Bday, Datenow)
Case Is = Month(Datenow)
Select Case Day(Bday)
Case Is < Day(Datenow)
Text1.Text = DateDiff("yyyy", Bday, Datenow)
Case Is = Day(Datenow)
Text1.Text = DateDiff("yyyy", Bday, Datenow)
Case Is > Day(Datenow)
Text1.Text = DateDiff("yyyy", Bday, Datenow) - 1
End Select
Case Is > Month(Datenow)
Text1.Text = DateDiff("yyyy", Bday, Datenow) - 1
Case Else
Text1.Text = 0
End Select
conn.Execute "Update Table1 set age ='" & Text1 & "' where ID ='" & 1 & "'"
rstCustomer.MoveNext
Loop Until rstCustomer.EOF = True



i attached my program

thank you

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: updating all the records in the database

#2 Ionut  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 327
  • View blog
  • Posts: 914
  • Joined: 17-July 10

Re: updating all the records in the database

Posted 27 January 2012 - 01:19 AM

conn.Execute "Update Table1 set age ='" & Text1 & "' where ID ='" & 1 & "'"


First, problems here.
1. ID doesn't change at all, it is always 1. Change that with the ID of the current record.
2. Text1 is a textbox, so you have to send the value stored in Text property.
3. Use parameterized queries to avoid SQL injections.
conn.Execute "Update Table1 set age ='" & Text1.Text & "' where ID ='" & rstCustomer.Fields("ID") & "'"



Now, I don't know if what is the logic behind your program, but you can achive all above through SQL statement
UPDATE Table1
SET age = case                
             when Month(Birthday) <= Month(GetDate()) and Day(Birthday) <= Day(GetDate()) then Datediff('yyyy', Birthday, GetDate())
             else DateDiff('yyyy', Bday, Datenow) - 1 
          end 



This works for SQL Server. If you use another database engine, it won't be hard to convert.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1