I have an append query which is tied to the OnClick event of a button on my form. I want the query to run only if the values of either of 2 fields is updated on the form; the 'Location' field or the 'Assigned To' field.
i.e. if the value of the 'Location' field is changed, or if the value of the 'AssignedTo' field is changed, then the query should run.
The query should also run if both fields have been updated.
The query code is below: -
INSERT INTO AssetMovement ( ID, Location, Condition, AssignedTo, AssetTag )
-
SELECT Assets.ID, Assets.Location, Assets.Condition, Assets.AssignedTo, Assets.AssetTag
-
FROM Assets
-
WHERE ID = Forms!AssetsDesktop!txtID;
-
Please help. Thanks.
14 3365
I have done something similar to this before and while it does work, it might not be the best way. Here is what I do. In the form's module, right under the Option Explicit, I declare a variable as an integer, like this: - Option Compare Database
-
Option Explicit
-
-
Dim intEdited as Integer
Now in the form's OnLoad event, I put
In your Location and AssignedTo controls' AfterUpdate events, put
Now, in your Save button's OnClick event, you need to test for the value of intEdited and only run your query if it equals 1. Like this: - If intEdited = 1 Then
-
DoCmd.SetWarnings = False
-
DoCmd.OpenQuery "YourQueryNameHere"
-
Docmd.SetWarnings = True
-
-
intEdited = 0
-
End If
This makes it so that if either or both of your controls gets edited, then your query will run when you click your Save button and won't run if they weren't edited.
Rabbit 12,516
Recognized Expert Moderator MVP
Use the On Current event of the form to store the old values. Then put in a check to compare the old value to the current value before you run the query.
Rabbit 12,516
Recognized Expert Moderator MVP
Looks like I cross posted with Seth. I would actually store the value because they can the edit variable to be set if they change the value and then change it back.
That would be true. I hadn't thought of that possibility.
Hi @Seth and @Rabbit, thanks for the replies. How can I implement Rabbit's suggestion of storing the values and then comparing old and current values?
Thanks again. I'm really learning a lot here.
zmbd 5,501
Recognized Expert Moderator Expert
In the before update event you have access to the control's oldvalue property. Save that to a temp variable.
Rabbit 12,516
Recognized Expert Moderator MVP
The same way Seth recommended. Except you would store the actual value instead of an indicator.
NeoPa 32,557
Recognized Expert Moderator MVP
While Seth's solution may well work, Z's idea of using the .OldValue properties from the two controls in the Form_BeforeUpdate() event procedure makes better sense.
Set a module-level flag with the logic in the Form_BeforeUpdate() event, then test this flag in the Form_AfterUpdate() event procedure and execute the SQL if it is True. Don't forget to set the variable back to False when done.
Rabbit 12,516
Recognized Expert Moderator MVP
I think they only want to insert when they click the button.
Otherwise, if they change it twice, say they made a typo, realized it, and went back to fix it, that would insert two records.
I think that a combination of the ideas here would probably be best. In the form's BeforeUpdate event, compare both fields old values and new values. If in either field the old and new values don't match, then set the flag to 1. Then the button can test for the flag value and run the Append Query if the flag is 1 and then set it back to 0.
Rabbit 12,516
Recognized Expert Moderator MVP
What if they change it, realize it wasn't supposed to be changed, then change it back? That would trigger an append.
They could change it and change it back without saving it and then the append query wouldn't run because the form's BeforeUpdate wouldn't see the change. If they change it, save it, and then change it back, I don't think that there is a way to protect against that.
For example, say that the criteria field 1 had the focus and a value of "Tom", and the user leaned on the keyboard adding an "a" so that the field then read "Toma". As long as the user doesn't hit save before he/she removes the "a", then the control's .OldValue would equal .Value ("Tom") and the flag wouldn't be set to 1 in the form's BeforeUpdate event, which would be triggered by the button's saving the record. However, like I said before if the user accidentally clicks save before the "a" is removed, then Access would have no way to know that one edit was made accidentally while another was made on purpose. I suppose a prompt could be made to confirm the changes to the fields, but again the user could accidentally click "Yes" instead of "No". I don't think that you can fool proof this beyond only checking in the form's BeforeUpdate event.
zmbd 5,501
Recognized Expert Moderator Expert
[{z1}] query which is tied to the OnClick event of a button
[{z2}] query to run only if the values of either of 2 fields is updated
So Let me restate:
Ephi has only two fields to check and wants to fire an update query if and only if two conditions are true:
[{z1}]: the user has clicked the command button
[{z2}]: at least one of the fields of interest is not the same value as upon entry to the current record.
I have a question that needs to be answered: what happens if there is a change and the user hasn't clicked on the update button and the user changes records ( z1 = false, z2 = true )? As we are, the update query will not run.
NeoPa 32,557
Recognized Expert Moderator MVP Rabbit:
I think they only want to insert when they click the button.
Unfortunate. That seems to me to indicate they don't have a very clear idea of the logic involved. I can't imagine a scenario (at least remotely similar to what has been described here) that would make sense to trigger that way. Rabbit:
Otherwise, if they change it twice, say they made a typo, realized it, and went back to fix it, that would insert two records.
This I don't see as a problem. Logging changes rarely requires aggregates to be logged. If there are two changes that cancel each other out then they are both still logged.
I'm thinking it's very hard to arrive at sensible advice for this question due to the lack of clear thinking behind the question itself. I'm not criticising Ephi for that, they seem to have a good attitude generally, I just think they need to give this aspect some thought before it makes sense to proceed. It is possible for there to be a scenario where this question makes sense, but then more details are necessary in the question to explain the scenario fully. As it is there are more questions than answers for it - which is not a good place to work from.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: James |
last post by:
When replacement DVDs are sent in a monthly update, what is the easiest way
to tell if any of the software products I have installed have been
changed/updated on the new DVDs? Or are updates only...
|
by: ralamo |
last post by:
When i execute the following insert query, the above mentioned error
is coming. Anything wrong in the query?
INSERT INTO ECN_1 (Old_SbPartNo, Old_PartDesc, Old_ManPartNo,
Old_Manuf,...
|
by: kathyburke40 |
last post by:
Odd problem. I have a table in the following format:
DocID Question1 Question2 Question3
------------------------------------------------
298 1, 2, 3 or 0
Each Question...
|
by: Tahir |
last post by:
HelpTopic: Import/Restore/Transfer, Only Updated/Changed/New Values
Between Two SQL Servers Over Dialup Connection.
--
Hi All,
I need a little helping hand/Tips:
The Problem & Situation: One...
|
by: rdemyan via AccessMonster.com |
last post by:
Anyone have any ideas on how to determine when the back-end file (containing
only tables) has been updated with new data.
The date/time of the file won't work because it gets updated to the...
| |
by: Kosmos |
last post by:
I'm getting this error message: Number of query values and destination fields are not the same.
This is the code:
INSERT INTO tblDateDifference ( DateDifference )
SELECT tblContracts.EndDate...
|
by: cypriot |
last post by:
Hi. I am developing an application program in java. I use MsAccess for keeping data.
I had a problem with sql insert method.
public void AddPatient()
{
String...
|
by: Cliff72 |
last post by:
Is there a way in VBA to check if a linked table in the database has
been updated?
Example:
I have a table "LedgerTemp" which is a direct link to a text file on
the LAN "Ledger.txt"
This text...
|
by: Fuzz13 |
last post by:
I have a several text boxes available on a form. At the bottom of the form there is a disabled text box for the total. I currently can add all the values upon a button click, however I would like to,...
|
by: adascat |
last post by:
Could anyone find out the problem why this is happening? The number of query values I am trying to insert is exactly the same as in my accdb file actually.
Number of query values and destination...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |