Error in Expression used for Control Source in an Access Form - ms-access

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.

Related

How to calculate the difference between days in a table field

Ok this should be a relatively easy thing to do, yet I'm at the head desk stage trying to figure out the insanity here.
I have a table called tblPersonnel. I'm tracking two document expiration dates in date/time fields called CED and PPED. When I run a query against tblPersonnel I need it to look at PPED, determine if that document is expired and if so use CED instead. I have a few fields in the query that need to use this concept to determine what the output value is, but I am hitting a wall here trying to get the query to spit out the correct value. Here's what I'm using for one of the fields - Document Expiration Date: IIf([PPED]-Now()<0,[CED],[PPED]). What's happening is that the expression is constantly popping as false, so PPED is getting used regardless if it's an expired date or not. Does anyone have any ideas as to what I'm doing wrong here?
I've also tried to set this up as its own field in tblPersonnel, but that's even more aggravating. If I try to set the field to just a text field - IIf([PPED]-Now()<0,"Yes","No"), the formula will accept the use of Now(), but it doesn't like the reference to the other fields in the table. If I set it as a calcuated column, I can reference the other fields but it doesn't like Now(). I'm at a loss here.
If PPED is less than Date(), it is expired. Don't need to subtract. Assuming CED and PPED are just date parts, no time, consider:
IIf([PPED] < Date(), [CED], [PPED])
If PPED could be null:
IIf(Nz([PPED],0) < Date(), [CED], [PPED])
Ok finally fixed it here. I had another issue in that I wasn't accounting for how Access would handle a Null or blank value in PPED. The functioning formula is Document Expiration Date: IIf(Len([PPED])>0,IIf([PPED]<Date(),[CED],[PPED]),[CED]) Thanks to June7 for helping me simplify the expression, as I was using DateDiff('d',[PPED],Date())<0 but their answer is just so much cleaner and quicker to type.

Exporting Data with a Specific Date

