Date retrieval problem via access database

Please support our VB.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2009
Posts: 20
Reputation: BlkR is an unknown quantity at this point 
Solved Threads: 0
BlkR BlkR is offline Offline
Newbie Poster

Date retrieval problem via access database

 
0
  #1
Aug 3rd, 2009
Hi,

Having troubles with the conversion of date in a textbox i think.... currently txtDate.text has the date format of dd/mm/yyyy and im trying to grab data from the database that is equal to that date and put it in a listview. Problem Im haivng is the conversion from MM/dd/yyyy and dd/MM/yyyy which is so damn frustrating. My access database has the format of dd/MM/yyyy and yet it won't retrieve the data needed

here is part of the code.....
  1. If dtpSelection.Checked And dtpSelection2.Checked Then
  2. MyConn = New ADODB.Connection
  3. MyConn.ConnectionString = ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\MenuItems.mdb;")
  4. MyConn.Open()
  5. 'MessageBox.Show(FormatDateTime(dtpSelection.Value.ToShortDateString))
  6. MyRecSet = MyConn.Execute("SELECT Item_ID, ItemName, Price, Qty, Total, Description, OrderDate FROM trans2 where (((trans2.OrderDate) = #" & txtdate.Text & "#))")
  7.  
  8. Do Until MyRecSet.EOF
  9. var = MyRecSet.Fields.Item("Item_ID").Value
  10. var2 = MyRecSet.Fields.Item("ItemName").Value 'variable2 = MyRecSet.Fields.Item("column2").Value
  11. var3 = MyRecSet.Fields.Item("Price").Value
  12. var4 = MyRecSet.Fields.Item("Qty").Value
  13. var5 = MyRecSet.Fields.Item("Total").Value
  14. var6 = MyRecSet.Fields.Item("OrderDate").Value
  15. var7 = MyRecSet.Fields.Item("Description").Value.ToString
  16. ' lvItems.Clear()
  17. lvwItem2 = lvItems.Items.Add(var6)
  18. lvwItem2.SubItems.Add(var)
  19. lvwItem2.SubItems.Add(var2)
  20. lvwItem2.SubItems.Add(var3)
  21. lvwItem2.SubItems.Add(var4)
  22. lvwItem2.SubItems.Add(var5)
  23. lvwItem2.SubItems.Add(var7)
  24. ' Listbox2.AddItem(variable2)
  25. MyRecSet.MoveNext()
  26. Loop
  27. MyConn.Close()
  28. End If


the problem i think is that the the value inside txtdate.text was retrieve from another listview and was grabbed like this

  1. txtdate.Text = slist.Text

any help would good thanks guys..... and ive tried these cdate, formatdatetime etc
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,373
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 608
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: Date retrieval problem via access database

 
0
  #2
Aug 3rd, 2009
Use parameterized queries to avoid date formatting issues.

Example:
  1. Private Sub DisplayPersonData(ByVal first_name As String, _
  2. ByVal last_name As String)
  3. ' Open the connection.
  4. connUsers.Open()
  5.  
  6. ' Make a Command for this connection
  7. ' and this transaction.
  8. Dim cmd As New OleDb.OleDbCommand( _
  9. "SELECT * FROM People WHERE FirstName=? AND " & _
  10. "LastName=?", _
  11. connUsers)
  12.  
  13. ' Create parameters for the query.
  14. cmd.Parameters.Add(New _
  15. OleDb.OleDbParameter("FirstName", first_name))
  16. cmd.Parameters.Add(New OleDb.OleDbParameter("LastName", _
  17. last_name))
  18.  
  19. ' Execute the query.
  20. Dim db_reader As OleDbDataReader = _
  21. cmd.ExecuteReader(CommandBehavior.SingleRow)
  22.  
  23. ' Display the results.
  24. If db_reader.HasRows Then
  25. db_reader.Read()
  26. txtFirstName.Text = _
  27. db_reader.Item("FirstName").ToString
  28. txtLastName.Text = _
  29. db_reader.Item("LastName").ToString
  30. txtStreet.Text = db_reader.Item("Street").ToString
  31. txtCity.Text = db_reader.Item("City").ToString
  32. txtState.Text = db_reader.Item("State").ToString
  33. txtZip.Text = db_reader.Item("Zip").ToString
  34. Else
  35. For Each ctl As Control In Me.Controls
  36. If TypeOf ctl Is TextBox Then ctl.Text = ""
  37. Next ctl
  38. End If
  39.  
  40. ' Close the connection.
  41. connUsers.Close()
  42. End Sub

Borrowed from:
http://www.vb-helper.com/howto_net_d...zed_query.html
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC