After Insert data macro not running - ms-access

I have a table tblEmp with a After Insert data macro defined. However, the event is not working. what I am missing here?

Simply move your logic to the BeforeChange trigger which is the state before any record (new or existing) is saved. Your logic attempts to change field values in AfterInsert which is after the save mode. Also, be sure to not include the table identifier, tblEmp, in referencing column name in macro:
In fact, had you clicked the Application Errors on status bar in lower right after your current attempt, the outputted system table indicates the issue since you are in read-only mode after inserting:
EditRecord failed because the default alias represents a record which
is read only.

Related

Trying to track changes in my MS Access Database

I have a table set up to track when people place an order (Order_Tbl). I duplicated the table that I call my order_change_log and added a date/time field (default value set to now). I have a one to many relationship between the orderID and order_change_logID. The idea is that before update, I want the existing data to be inserted into the change log table. I went into the before update field and made the following statement:
CurrentDB.Execute “INSERT into Order_Change_Log SELECT * FROM Order_Tbl WHERE ChangeLog_ID =“”” & Me.ID & “”””
I keep getting “Invalid Outside Procedure” and I’m getting frustrated… Not sure what I’m doing wrong.
What you want to do is called a trigger. In access a table's triggers can be accessed and created from the table's design mode via [Create Data Macros] on the ribbon. You are pretty much forced to use the macro language to create the trigger.
As an aside it looks like you are setting the default date in Order_Change_Log. Leave that default blank as it is not needed here and in many other cases leads to bugs.
In this case we will be using the after-update macro. The Before-Update values are available using [Old]
For an older example of a trigger happening after Delete see MS Access trigger?
Example Table and Relationships:
After Update Trigger Macro:
Use the ribbon to save the macro and you are done. change some values in the Order_Tbl then refresh or open the Order_Change_Log table to see the results

How can I insert a new record in Access using a trigger?

Bearings first...
Microsoft Access.
Version? Unsure. For that I see
"Microsoft Office 365 Pro Plus" and
"Access 2007 - 2016 file format"
I'm a MS Access novice, but rather good at relational DBS (Postgres, MySQL, etc...) in which I've created triggers and their companion stored prodecures.
I'm trying to effectively create an after update trigger for a table. I want the trigger to insert a record in a different table with values that I can either echo or customize based on values in the table that was just updated.
In the "table" tab, "after update", this is what I'm defining...
If [Old].[est_mandays]<>[est_mandays] Then
Create a Record In ajax_hist
Alias ah
SetField
Name ah.est_mandays
Value = [Old].[est_mandays]
SetField
Name ah.id
[Old].[id]
End If
As you can see, I'm just echoing those 2 values in the ajax_hist table.
It seems to swallow this OK as far as syntax. At least I don't get any errors. But when I change the value of est_mandays for a record in the table which has the trigger, no record is inserted in the ajax_hist table. No messages of any kind, error, warning or otherwise.
I "saved" the table after the update in an attempt to force the change (just in case). No difference.
Any ideas what I'm doing wrong ?
More importantly, is there a way to debug this (a log file or debug mode or something that tells me the trigger was actually fired ?)
Thanks in Advance!
Get rid of the "[Old]." where setting values.
Thanks to Erik von Asmuth for the tip on USysApplicationLog which gave me the clue I needed.

Saving new records filtered by "unique" in mysql

I have a query that runs everytime a user logins. Since this query regards information the user might have third-party updated recently I thought it would be a good idea to turn the user_id + information combo in the table unique. As so, everytime a user tried to save new information it would only save the one information I already didn't have. So, the first query being
INSERT INTO table VALUES ("1","cake"),("1","pie"),("1","bedsheets")
And as the user logins a second time and it being
INSERT INTO table VALUES ("1","cake"),("1","pie"),("1","bedsheets"),("1","chocolate")
It would only save ("1","chocolate") because (id,info) being an unique pair all other would not be inserted. I came upon the realization they all fail if only one fails. So my question is: is there any way to override this operation? Or do I have to query the db first to filter the information I already have? tyvm...
When you use the IGNORE Keyword, so every errors, in the execution are ignored. Example: if you have a duplicate or PRIMARY key error while executing a INSERT Statement, so it will ignored and the execution is not aborted
Use this:
I NSERT IGNORE INTO table VALUES ("1","cake"),("1","pie"),("1","bedsheets"),("1","chocolate");

AUTOINCREMENT not automatically adding values in Mysql Workbench IDE

I am learning to use MYSQL-Workbench for inserting data into tables
My Problem::
I have two columns one for Sl_no and one for Name
Sl_no is the primary key which has auto increment checked as shown in
snapshot-1
If you look at Snapshot-2 auto_increment is not automatically
incremented when i keep adding the values
should i need to enable any option for this ?
Because Using the IDE should automatically add values(Since Sl_NO has
autoincrement enabled) but it is not doing so
I have to manually add it one after another
Snapshot-1
Snapshot-2
How can i resolve this ?
{EDIT}
I am using Windows-7 OS
As you can see i am clicking the marked area in the pic to apply
changes, but nothing is happening.
Should i need to enable any settings
UPDATE: It wasn't apparent right away that you're doing this from model and not directly in your db table.
To create your schema from model you need to do Forward Engineering and in the dialog choose Generate INSERT Statements for Tables.
Then go to the object browser in your MySQL connection, not your model. Fire select statement in query window or choose from context menu Select Rows - LIMIT 1000 and you'll see that your sl_no column is populated as expected.
BTW both mac and windows interfaces are identical.
Original answer:
You forgot to commit your changes by clicking Apply
And after reviewing the change script click Apply again.
After that you'll see your auto_increment column values populated

table locked or in use when calling RunSQL

I have some code which re-arranges some items on a form, but only one SQL query. All my tables aren't locked before the code runs but for some reason I get an error when running:
DoCmd.RunSQL ("Select * Into MasterTable From Year07 Where 'ClassName' = '7A'")
Error:
The database engine could not lock table because it is already in use by another person or process. (Error 3211) To complete this operation, a table currently in use by another user must be locked. Wait for the other user to finish working with the table, and then try the operation again.
Any ideas what I can do to stop the table being locked?
Is MasterTable included in your form's Record Source? If so, you can't replace it, or modify its structure, while the form is open.
Apart from the table lock issue, there is a logic error in the SELECT statement.
Where 'ClassName' = '7A'
The string, ClassName, will never be equal to the string, 7A. Therefore your SELECT can never return any records. If ClassName is the name of a field in your Year07 table, discard the quotes which surround the field name.
Where ClassName = '7A'
I'm guessing, but if you're using a form that is bound to MasterTable, you can't run a query to replace it with a new MasterTable while you've got it open in the form.
I would suggest that you get rid of the MakeTable query (SELECT INTO) and instead use a plain append query (INSERT). You'll want to clean out the old data before appending the new, though.
Basically, a MakeTable query is, in my opinion, something that does not belong in a production app, and any process that you've automated with a MakeTable query should be replaced instead with a persistent temp table that is cleared before the new data is appended to it.
I have seen this when you re-open a database after Access has crashed. Typically for me a reboot has fixed this.
What version of MSAccess? Not sure about newer ones, but for Access 2003 and previous, if you were sure nobody was in the database, you could clear up locks after a crash by deleting the .ldb file.