Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Calculation of date variables with IF statements part 2

Posted on 2013-12-08
3
Medium Priority
?
340 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Windows Explorer let you handle zip folders nearly as any other folder: Copy, move, change, and delete, etc. In VBA you can also handle normal files and folders, but zip folders takes a little more - and that you'll find here.
In a use case, a user needs to close an opened report by simply pressing the Escape (Esc) key. This can be done by adding macro code in Report_KeyPress or Report_KeyDown event.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

972 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