Access 2013: I am trying to return today's date when a yes/no fields is marked as "Yes" - date

I am trying to return today's date in a date field when another field is marked as "Yes". I tried the following expression but to no avail. I have limited experience with expressions and greatly appreciate any guidance. If the field does not have a "Yes" the date field can be blank. I cannot set the default to "No" for the Approved field.
Approved Date: IIf([Approved]=True,Today(),Null)
If I set the date, it will work but the date is dynamic so this is not really helpful.
Approved Date: IIf([Approved]=True,5/1/2016,Null)
Thank you in advance for your help.

OK, so I spent a little time looking at how to do this with an expression. The answer is you can't, at least not without a helper function. So, my hybrid solution is:
1) Create a function in a standard module:
Public Function SetControlValue( _
ByVal ctlControl As Access.Control, _
ByVal varValue As Variant, _
Optional ByVal varTest As Variant = True)
If (varTest) Then
ctlControl.Value = varValue
End If
End Function
2) In the AfterUpdate event for the Approved checkbox, enter:
=SetControlValue([ApprovedDate],Date(),([Approved]=True) AND (IsNull([ApprovedDate])))
This approach saves you from making a class module under the form. And you can keep all such code in a common module, so you can build a library of such functions for other forms.
(old answer 2)
Based on your answers, what you can do is add an AfterUpdate event to the Approved control, which does something like:
Private Sub Approved_AfterUpdate()
If (Approved.Value = True) And IsNull(ApprovedDate.Value) Then
ApprovedDate.Value = Date()
End If
End Sub
This will set the approved date once, when the approved checkbox is first checked. If you need different behavior, this can easily be modified.
(old answer 1)
I think you have it backwards.
When [Approved] is set to True, set [ApprovedDate] = Today(). That way, it is saved to the table, and you have a permanent record of when it was approved.

Related

How do you create branching logic/skipping rules in a Microsoft Access form?

I'm creating a very simple Access database with a table and corresponding form. For some questions on the form, I'd like to disable following questions, or hide them using branching logic.
For example, in my form I have a combobox question that asks: Are you a smoker? - "Yes", "No", "Prefer not to answer". The following question is: If yes, how often do you smoke? If they chose the answers "No" or "Prefer not to answer" for the first question, then I don't want the second question to be visible/enabled.
I've been searching for a way to do this and the easiest way seems to be setting the Visible property of textbox "If yes, how often do you smoke?" to No. After that, I go to question "Are you a smoker?" and go to Event Procedure in the Properties menu. This brings up a VBA code editor with the following text:
Option Compare Database
Private Sub Text969_Click()
End Sub
Private Sub Combo367_Click()
End Sub
I've been looking at different pages but I can't seem to get the code to work. For the particular question I'm asking, the name of the form is "Chronic Smokers" and the field for the first question is named "Are you a smoker." and the second question is named "If yes, how often." This is the code I've been trying and it doesn't work, but I can't seem to figure anything else out:
Option Compare Database
Private Sub Text969_Click()
End Sub
Private Sub Combo367_Click()
If Chronic Smokers.Combo367='Yes' then
Chronic Smokers.If yes, how often.Visible = True
Else
Chronic Smokers.If yes, how often.Visible = False
End if
End Sub
I think part of my problem is that I don't know the way the naming conventions or syntax for this code works. I have a feeling part of the problem is that I have blank spaces without underscores in the names If anybody can help me out with this, I'd really appreciate it!
VBA code could be as simple as Me.[how often].Visible = Me.Combo367 = "Yes". No If Then Else needed. Code will need to be in combobox AfterUpdate as well as form Current events, not Click event. Code will apply to ALL instances of control, not just the current record.
NOTE: use of Me qualifier is shorthand for form/report object code is behind.
If you prefer to use If Then Else, correct syntax would be:
With Me
If .Combo367 = "Yes" Then
.[how often].Visible = True
Else
.[how often].Visible = False
End if
End With
Suggest you explore Conditional Formatting. It allows to enable/disable textboxes and comboboxes dynamically per record without VBA. Controls will still be visible but 'greyed out'.
And yes, strongly advise not to use spaces nor punctuation/special characters (underscore is only exception) in naming convention nor reserved words as names. If you do, then enclose in [ ] as shown above. Better naming would be ChronicSmokers and HowOften. And give objects more meaningful names than the defaults assigned by Access.

