473,385 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

I am a total new person to Access... help please!

I think this is an easy question but I'm having a hard time with the manuals.
I have a form which includes a start date and an end date. When the user has entered the data on the form, I want to have a button on the form that says 'calculate' and it figures the difference between the times and puts it in a field for storage in the record. I don't want to use VB I want to use Access on this. The format of the time fields is medium time.
Thanks for any help!!!
Apr 12 '07 #1
5 1352
Corster
36
I know you're thinking "God help me!", but VB coding is the best way to get exactly what you want in Access.
It's not as complex as you might think, most of it is logic. Try implementing the following code with your objects' (Controls') names:

Make sure you create a text box or label for the calculated difference in dates.

Assign your variables:
Expand|Select|Wrap|Line Numbers
  1. Dim vblDate1 As Date
  2. Dim vblDate2 As Date
  3. Dim vblDateDiff As Integer
Be sure to name your text boxes accordingly;
I use the prefix txt for TextBox, lbl for Label, btn for Button, vbl for Variable to ensure that I don't use reserved names:
Expand|Select|Wrap|Line Numbers
  1. Private Sub btnCalc_Click()
  2.     txtDate1.SetFocus
  3.     vblDate1 = CDate(txtDate1.Text)
  4.     txtDate2.SetFocus
  5.     vblDate2 = CDate(txtDate2.Text)
  6.     vblDateDiff = DateDiff("y", vblDate1, vblDate2)
  7.     Text5.SetFocus
  8.     Text5.Text = vblDateDiff & " Days"
  9. End Sub
"y" returns the number of days between the two dates.
You need to set focus of the controls you are referencing before taking or setting properties.
CDate converts the String or Numeric value to Date format so that DateDiff can recognise and utilise the format effectively.

I hope this has resolved the issue and that it has given some insight.

Corster.
Apr 13 '07 #2
Now I'm embarrassed 'cause I misspoke on my question. I think my frustration level was kinda high yesterday.
I'm working on times not dates. Sorry!! I have a start time and an end time and want to calculate (and store in the record) the difference in times. I expect the code is similar to below? I am intimidated by VB but I will give it a whirl.
Thank you!!!


I know you're thinking "God help me!", but VB coding is the best way to get exactly what you want in Access.
It's not as complex as you might think, most of it is logic. Try implementing the following code with your objects' (Controls') names:

Make sure you create a text box or label for the calculated difference in dates.

Assign your variables:
Expand|Select|Wrap|Line Numbers
  1. Dim vblDate1 As Date
  2. Dim vblDate2 As Date
  3. Dim vblDateDiff As Integer
Be sure to name your text boxes accordingly;
I use the prefix txt for TextBox, lbl for Label, btn for Button, vbl for Variable to ensure that I don't use reserved names:
Expand|Select|Wrap|Line Numbers
  1. Private Sub btnCalc_Click()
  2.     txtDate1.SetFocus
  3.     vblDate1 = CDate(txtDate1.Text)
  4.     txtDate2.SetFocus
  5.     vblDate2 = CDate(txtDate2.Text)
  6.     vblDateDiff = DateDiff("y", vblDate1, vblDate2)
  7.     Text5.SetFocus
  8.     Text5.Text = vblDateDiff & " Days"
  9. End Sub
"y" returns the number of days between the two dates.
You need to set focus of the controls you are referencing before taking or setting properties.
CDate converts the String or Numeric value to Date format so that DateDiff can recognise and utilise the format effectively.

I hope this has resolved the issue and that it has given some insight.

Corster.
Apr 13 '07 #3
AccessIdiot
493 256MB
I think you can also just create an empty text box, then in the text box put
Expand|Select|Wrap|Line Numbers
  1.  = [EndTimeField] - [StartTimeField]
Then format the field to the time format you want (like Medium Time or whatever).

HTH
Apr 13 '07 #4
Corster
36
Don't be intimidated! Like everyone else's first time losing their VB-ginity, I wouldn't be surprised if you too are thinking "WTF? It's a whole 'nother language! What does it all mean?"

Most of it is common logic. The rest is knowing what kinda verbs to use. Try this, it's similar to what I first posted, and CDate converts time as well as date:

Expand|Select|Wrap|Line Numbers
  1. Dim vblTime1 As Date
  2. Dim vblTime2 As Date
  3. Dim vblTimeDiff As Integer
  4. Private Sub btnCalc_Click()
  5.     txtTime1.SetFocus
  6.     vblTime1 = CDate(txtTime1.Text)
  7.     txtTime2.SetFocus
  8.     vblTime2 = CDate(txtTime2.Text)
  9.     vblTimeDiff = Format(vblTime2 - vblTime1, "hh:mm:ss")
  10.     txtTimeDiff.SetFocus
  11.     txtTimeDiff.Text = vblTimeDiff
  12. End Sub
So basically, your "hh:mm:ss" part is the format you want your time to display in. you can change the hh to just h to suppress any leading zeros, as with the minutes and seconds. You can also change the : to any other character to separate the values, like "." or "-" or even "£" if you so wish!
Apr 16 '07 #5
Corster
36
Most of it is common logic...
It's no excuse, but I'm tired - really!

Expand|Select|Wrap|Line Numbers
  1. Dim vblTimeDiff As String
Not Integer - you will get a type mismatch. Integer is a whole number, so a time will not be accepted!
Apr 16 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Jeroen van vliet | last post by:
Hello, im using visual basic 6.0 and i made an form where to insert names,street and an amount of money into an access database. Well every time when i insert an amount of money, the amount has...
4
by: James Greig | last post by:
hello people, i'm just learning javascript, could someone point me in the direction of an example of the following, or give me some clues as to how it might be done: what i would like to do...
1
by: Nothing | last post by:
I have a report that has several groups on it. I want to total for each group. My problem is that I group by a name and under the name I have data that breaks out into several sub-groups for that...
4
by: mukeshhtrivedi | last post by:
I have TYPE field which has 3 data like HEAD, TRACK and PANEL. Now whenever any person works on HEAD we put 8 Hrs in HRS filed and in LABOR field it reflects the dollar value automatically like...
2
by: gaga | last post by:
hi all, i need to write a program - cash register for dummies, so to speak. here's the gist - enter the amount of pies that a person wants to get. A person can get either, 1, 2, 3, or 4 pizza...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.