calculate date expression and update table in access - function

I have a form based on a table.
I have a purchase_date field and an expiry_date field.
I want expiry_date to add 6 months to purchase_date (in my form) and then insert this value into the table.
I have created this expression: DateAdd("m",6,[purchase_date]) as the control source of expiry_date. This works as expected but this value is not being updated/ saved from the form into the table. I have tried using onClick and afterUpdate functions to run the expression but no joy.
Thanks for any help.

Have you tried OnSave? Your dateadd syntax seems ok. Watch out for any protected cells/sheets! You can generally use activesheet.unprotect if this is the case.
Also, activesheet.calculate might help you, if correctly placed in your VBA.

Related

Autofill a field based in a Data Field in a Table

Hope you're doing well. I have a question in Access. I have an Employee Table with some Fields and 2 of them are 'EndofContract' and the other is 'Condition'.
The EndofContract field is in date format (is the date in which the employee stopped working in the company) and what I want do to is autofill the Condition field with Inactive or Active, based on the date of EndofContract. Basically:
if EndOfContract is blank I want to show Active in Condition;
if EndOfContract has a date that is previous to today's date, I want to show Inactive in Condition
I've tried various different things and the last one was going to Design View » Condition Field » Lookup » RowSource and in the query designer I wrote this condition IIf(Date()>[EndofContract];"Inactive";"Active") but it doesn't work. Already tried an AfterUpdate macro but I can't understand how to change the value of every record.
Hope somebody can help me and thank you!
Have a textbox on your form where you display the data.
Assign this expression as its ControlSource:
=IIf(Nz([EndofContract],Date())>=Date(),"Active","Inactive")
or, if semicolon is your list separator:
=IIf(Nz([EndofContract];Date())>=Date();"Active";"Inactive")
Or, remove the field Condition from the table and use a query:
Select
*, IIf(Nz([EndofContract],Date())>=Date(),"Active","Inactive") As Condition
From
EmployeeTable
If EndofContract is text, not a true DateTime value, try this:
Select
*, IIf(Nz(CVDate([EndofContract]),Date())>=Date(),"Active","Inactive") As Condition
From
EmployeeTable
This requires no updates.

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.

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

Showing Date in a specific format

I need to make the date format that I entered into the database look exactly the same as when i view it. Eg. Currently my format is 2013-12-21, but when i view it, it shows 12/21/2013. It's quite confusing because when I edit the date in my form, it's in the 12/21/2013 format and the database wont accept it when i change the date to something like 12/23/2013. Please help. Thanks
(btw, it just auto formatted my date. I didn't even use the <%formatdatetime%> function.)
EDIT: Sorry for the lack of information guys. This is what's happening.
1. I created a form to add date amongst othes to mysql. Eg. Purchase date, item, etc (sql wants it in yyyy-mm-dd format and that's how i enter it.
2. created a view list to select which rows i want to update. (Used
<%=Formatdatetime(f_purchasedate,2)%>
to show date only without time as it was 12/21/2013 12:00:00 AM)
3. created an update form.
Now the problem is it shows the date as 12/21/2013 instead of 2013-12-21. So when i submit the form after altering other fields, it says date error. I have to manually type the format 2013-12-21 for all my dates before i can submit the form.
i'm guessing it has something to do with this line of mine.
purchasedate.Text = ODBCdataset.Tables("tbl_vm").Rows(0).Item(2)
tried this but it doesnt help either...
formatDateTime('purchasedate').Text.ToString("yyyy-MM-dd") = ODBCdataset.Tables("tbl_vm").Rows(0).Item(2)
You can use DATE_FORMAT(date,format)
Select DATE_FORMAT('Your Date Value'),%Y-%m-%d) as Date from table_name
Or
You can pass the value to a variable like below
purchasedate.Text = CType(ODBCdataset.Tables("tbl_vehiclemanagement").Rows(0).Item(2).ToString, DateTime).ToString("yyyy/mm/dd")
Dim date As String = DateTime.Now.ToString("yyyy/mm/dd")

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.