Solved

Calculation of date variables with IF statements part 2

Posted on 2013-12-08
3
335 Views
Last Modified: 2013-12-08
I am still having trouble converting an unbound field "txtRptPD" to a calculated start and end date (dtStart & dtEnd) depending on the value of another variable "vType"

I have a "msgbox" pop up after I update the vType variable to make sure it has a value:
Private Sub lstEval_AfterUpdate()
 Dim vType As String
     vType = Trim(Me.LstEval.Column(0) & "")

     MsgBox vType & ""  '<--------Does this display a value?
 End Sub

Open in new window


After I update the "txtRptPD" field I have another msgbox pop up to show me the "dtRpt" variable show.  Then I have a msgbox open to show the "dtStart" & "dtEnd" values.

The dtRpt value is good.  However, the dtStart and dtEnd shows a time 12:00:00 AM.

Below is the code to calculate and then show the dtRpt, dtStart and dtEnd values:

Private Sub txtRptPD_AfterUpdate()
 Dim dtEnd As Date
 Dim dtStart As Date
 Dim dtRpt As Date
 dtRpt = Me.txtRptPD
     

 

If vType = "Advancement" Then
     dtStart = Format(DateSerial(Year(DateAdd("m", -7, dtRpt)), Month(DateAdd("m", -7, dtRpt)), 1), "mm/dd/yyyy")
     dtEnd = Format(DateAdd("d", -1, DateSerial(Year(dtRpt), Month(dtRpt), 1)), "mm/dd/yyyy")
 End If

 If vType = "Reappointment" Then
     dtStart = Format(DateSerial(Year(DateAdd("m", -25, dtRpt)), Month(DateAdd("m", -25, dtRpt)), 1), "mm/dd//yyyy")
     dtEnd = Format(DateAdd("d", -1, DateSerial(Year(dtRpt), Month(dtRpt), 1)), "mm/dd/yyyy")
 End If

    MsgBox dtRpt & ""  '<--------Does this display a value?
     MsgBox dtStart & ""
     MsgBox dtEnd & ""
 End Sub 

Open in new window

Can someone look at the Calculation code and give me  a suggestion on how to store the correct values to dtStart and dtEnd?

Thanks

Glen


Thanks
0
Comment
Question by:GPSPOW
  • 2
3 Comments
 
LVL 61

Expert Comment

by:mbizup
ID: 39705175
Your vType variable is seen in the first sub, but it is local to that sub, and not seen in the second.

>>> Dim vType As String

Try moving this line of code from lstEval_AfterUpdate to the very top of the code behind your form, immediately under Option Compare Database and Option Explicit (which you should add if it is not in your code already):

Option COmpare Database
Option Explicit

Dim vType As String

Open in new window


Placing it at the top of your form's module makes it visible to all of the procedures in that module.
0
 
LVL 61

Accepted Solution

by:
mbizup earned 500 total points
ID: 39705178
That said... a better solution would be to remove the listbox after update event and simply use this (unless there is more code not shown here in lstEval_AfterUpdate that uses vType) :


Private Sub txtRptPD_AfterUpdate()
 Dim dtEnd As Date
 Dim dtStart As Date
 Dim dtRpt As Date


 Dim vType As String
     vType = Trim(Me.LstEval.Column(0) & "")


 dtRpt = Me.txtRptPD
     

 

If vType = "Advancement" Then
     dtStart = Format(DateSerial(Year(DateAdd("m", -7, dtRpt)), Month(DateAdd("m", -7, dtRpt)), 1), "mm/dd/yyyy")
     dtEnd = Format(DateAdd("d", -1, DateSerial(Year(dtRpt), Month(dtRpt), 1)), "mm/dd/yyyy")
 End If

 If vType = "Reappointment" Then
     dtStart = Format(DateSerial(Year(DateAdd("m", -25, dtRpt)), Month(DateAdd("m", -25, dtRpt)), 1), "mm/dd//yyyy")
     dtEnd = Format(DateAdd("d", -1, DateSerial(Year(dtRpt), Month(dtRpt), 1)), "mm/dd/yyyy")
 End If

    MsgBox dtRpt & ""  '<--------Does this display a value?
     MsgBox dtStart & ""
     MsgBox dtEnd & ""
 End Sub 

Open in new window

0
 

Author Closing Comment

by:GPSPOW
ID: 39705187
You were right about the enhancement.

When I ran it before making the vType adjustment, vtype was not recognized.

After I moved it down, worked  like a charm.

thanks

Glen
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

685 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