Full Version: Code for Date Calculations on If Statement
UtterAccess Discussion Forums > Microsoft® Access > Access Forms
ShelleyClark
In my database I have two fields [TrainingDate] and [Expiration] date. I want the Expiration date to automatically figure "TrainingDate" + 1yr, except if the [ClassName] = "Scaffolding" ( it will add 2 yrs) or "ConfinedSpace" ( add 3 years)

I started with
Private Sub TRAINING_DATE_Enter()
If CLASSNAME = "Scaffold User" Then
DateAdd("y",2,[EXPIRATION])
ElseIf CLASSNAME = "Confined Space" Then
DateAdd("y",3,[EXPIRATION])
Else
DateAdd("y",1,[EXPIRATION])
End If
End Sub

I've put this in the training date events on the on Enter field.

I'm new to this and I'm getting syntax errors. Can someone tell me if I'm heading in the right direction?!?!?!?!
dashiellx2000
You should not be storing Expiration Date in your table, but just displaying this are run time. I'd go with the following on the Training Date After Update Event:

CODE
Select Case Me.ClassName
Case "Scoffold User"
    Me. Expiration = DateAdd("y",2,Me.TrainingDate)
Case "Confined Space"
    Me. Expiration = DateAdd("y",3,Me.TrainingDate)
Case Else
    Me. Expiration = DateAdd("y",1,Me.TrainingDate)
End Select


HTH.
BrianS
You are not assigning the results of DateAdd to anything and your Strings "Scaffold User" and "Confined Space" are not consistent with your description above, "Scaffolding" and "ConfinedSpace". Which is it?

try this

Use the AfterUpdate event instead.

CODE
Private Sub TrainingDate_AfterUpdate()

Dim multiplier as Long

Select Case Me.ClassName
   Case "Scaffolding"
      multiplier = 2
   Case "ConfinedSpace"
      multiplier = 3
   Case Else
      multiplier = 1
End Select

Me.Expiration = DateAdd("y",multiplier,Me.TrainingDate)

End Sub
ShelleyClark
ok, I have been trying this and I believe I understand the code, (it's been years since I did Basic) but it isn't working and I believe it has something to do with the Control box settings. In my subform I have the Class name (pulled from a look up table and stored in the records table) the training date (I enter and is stored in the records table) then I have an unbound box named Expiration. I entered this in the code after update but I'm not getting anything. Can you look at the attached file and let me know what I might be doing wrong?
ShelleyClark
2nd attachemt
ShelleyClark
Sorry the second file I attached was a duplicate of the first. Maybe this one will come accross right. It is supposed to be a screen shot of the code.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.