MS Access db, VBA form dynamic Default value on condition - ms-access

I'm using form below and not sure if I can implement this logic:
Start Date = Now();
If Expedited then Exp_Date = Start_Date + 72 hours
else Exp_Date = Start_Date + 30 days.
I used this expression for default but looks like at time of evaluations it doen'st care about value in ExpeditedYN.
=IIf([ExpYN]=True,DateAdd("h",72,Now()),DateAdd("d",30,Date()))
Ideally I'd like to flip Exp.Date default clicking on Expedited box, is this available in VB. Tried DLookUp (D --> Dynamic??) and it doesn't work
DLookUp(IIf([ExpYN]=True,DateAdd("h",72,Now()),DateAdd("d",30,Date())))
Tx
Dai

Using IIf or DLookup to refer to another field on the same record in a Default Value isn't going to get you anywhere, because Access only asks Default Value what value to fill in when the new record is first initiated. That means it's going to be based on the default values of the other fields, and therefore will not respond to any user input.
I'd suggest that you keep the Default Value of both StartDate and DeadlineDate simple, similar to how you already are:
StartDate's Default Value: Now()
DeadlineDate's Default Value: DateAdd("d",30,Now())
And then change it in code if and when Expedited is changed. There's a few ways of doing this, I'd probably put it in the Before Update event of the checkbox, so it runs without having to move out of the record. You could also base it on the StartDate field if you might want to be able to change the record later on. You could build it with Access' macro builder, but it's easier to show you VBA here:
If ExpYN Then
DeadlineDate = DateAdd("h", 72, StartDate)
Else
DeadlineDate = DateAdd("d", 30, StartDate)
End If
If you want the expedition process to be based on the moment the button is clicked instead of the DateTime data in StartDate, just change StartDate (or whatever that field is called) to Now().

DefaultValue triggers only once - when record is first initiated by input into any field. If the calculation is based on value of Expedited, the DLookup should not work because the record is not yet committed to table. Record is committed when: 1. close table/query/form or 2. move to another record or 3. run code to save. If you want to programmatically modify DeadlineDate value based on Expedited value, then code needs to be in Expedited AfterUpdate event and abandon the DefaultValue approach.
Me!DeadlineDate = IIf(Me!ExpYN, DateAdd("h",72,Now()), DateAdd("d",30,Now()))

Related

SSRS Time expression filter not working with Parameter boolean value

Background:
Trying to achieve a filter in dataset, if the user selects Day shift from the parameter, any data entry between 06:00:00 and 18:00:00 is filter in the report. Furthermore, any data entry between 18:00:00 and 06:00:00 is reflect in the report for Night shift
Process:
I have got a parameter with 2 Boolean values "True" and "False". I have labeled those values as "Day shift" and "Night shift" in the available value Parameter window option. Default value is true.
Converting datetime to time, in the dataset query
SELECT
CONVERT(time, SWITCHOFFSET(CONVERT(datetimeoffset, LastModified),DATENAME(TzOffset, SYSDATETIMEOFFSET()))) as ShiftTime
FROM table abc
Filter expression:=Fields!ShiftTime.Value
Operator: in
Value: =IIf( ( TimeValue(Fields!ShiftTime.Value) >= "06:00:00" And TimeValue(Fields!ShiftTime.Value) <= "18:00:00" ) , Parameters!ShiftType.Value(0), Parameters!ShiftType.Value(1) )
Problem: [BC30516] Overload resolution failed because no accessible
'IIf' accepts this number of arguments
Not sure which part I am going wrong with, I am thinking it is the datatype but unsure.
Solution which I built on
I'm not sure what report designer you are using, but using the Report Designer extension for Visual Studio gives me very different errors.
And it gives errors both in the output window, and the expression window when building it.
Firstly to debug this, add it as a column to your report before you try and implement it as a filter.
Then check the relevant documentation for the function. It turns out the TimeValue function takes a string, not a DateTime, and returns a datetime.
Then, you can't access the available parameter values from the parameter, you can only access the selected parameter values. So instead of Parameters!ShiftType.Value(0) (which has a red line under it) you should just have true (or false depending on which way around your logic is) i.e. the value of the parameter.
Finally, I don't know what implicit conversion SSRS does from strings to a time, so I would do an explicit time compare instead of a string compare. Given we know that TimeValue returns a DateTime with the minimum date value and the time component we can compute the required limites for day/night shift.
The following is what worked for me:
=IIf(TimeValue(Fields!ShiftTime.Value.ToString()) >= DateTime.MinValue.AddHours(6) And TimeValue(Fields!ShiftTime.Value.ToString()) <= DateTime.MinValue.AddHours(18), true, false)
And actually re-visiting it, thats way too convoluted, you are already returning a time value which we can compare directly as
=IIf(Fields!ShiftTime.Value >= new TimeSpan(6,0,0) And Fields!ShiftTime.Value <= new TimeSpan(18,0,0), true, false)
And then your filter should be:
Filter expression: {as shown above}
Operator: =
Value: =Parameters!ShiftType.Value
Note: when you say "between" I don't know whether the intention is to include both 6am and 6pm within the day shift window, but I haven't modified your logic in that regard.

