Solved

Accessing Date And Time Separately From A Date/Time Field In An Access Database

Posted on 2011-03-23
6
329 Views
Last Modified: 2012-05-11
Hi

I am using Visual Basic.Net (Visual Studio 2008) and an Access 2007 Database. In the Access Database I a have Field called LvDate and its Storage format is Date/Time. At Any point in time, I put the value of Now in there. For example the field content is 23/03/2011 10:23:45. What is the easiiest way of addressing and extracting each one of these separately - I wish to show the Date and Time components under diifferent column headings in a report or enquiry say.

Many thanks
0
Comment
Question by:Nolanc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 35197046
You would still pull the data from the database into a single DateTime variable in code. You then simply use formatting features of the grid/report to display either the time or date part of the value.

For example, if you were displaying in a grid you might use:
<span><%# Eval("YourField", "dd/MM/yyyy") %></span>

Open in new window

0
 

Author Comment

by:Nolanc
ID: 35197218
Hi carl tawn

I am not sure what language you are using as an example but let's say I am reading from the affected table and I wish to do the following:

Dim dDate as DateTime   (as you are suggesting)

ddate = dsLvH.Tables("LvHis").Rows(I).Item("LvDate")    which I think is what you are suggesting

Now I wish To display the Time component for each record as follows:

MsgBox (dDate.TimeOfDay)

I get a Run-Time error saying it is unable to convert the data to type String - It looks like I have to do a conversion of some description for MsgBox.

Am I on the right track

Thanks
0
 
LVL 5

Expert Comment

by:karstieman
ID: 35197228
Dim i As Integer
Dim aryTextFile() As String

LineOfText = "UserName1, Password1, UserName2, Password2"

aryTextFile = dDate.tostring.Split(" ")

For i = 0 To UBound(aryTextFile)
MsgBox(aryTextFile(i))
Next i
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 52

Accepted Solution

by:
Carl Tawn earned 100 total points
ID: 35197235
If you want to show it in a message box like that you would pass a format to the ToString() method to control the format:
MsgBox(dDate.ToString("HH:mm"))        '// display the time only

Open in new window

0
 
LVL 5

Expert Comment

by:karstieman
ID: 35197248
oops. one line should not be in my code ;-)
Dim i As Integer
Dim aryTextFile() As String

aryTextFile = dDate.tostring.Split(" ")

For i = 0 To UBound(aryTextFile)
MsgBox(aryTextFile(i))
Next i

Open in new window

0
 

Author Closing Comment

by:Nolanc
ID: 35197333
Hi carl tawn

Thanks. It works beautifully.

Nolanc
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question