473,499 Members | 1,572 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Run query only if values of either of 2 fields have been updated/changed

11 New Member
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:
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO AssetMovement ( ID, Location, Condition, AssignedTo, AssetTag )
  2. SELECT Assets.ID, Assets.Location, Assets.Condition, Assets.AssignedTo, Assets.AssetTag
  3. FROM Assets
  4. WHERE ID = Forms!AssetsDesktop!txtID;
  5.  
Please help. Thanks.
Feb 12 '13 #1
14 3365
Seth Schrock
2,965 Recognized Expert Specialist
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:
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Dim intEdited as Integer
Now in the form's OnLoad event, I put
Expand|Select|Wrap|Line Numbers
  1. intEdited = 0
In your Location and AssignedTo controls' AfterUpdate events, put
Expand|Select|Wrap|Line Numbers
  1. intEdited = 1
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:
Expand|Select|Wrap|Line Numbers
  1. If intEdited = 1 Then
  2.     DoCmd.SetWarnings = False
  3.     DoCmd.OpenQuery "YourQueryNameHere"
  4.     Docmd.SetWarnings = True
  5.  
  6.     intEdited = 0
  7. 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.
Feb 12 '13 #2
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.
Feb 12 '13 #3
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.
Feb 12 '13 #4
Seth Schrock
2,965 Recognized Expert Specialist
That would be true. I hadn't thought of that possibility.
Feb 12 '13 #5
ephi
11 New Member
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.
Feb 12 '13 #6
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.
Feb 12 '13 #7
Rabbit
12,516 Recognized Expert Moderator MVP
The same way Seth recommended. Except you would store the actual value instead of an indicator.
Feb 12 '13 #8
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.
Feb 15 '13 #9
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.
Feb 15 '13 #10
Seth Schrock
2,965 Recognized Expert Specialist
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.
Feb 15 '13 #11
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.
Feb 15 '13 #12
Seth Schrock
2,965 Recognized Expert Specialist
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.
Feb 15 '13 #13
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.
Feb 16 '13 #14
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.
Feb 17 '13 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

3
1159
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...
2
8119
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,...
3
1713
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...
0
1096
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...
8
1919
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...
9
3346
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...
2
5885
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...
10
4802
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...
3
6298
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,...
3
3994
adascat
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...
0
7130
marktang
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,...
0
7007
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...
0
7220
jinu1996
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...
0
7386
tracyyun
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...
0
5468
agi2029
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,...
1
4918
isladogs
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...
0
3098
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...
0
1427
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 ...
0
295
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.