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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JRNewKid
    New Member
    • Apr 2007
    • 9

    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!!!
  • Corster
    New Member
    • Mar 2007
    • 36

    #2
    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:
    Code:
    Dim vblDate1 As Date
    Dim vblDate2 As Date
    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:
    Code:
    Private Sub btnCalc_Click()
        txtDate1.SetFocus
        vblDate1 = CDate(txtDate1.Text)
        txtDate2.SetFocus
        vblDate2 = CDate(txtDate2.Text)
        vblDateDiff = DateDiff("y", vblDate1, vblDate2)
        Text5.SetFocus
        Text5.Text = vblDateDiff & " Days"
    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.

    Comment

    • JRNewKid
      New Member
      • Apr 2007
      • 9

      #3
      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!!!


      Originally posted by Corster
      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:
      Code:
      Dim vblDate1 As Date
      Dim vblDate2 As Date
      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:
      Code:
      Private Sub btnCalc_Click()
          txtDate1.SetFocus
          vblDate1 = CDate(txtDate1.Text)
          txtDate2.SetFocus
          vblDate2 = CDate(txtDate2.Text)
          vblDateDiff = DateDiff("y", vblDate1, vblDate2)
          Text5.SetFocus
          Text5.Text = vblDateDiff & " Days"
      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.

      Comment

      • AccessIdiot
        Contributor
        • Feb 2007
        • 493

        #4
        I think you can also just create an empty text box, then in the text box put
        Code:
         = [EndTimeField] - [StartTimeField]
        Then format the field to the time format you want (like Medium Time or whatever).

        HTH

        Comment

        • Corster
          New Member
          • Mar 2007
          • 36

          #5
          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:

          Code:
          Dim vblTime1 As Date
          Dim vblTime2 As Date
          Dim vblTimeDiff As Integer
          Private Sub btnCalc_Click()
              txtTime1.SetFocus
              vblTime1 = CDate(txtTime1.Text)
              txtTime2.SetFocus
              vblTime2 = CDate(txtTime2.Text)
              vblTimeDiff = Format(vblTime2 - vblTime1, "hh:mm:ss")
              txtTimeDiff.SetFocus
              txtTimeDiff.Text = vblTimeDiff
          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!

          Comment

          • Corster
            New Member
            • Mar 2007
            • 36

            #6
            Most of it is common logic...
            It's no excuse, but I'm tired - really!

            Code:
            Dim vblTimeDiff As String
            Not Integer - you will get a type mismatch. Integer is a whole number, so a time will not be accepted!

            Comment

            Working...