Solved

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

Posted on 2011-03-23
6
333 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
Technology Partners: 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

632 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