Access 2007 form: event AFTER undo

I have a form in Access 2007, which has an "update" routine, that enables or disables certain textboxes based on values in other fields (textboxes, checkboxes, comboboxes). The regular operation of that routine works well.
Now I found that pressing ESC calls the undo function, that restores the original values in all fields. But this undo does not call the events on those fields, so the form is in a wrong state, where textboxes are disabled/enabled although they shouldn't.
I also found that there is an undo-event, but that is useless for me because it is called before undo. I need an event after undo. What can I do here to update the fields when ESC is pressed?
I like this solution more, because it works not only on the "ESC"-Key:
private Sub form_Undo(cancel as integer)
afterUndo = true
TimerInterval = 1
end Sub
private Sub Form_Timer()
if afterUndo then
'do something after the Undo-Event
end if
TimerInterval = 0
end Sub
Well, like many times before I have an idea for a solution after postion the question.
The solution here is enabling KeyPreview on the form and using the KeyUp event. The undo is called on KeyDown, so when KeyUp is raised, the form already has the restored values again and the update routine works.
Another solution to this problem is to use each control's OldValue property. Through experimentation, I've found that the three different value properties of controls come into play in different situations:
Control.Value or simply Control
The control's current value most of the time
When the control has focus, it is the value the control had before gaining focus
During a Form.Undo event, it is the value the control prior to the Undo
Relevant during the Control.AfterUpdate and Control.Undo events
Control.Text
The control's value while it has focus
Relevant during events that occur as the user types, such as Control.Change, Control.KeyUp, Control.KeyDown
Control.OldValue
The value the control had when the current record was first opened
Also the value that a form-level Undo will reset the control to
Relevant during the Form.Undo event
The timer-based answer to this question is my go-to solution, but if you're already handling events as the user types (e.g., for live validation), then code like this can be sensible:
Private Sub LastName_Change()
ValidateLastName SourceProperty:="Text"
End Sub
Private Sub LastName_Undo(Cancel As Integer)
ValidateLastName SourceProperty:="Value"
End Sub
Private Sub Form_Undo(Cancel As Integer)
ValidateLastName SourceProperty:="OldValue"
End Sub
Private Sub ValidateLastName(SourceProperty As Variant)
Dim LastName As String
Select Case SourceProperty
Case "LastName"
LastName = Nz(Me.LastName.Text, "")
Case "Value"
LastName = Nz(Me.LastName.Value, "")
Case "OldValue"
LastName = Nz(Me.LastName.OldValue, "")
Case Else
Debug.Print "Invalid case in ValidateLastName"
Exit Sub
End Select
' <Do something to validate LastName>
End Sub
Note that this method does not get you access to the post-Form-Undo value of Control.Column(x) of combo/list boxes. You can get it by using DLOOKUP on Control.OldValue, but you're probably better off just using the timer-based solution instead.

SSRS check if there was an entry yesterday with IIF expression