Scenario:
I have a single table in access 2007 with few columns and several thousand records which I have imported form a CSV file via a “DoCmd” statement.
What I want:
I want to export these records but on a basis of specific field content and with another column’s date basis. i.e. I want to export the records with the “EQ” ( content of a columns field “SERIES”) and with a date which is one amongst the many dates the column have.
The “SERIES”, I have defined in “Criteria” in my query and it is working fine as the “SERIES” remains the same every day.
Issues:
The problem is with the date that changes every month and I cannot define or hard-code it in anywhere.
Query is working fine with the file where there is no date, but with a date, it is an issue.
Question:
Can we put a user define textbox, where user can define the date and that date will be taken by the query and will return the records with that defined date? In addition, “SERIES” is already defined in query so the result will be exact.
I use the following statement for exporting the data:
DoCmd.TransferText acExportDelim, "NewFnoSpec", "fnoquery",
"C:\Users\welcome\Desktop\Output.txt", True
Using the following 'WHERE' clause as a starting point to select records for one specific date:
WHERE (((Table1.SERIES)="First") AND ((Table1.MyDate)=#4/4/2014#));
You can prompt the user to enter a date by using:
WHERE (((Table1.SERIES)="First") AND ((Table1.MyDate)=[Enter Date]));
If there was some pattern or rule as to the desired date (i.e. first day of prior month, first Monday of prior month, etc.) you could structure the 'WHERE' clause to handle that without a prompt.
Thank you for your reply and an answer. Your answer is quite helpful. I have tried a little easier way and it worked for me. I put a text box named txtexpdate, on the form and in the query ( design mode ), in criteria I have put this :
Like "" & [Forms]![Futures]![txtexpdate] & ""
This is working fine at this juncture. And thank you once again for your efforts to answer my question. Hope this also will help others as an option to this problem.
Regards
Achal

Access VBA filtering SQL by short date on a long date/time field

I am having an issue getting criteria to work. There is a related question on StackO here, but I have tried what's in it and it's still not working, so I'm asking my specific question. Apologies if that's not technically kosher at StackO.
Can't filter MS access datetime field using short date
I have a query that I need to have WHERE criteria based on an associate ID and the current day’s date. But the field that has the current day’s date needs to be a long date and time field, so criteria for short date doesn’t work. I have made this query in query design mode and it does exactly what I want:
SELECT tbl_Data.[#], AssocID, tsUpdated FROM tbl_Data WHERE AssocID = 4441 AND DateValue([tsUpdated])=Date()));
But when I do the equivalent in VBA, which I need to have this happen in, it does not work. Please note, I have used Date in this vba version because according to this website, Date will return current date:
Set FinishReport = CurrentDb.CreateQueryDef("qry_SessionReport", "SELECT tbl_Data.[#], AssocID, tsUpdated FROM tbl_Data WHERE AssocID = 4441 AND DateValue([tsUpdated])=Date”)
I have tried all kinds of syntax, and I have tried the answers from that StackO URL above, but nothing has worked. Any thoughts?
Thanks!
For the querydef, I noticed you don't have the closing parenthesis for the Date() function in the SQL text. In the VBA editor, the function is listed as Date with no parenthesis but if you tried to use the Date() function in a query without the parenthesis Access would throw an error. Hope that helps!

Use a summary field in an expression in SSRS reports

I have the details of my report being summed up in a summary expression, all works fine. The fields are decimal values of hours worked. Thus, the summary value is also a decimal value. I'd like to access the summary value and convert it to hours / minutes. I've labeled the express as "WorkTimeSum", but can't seem to get a handle to it, however. Fields! obviously won't work since it is a summary expression. I was thinking ReportItems! should work, but to no avail. How can I use this expression field (in a summary row) in an expression in the same summary row?
If I've understood correctly, you're asking how to reference the textbox containing the total work hours value so that you can convert it to hours and minutes using an expression in a different textbox?
You can use either ReportItems! e.g.
=ReportItems!Textbox20.Value)
or ReportItems("") e.g.
=ReportItems("Textbox20").Value
to reference the value of another textbox. Be careful with the names as they are case sensitive.
You can use aggregate functions in any expression. For example, in any header rows you can use the following expression to determine the total hours value:
=Floor(Sum(Fields!hours.Value))
Sum(Fields!hours.Value) is just the total hours in whatever context, e.g. the group total if it's a group header row; you can use this expression as an input in any other expression you require.
It sounds like your issue wasn't the conversion itself, so hopefully this points you in the right direction. If you need further information please specify.

Access Report - Show Week Ending Date

I have an Access 2000 report based on a query like this
SELECT
...
FROM Clients AS wc INNER JOIN ...
WHERE ((wo.OfferStatusID)=3) AND
((DatePart("ww",[wo.StatusTimeStamp]))=DatePart("ww",[Enter Week End Date]))
AND ((Year([wo.StatusTimeStamp]))=Year(Date())));
The where clause allows you to enter the 'Week End Date' and it finds all of the records for the Sunday-Saturday week that contains the end date.
My problem is I need to include the Saturday end date on the report so that the header reads '... for week ending 5/9/09' and I can't figure out how to get that date without the query asking me for it a second time.
Is there a way to force Access to return the parameter entered as another field in the results, or another way to get that week ending date?
Continuing to poke around in the query designer I discovered that I could add this to the SELECT clause and get the value entered added to each row:
[Enter Week End Date] AS WeekEndDate
This works, but I am still open to other suggestions.
You could stack two queries (make one the source of the other). This would be pretty MS Access'y. However, if you have it working now, I'd stick with what you have. It's probably cleaner.
Another approach is to use a form to grab the query params first, and reference the form control in the query. For instance, with your example, create a form called frmGetDateParam, and add a textbox called txtDate. Change the format to a date, perhaps add some validation, doesn't matter. Add a command button to the form which opens up the report. Then, change your query to look like this :-
SELECT
...
FROM Clients AS wc INNER JOIN ...
WHERE ((wo.OfferStatusID)=3) AND
((DatePart("ww",[wo.StatusTimeStamp]))=DatePart("ww",Forms!frmGetDateParam!txtDate))
AND ((Year([Forms!frmGetDateParam!txtDate]))=Year(Date())));
You can also reference Forms!frmGetDateParam!txtDate as a field on your report.
The only downside to this approach is that if you try to open the query/report without the parameter form being open, then the query will still prompt you for the date value.