Solved

Calculation of date variables with IF statements part 2

Posted on 2013-12-08
3
337 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
[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
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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

630 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