Solved

Calculation of date variables with IF statements part 2

Posted on 2013-12-08
3
326 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 Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

760 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now