I have a problem with an IIF expression in SSRS.
I'm creating a report for seeing who arrives to work late, we have 4 shifts.
I'm trying to get false positives out of the report (e.g. someone goes back in after his/her shift has ended, and they are automatically counted as someone who is late for the next shift)
Everything works except for the night shift(the IIF for it is inside the first shift), which starts the previous night.
i want the code to check if there is already an entry the previous night within the specified timeframe, if there is, than show the new "late" time as 0.
All the other shifts are functional.
so what i have is
=CInt(IIF(
(Format(Fields!Aeg.Value,"HH")="06")
AND (Format(Fields!Aeg.Value,"mm")>"00")
AND (Fields!UksID.Value=14),IIF(CInt((Format(Fields!Aeg.Value, "dd"))-1)
AND(Format(Fields!Aeg.Value, "HH")>="22"),"0",(Format(Fields!Aeg.Value,"mm")))
,(IIF((Format(Fields!Aeg.Value,"HH")="08")
AND (Format(Fields!Aeg.Value,"mm")>"00")
AND (Fields!UksID.Value=14),IIF(Min(Format(Fields!Aeg.Value,"HH")<="8"),"0",
(Format(Fields!Aeg.Value,"mm")), ...
...etc
where "Aeg" is the database field for the logged time
and "UksID" is a filter for the door
Help appreciated.
I've done a few edits according to your suggestion #tezzo,
i added
Public Function CheckDate(ByVal dateFromField As DateTime) As Boolean
Dim dateChecked As DateTime
dateChecked = DateAdd("d", -1, dateFromField)
Dim hourCheck As Integer
hourCheck = Hour(dateChecked)
If hourCheck >= 22 Then
Return True
Else
Return False
End If
End Function
But it's checking for excactly 24 hours ago.
how can i get it to take the Max value from that day
Max(Hour(dateChecked)) gives "Overload resolution failed because no accessible 'Max' accepts this number of arguments. (rsCompilerErrorInCode)"
The error is here: CInt((Format(Fields!Aeg.Value, "dd")) - 1)
I think you can use a function that accept your Aeg.Value, verify if exists a record at DateAdd(Day, -1, Aeg.Value) with HH > 22 and return a Boolean.
Check this link on how to add code to a report.

Query criteria: from public variable or from form

After a few hours searching, I couldn't find any solution to this little problem I'm having.
I have a query that retrieves one of its criteria from a form. I have referenced correctly the value on the form from the query, and it works, but what I wanted to do is a bit more complicated: when the form is closed, I want to launch the query with a "default value".
I tried to do it in 2 different ways:
a) Defining an "IIf" at the query criteria: I would need a function that checks if the form from which I retrieve the values is open.
b) Defining public variables with a default value, which would be changed from the form: I don't know where/when to initialize the value of the variable.
Does anyone have a better idea on how to do this?
TL;DR: Query gets criteria from form when it's open. If form is closed, query uses default value. HELP!
You can create a VBA function in a module to do this :
Function MyCriterion() As Long
MyCriterion = 1234 ' default value
If CurrentProject.AllForms("MyForm").IsLoaded Then
MyCriterion = Forms("MyForm").MyControl.Value
End If
End Function

Hiding end date in Drupal

I'm creating a calendar module in Drupal. Due to the clients' needs, I need to be able to hide the end date using a boolean variable on the node, saved in a CCK field.
My problem is that I am able to hide it, by hooking into the theme_date_display_range() theming function, but not from within my module. As far as I can see, this is only possible from within the theme. This will mean, that I won't be able to hide the end date without using a certain theme enabling this.
If I then say, I'll use a certain theme and live with that, I'm still not able to see the context in which the mytheme_date_display_range() is called, and I thereby have no way of knowing if the current node wants to show or hide the end date. I could pass it along as a variable, but would there be a better way to do this?
Could I maybe overwrite the date's theming function to use my module instead, and how would I do this, if that was the best/correct way?
Okay, so I think I've found a good solution without using the theming functions at all. I took a closer look at theme_date_display_combination(), which themes the dates. If the end date is not set, it will only display the start date. I hook into hook_entity_prepare_view() and check if both the date and hide endtime fields are present. If so I unset the endtime based on the boolean value.
/**
* Implements hook_entity_prepare_view().
*/
function kw_calendar_full_entity_prepare_view($entities, $type, $langcode) {
foreach ($entities as &$entity) {
if (isset($entity->field_event_date) && isset($entity->field_hide_endtime) && $entity->field_hide_endtime[LANGUAGE_NONE][0]['value'] == 0) {
unset($entity->field_event_date[LANGUAGE_NONE][0]['value2']);
}
}
}
Hope this will help somebody one day...