How to add a record to a table - sql

I would like to create an control on embedded on a continuous form of which its activation event creates a new record on the form and inserts today's date in the date field. I was thinking to use an SQL query to create the record but still am having difficulty.

Maybe you should try this...
In the table being inserted into, add a field called "date_entered" and have its default
value set to "Now()". This will give you a timestamp (knowledge of when the record was entered).
Doing it this way is the easiest way, and then you don't have to write any extra code.

Related

Find most recently edited row in MS Access

In Access VBA, is it possible to find the most recently edited row (Record) in a table?
Example below would be ID 3 as the most recently edited row
Since you are using Access 2010 you can use a Before Change data macro to automatically update a Date/Time field in your table. For example, if you add a Date/Time field named [LastUpdated] then the following macro will update that field whenever a record is inserted or updated:
The advantage of this approach is that it takes place at the table level so the [LastUpdated] field is updated regardless of how the record is inserted or modified. (That is, you don't need to rely on form code to update the field.)
You can add a time stamp field to your table and create a form based on the table. When shown as a table, the form will give you the same functionality as the table itself, but you will be able to set an event AfterUpdate The event can then (over)write the time stamp for that record. You can hide the time stamp control on the form if you like, or set Enabled=False

MS Access Delete query based on combobox

I have a table with a bunch of different fields. One is named period.
The period is not part of the raw data but I run a query when I import new data to the database that gives each record a period.
Now I need a delete query that will delete all the records that have the same period as what is selected in a combobox.
The values in the combobox come from a calendar table that contain all the possible values that could be in that period column at any time.
This is the basic query i thought would solve this issue but it tells me it is going to delete 0 rows every time I run it:
DELETE *
FROM PlanTemp
WHERE PlanTemp.period = Forms![Plan Form]!Combo163;
If you don't need the key field, just remove it.
Look at the "PROPERTIES" section and look at the column names.
Ether remove it there, or from your QUERY source.
You can also look at the Data section of the properties, and change your BOUND column, to Column 2... or whatever holds the data you want to use.

What is same as TIMESTAMP datatype in Access?

For auto date\time we use TIMESTAMP datatype in SQL. What is equivalent to that datatype in MS Access 2007...
There is no exact equivalent in Access.
To clarify, TIMESTAMP in SQL is not always a usable Date/Time, for instance in SQL Server it is deprecated and equivalent to ROWVERSION, which always returns a unique value and it not used to track date and time, even though its value is loosely derived from the current time.
But let's say you want to track changes to records.
In Access, the following ways let you set a field to a DateTime automatically:
First, you can assign =Now as the default value for a DateTime field in a table. This will assign the current time when the record is created (but will not update it automatically when the record is changed).
For recording the current DateTime whenever you make a change, you will have to program that in VBA or through Macros:
When going through a recordset, just update your !ModifiedDateTime or (whatever you called your field) whenever you make a change to a record.
When your table/query is bound to a form, you can let the form update your ModifiedDateTime field by handling the BeforeUpdate event of the form:
Private Sub Form_BeforeUpdate(Cancel As Integer)
ModifiedDateTime = Now
End Sub
If you use a query rather than a table to bind to the form, make sure that the field is present in the query.
In Access 2010 and later, you may also use the new Data Macro, which are the Access equivalent of triggers, to record the current date and time when a record changes.
This is less portable, but would probably be more reliable than using VBA since you don't have to remember to code it whenever you modify a record (Data Macros are handled at the ACE database driver level).
There are tons of articles on how to create audit trails in Access if that is what you are looking for.
DateTime is the only date-based datatype in Access. You would then format the field to either General Date or Long Time to capture down to seconds. I don't think Access gets more accurate than that.

SQL Default Constraint

I have a table with 2 columns dateRecieved and dateDue. I know how to make the dateNow column show today's date by default by using:
DEFAULT GETDATE()
The user can then modify the dateRecieved column to whatever date they want.
After the user modifies the dateRecieved column; how do I set up the dateDue column so that it, by default, shows a date x days away from dateRecieved?
I looked at this answer but I need to refer to another column.
Need to add constraint: date plus 10 days
Is there a way that dateRecieved can modify its value automatically when dateRecieved changes?
The user should be able to override the default values in both these columns.
I'm using the W3 schools sql tutorial but it didn't have and answer I couldn't find one on stackoverflow either
http://www.w3schools.com/sql/sql_default.asp
use current date as default value for a column
Thanks
So when you say the "user modifies" you are talking about an UPDATE.
DEFAULT values are relevant to INSERT statements only.
It is true you can accomplish whatever automagical UPDATE change you want via triggers but as a beginner, I would squarely advise you away from triggers. They are rarely implemented correctly and make understanding when things go wrong more difficult.
What you want to do is INSERT your row(s) (allowing any DEFAULT values to "happen") and then UPDATE your row(s) with the values you intended.
It sounds like what you're looking for is a trigger that waits until the update is finished, then the dateDue automatically updates.
This answer may point you in the right direction:
How to: Create trigger for auto update modified date with SQL Server 2008

Date in a short text data type field... Select query trouble

In my Access database, I have a table called customers. In this table I have a column called DateEntered. The data type for the field is short text.
The values in this column are not coherent - they come in several variations:
MM-DD-YYYY,
MMDDYYYY and
MM/DD/YYYY.
There doesn't seem to be any standard set.
My goal is to select all customers from 2012. I tried
select *
from customers
where DateEntered <('%2013') AND >('%2012');
but it comes up blank when I run it.
Can anyone point out what I'm failing to do correctly & more importantly explain why exactly this query doesn't work in Access? From my understanding of SQL (not very advanced) this should work.
Another variant)
select * from customers where RIGHT(DateEntered, 4) = '2012'
If you have control over the database and application code, the best way to handle this is to use an actual Date field instead of text in the table.
One way to handle this would be to add a new field to the table, write a query or two to correctly convert the text values to actual date values, and populate the new field.
At this point, you would then need to hunt down the application code the refers to this field in any way and adjust to treat the field as a date, not text. This includes your insert and update statements, report code, etc.
Finally, as a last step, I would rename the original text field (or remove it altogether) and rename the new date field to the original field name.
Once you fix the problem, querying against the field will be a piece of cake.
Alternatively, if you can't alter the table and source code, you can use the date conversion function CDATE() to convert the text value to an actual date. Note that you may need to guard against non-date entries (NULL or empty string values, as well as other text values that aren't really dates in the first place). The IsDate() function can be your friend here.
If you have the time and patience, fixing the data and code is the better approach to take, but sometimes this isn't always feasible.
Why don't you use LIKE operators (they're appropriate when you have a pattern using % and _):
select * from customers where DateEntered like '%2013' or DateEntered like '%2012'