VS Application/Access Db Date Conversion Issues - Very Weird

Reply

Join Date: Jun 2008
Posts: 2
Reputation: richo.mw is an unknown quantity at this point 
Solved Threads: 0
richo.mw richo.mw is offline Offline
Newbie Poster

VS Application/Access Db Date Conversion Issues - Very Weird

 
0
  #1
Jun 2nd, 2008
Hi There,

I am experiencing a very weird problem with a visual basic.net program I am creating. The app uses an access database.

Note: I am an Australian user so use date format dd/mm/yyyy

Problem: my frmMain has a list box that retrieves a list of date values from the access db and displays them in the correct format (15/12/2000 - dd/mm/yyyy). When I update the DB using that same date the date format becomes reversed (ie interprets as 12/15/2000 - mm/dd/yyyy).

Test cases:
01/02/2000 - WRONG... Updates database records for 2/1/2000
31/02/2000 - CORRECT... updates 31st Feb

I believe the problem is something to do with the fact I am enclosing my date in my code with #'s and that somewhere between visual studio and access, the dates are getting flipped.

Here is a sample of my code...

  1. Private Sub UpdateDatabase()
  2. MessageBox.Show(lbxDate2.Text)
  3. Dim strUpdateDates As String = _
  4. "UPDATE Dates SET Observation = '" & txtObservation.Text & _
  5. "' WHERE Dates.Date2 = #" & lbxDate2.Text & "#"
  6.  
  7. Dim cmdWeatherDbDates As New System.Data.OleDb.OleDbCommand(strUpdateDates, connWeatherDb)
  8. Dim intDatesUpdateRows As Integer
  9.  
  10. cmdWeatherDbDates.Connection.Open()
  11. intDatesUpdateRows = cmdWeatherDbDates.ExecuteNonQuery()
  12. connWeatherDb.Close()
  13. End Sub


Any help would be much appreciated!
I have tried everything I can think of and more. As well as spending a whole day trying to work out the issue.

Matt
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 22
Reputation: dadelsen is an unknown quantity at this point 
Solved Threads: 5
dadelsen dadelsen is offline Offline
Newbie Poster

Re: VS Application/Access Db Date Conversion Issues - Very Weird

 
0
  #2
Jun 3rd, 2008
There seems to be some logic in the access DB engine to recognize that 31 cannot be the number of a month. But not enough to recognize that Feb 31th is very unusual.

maybe VBA documentation is helpful:
http://msdn.microsoft.com/en-us/libr...ffice.11).aspx
http://msdn.microsoft.com/en-us/libr...ffice.11).aspx

When using SQL and # # to write dates to Access, MS recommends to use USA dateformat.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 2
Reputation: richo.mw is an unknown quantity at this point 
Solved Threads: 0
richo.mw richo.mw is offline Offline
Newbie Poster

Re: VS Application/Access Db Date Conversion Issues - Very Weird

 
0
  #3
Jun 4th, 2008
Cheers for the response. I have since resolved this issue.

"The SQL standard was set up to always use the US format for dates (and numbers, etc), and not what you have picked elsewhere.

The problem with some dates being 'flipped' and others not is because Access/Jet decides to be 'nice' (or to be more accurate, Evil!), and convert values that don't fit.. 01/02/2000 is apt in US format so is interpreted as such; 31/02/2000 isn't apt because 31 is too big to be a month, and so it gets automatically converted to 02/31/2000.


What you could do is add the value to your SQL string in US format, however it is much safer instead to add it as a Parameter to the Command - as that will ensure that it is delimited and formatted correctly (assuming of course that you actually give it a Date value, rather than Text).
"

Used parameters and started to make progress.

My code (working as it should) below: ...there are additional components now to the original code
  1.  
  2. Private Sub UpdateDatabase()
  3. 'Update the date related information into Dates table.
  4. Dim strUpdateDates As String = _
  5. "UPDATE Dates SET Observation = '" & txtObservation.Text & _
  6. "' WHERE Dates.Date2 = @DateParam"
  7.  
  8. 'Update all reading information into readings table.
  9. Dim strUpdateReadings As String = _
  10. "Update Readings Set Readings.Rained = " & cbxRained.Checked & _
  11. ", Readings.Tmax = " & txtTempMax.Text & _
  12. ", Readings.Tmin = " & txtTempMin.Text & _
  13. ", Readings.Rain = " & txtRain.Text & _
  14. ", Readings.Evap = " & txtEvaporation.Text & _
  15. ", Readings.VapourPressure = " & txtVapourPressure.Text & _
  16. " WHERE Readings.Date2 = @DateParam"
  17.  
  18. 'Create Db command using update statements and Db connection string
  19. Dim cmdWeatherDbDates As New System.Data.OleDb.OleDbCommand(strUpdateDates, connWeatherDb)
  20. Dim cmdWeatherDbReadings As New System.Data.OleDb.OleDbCommand(strUpdateReadings, connWeatherDb)
  21.  
  22. 'Add Date parameter for update queries to respective commands. Required to keep the date in
  23. 'correct format and prevent date changing between AUS and US date formats.
  24. cmdWeatherDbDates.Parameters.AddWithValue("@DateParam", lbxDate2.SelectedValue)
  25. cmdWeatherDbReadings.Parameters.AddWithValue("@DateParam", lbxDate2.SelectedValue)
  26.  
  27. Dim intDatesUpdateRows As Integer
  28. Dim intReadingsUpdateRows As Integer
  29.  
  30. cmdWeatherDbDates.Connection.Open() 'Open connection to Db
  31. intDatesUpdateRows = cmdWeatherDbDates.ExecuteNonQuery() 'Execute query for Dates table
  32. intReadingsUpdateRows = cmdWeatherDbReadings.ExecuteNonQuery() 'Execute query for Readings table
  33. connWeatherDb.Close() 'Close Db connection
  34. End Sub

Thanks for response.
Hope this helps anyone who has the same problem in the future
Last edited by richo.mw; Jun 4th, 2008 at 7:57 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the VB.NET Forum


Views: 1168 | Replies: 2
Thread Tools Search this Thread



Tag cloud for VB.NET
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC