Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Calculation of date variables with IF statements part 2

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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
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.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…

670 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