Are SSRS parameters fixed while generating a report?

I have been using the following code within my SQL queries:
DECLARE #CurrentDate DateTime = Getdate()
I then reference #CurrentDate instead of using the Getdate() function multiple times within the same query. This is useful because the value of #CurrentDate doesn't change during execution of the query.
However, I have started converting some of these variables to parameters set by SSRS. This allows the user to change the parameter #CurrentDate which is useful for various reasons. Typically the default value is set to "=Now()" or some expression containing the Now() function.
At what stage are these parameters calculated and is there a "correct" way to calculate them if I want parameters to be consistent with one another?
I'm trying to figure out if I should have one parameter for the current date and then reference that in the calculation of my other parameters or if this will produce the same inconsistencies (or worse inconsistencies) as simply using Now() within the expression for each parameter.
It all depends on what you mean by "consistent". Take these two scripts for example:
--Method #1
UPDATE tracker.shipment SET delivery_date = GETDATE() WHERE delivery_id = 1;
WAITFOR DELAY '00:00:01';
UPDATE tracker.shipment SET delivery_date = GETDATE() WHERE delivery_id = 2;
--Method #2
DECLARE #current_date DATETIME = GETDATE();
UPDATE tracker.shipment SET delivery_date = #current_date WHERE delivery_id = 1;
WAITFOR DELAY '00:00:01';
UPDATE tracker.shipment SET delivery_date = #current_date WHERE delivery_id = 2;
This is a pseudo script to update a tracking system to show when delivery was made. I would argue that the first version is more "consistent" as it records when the update was actually made, and not an arbitrary date/ time from when the stored procedure was first entered.
Let's pretend that there's another system constantly checking for delivery items that have a delivery date added, then makes the actual delivery, or that the code immediately before each delivery update does something to trigger the actual delivery. In this case it would be much less consistent to use the second method, as this would record deliveries as being made for some time in the past, before they actually were delivered.
As far as I am aware the parameter sent by SSRS will be evaluated to determine the server time on the reporting server at the point where the user clicks the "View Report" button. So every parameter that has Now() will end up with the same date time, but this might be slightly out from the SQL Server if your servers aren't exactly in synch.
Does this actually matter? Well that depends entirely on what you are doing with the date/ time you are passed I guess. When your users enter a custom date/ time are they entering a date or a full date/ time? I imagine they only enter a date, so this is automatically converted to having a 00:00:00.00 time component?
Basically I would need more context to be able to give a fuller answer to this.
Just create a datetime SSRS parameter and set the default value to NOW(). Then it will assume everything, but users can opt for a different date. Note also that SSRS uses DATETIME parameters, but for a lot of my reports I actually use a string parameter field which I CAST to DATE to avoid losing data between hours that the user isn't expecing:
WHERE CAST(StartDate AS DATE) > CAST(#StartDateParam AS DATE)

Error in Expression used for Control Source in an Access Form

I actually have two questions.
I'm a beginner user of Access, still trying to get a good understanding of the software. I'm trying to create a database for a library (School project) with a borrowing out system. I have two fields in a table called DueDate and DateHired. The DueDate functions on the expression =Now()+28 and the DateHired function on the expression =Now(). Basically making the due date 4 weeks ahead of when the book was hired. My first question is quite simple; if I were to input a record today, would the two DueDate/DateHired fields remain the same date and time by tomorrow? Or would they update to the Now() of tomorrow?
My second question is something regarding an expression. I'm trying to make an Overdue checkbox. The premise is that if Now()>DateDue then the Checkbox will be 'Yes'. My current code in the ControlSource part is this:
=CBool([DateDue]
However, the checkbox simply displays '#Error' and not Yes/No. I'm also concerned that if the answer to the first question was '=Now() stays the same after the record is added and doesn't update' that would also mean the Overdue function would not really work unless you were inputting the record after the due date. Which would make no sense. Any suggestions?
Cheers.
This is relation to your second question. You can ask a separate question for the first part.
=CBool([DateDue]
What you are trying to do here, is convert a Date data type to a Boolean (you're missing the closing parentheses by the way) which is impossible.
What you should be doing is check if the due date is less than today and return the appropriate True/False value.
IIf([DueDate] < Date(), True, False)
Which means:
IIf("Is due date in the past?", "Yes, it's overdue", "No, it's not overdue")
You can read more about the IIf function here.
Indeed as a beginner, make it a habit to use the date functions. Later you can turn to "smarter" methods which, by the way, often aren't that smart.
1.
If you store a date, it remains unchanged in the table. And don't use Now unless you specifically need the hours too:
DateDue = DateAdd("d", 28, DateHired)
or in a query - using the designer:
DateDue: DateAdd("d",28,[DateHired])
or as a ControlSource for a textbox on your form:
=DateAdd("d",28,[DateHired])
2.
You can use DateDiff for this:
Due = DateDiff("d", DateHired, Date) > 28
or in a query - using the designer:
Due: DateDiff("d",[DateHired],Date()) > 28
or as a ControlSource for a textbox on your form:
=DateDiff("d",[DateHired],Date()) > 28
and set the Format property of the textbox to, say, Yes/No or something else meaningful for your users.

SSRS Date Parameters - Setting Date B = Date A whenever Date A is changed

I am looking into a query from one of our users regarding the behaviour of date pickers on their report.
They have asked that when they enter a date in Date Paramater A that this is then duplicated in Date Parameter B.
I can achieve this when the report is first run by given Date Parameter A no default value (so it has to be chosen by the user) and seet Date Parameter B's default value via an expression to "=Parameters!StartDate.Value".
The question though is wether or not I can recreate this when Parameter A is updated. Therefore if they run the report once and then decide they need to choose another date. Can I set Date Parameter B to refresh each time Date Parameter A is changed?
E.G
Report is opened
Date Parameter A is set to 02/12/2013.
Date Parameter B now defaults to 02/12/2013.
Search is performed
A second search is required so, without closing the report the user changes the date in Date Parameter A
Date Parameter A is now set to 05/12/2013
Date Paramater B still says 02/12/2013 - can I somehow make this auto refresh to match Date Parameter A if Date Parameter A changes?
EDIT: Thanks to Kalim for pointing this out but it must also be noted that although I would like Date Parameter B to default to the new value selected by Date Parameter A, dates greater than that selected for Date Parameter A must also be available in case they wish to widen the range of dates.
Hopefully that is clear, but if any further information is required then please let me know.
Thanks in advance for your time and help.
You should be able to set the "Available Values" to the value of the first date parameter.
Select "Available Values" then select the "Specify values" option. Add a value and edit the expression of the value. Set this to the same expression you used in your default value expression.
Hope that makes sense!
Screenshot:

Report Builder 3.0 Updating Parameter Panel

I have a report that has StartDt, and EndDt as parameters. When a user leaves these blank, I default StartDt to yesterday, and EndDt to today. That works fine the the actual parameter send to SQL. But is there a way to update the SSRS Parameter Panel to show the user what dates were defaulted?
I know I could just make the parms required, but I'd rather default the dates like this so users can just put in there account(s) and move on.
Set the default value expressions for each date parameter using the standard date/time functions.
You can set today as:
=Today()
Yesterday as:
=DateAdd(DateInterval.Day, -1, Today())
See How to: Add, Change, or Delete Default Values for a Report Parameter for more details.
Edit after comments
Say you have the following parameters:
StartDt and EndDt are just set up as Date/Time:
Set the Default Values expression for each parameter using the expressions above, i.e. =Today() and =DateAdd(DateInterval.Day, -1, Today()):
Now, when you load the report for the first time the two parameters are already populated with the Default Values:
Users can then just leave the dates as the defaults as they're already set, or change them as necessary.