Why this simple DATA MACRO is not working? (1 Viewer)

pantera

New member
Local time
Today, 00:03
Joined
Dec 2, 2010
Messages
9
This simple data macro is to update SALARY field everytime the DEPT field is filled with the words PURCHASING or MARKETING. (Delete the space in the link to see the image):

ht tp://uploaddeimagens.com.br/imagem/ver/datamacro2.gif

But it is not working, see the image bellow, where PURCHASING and MARKETING are there, but no changes in the SALARY field (Delete the space in the link to see the image):

ht tp://uploaddeimagens.com.br/imagem/ver/datamacro1.jpg

Seens that the code for data macro is ok. What can be happening? Maybe my Access 2010 is corrupted.
 

Banana

split with a cherry atop.
Local time
Yesterday, 20:03
Joined
Sep 1, 2005
Messages
6,318
How are you firing the data macro? If it's a named data macro, you need to do a Lookup first and qualify the Alias field - Alias is very important.

Also, do you get any "Application Error" message on the statusbar after running the macro?
 

pantera

New member
Local time
Today, 00:03
Joined
Dec 2, 2010
Messages
9
I'm following the lessons from LYNDA.COM video course ACCESS 2010 NEW FEATURES.

The database is attached, already with the data macro. Open the table EMPLOYESS and in the TABLE tab click in the AFTER UPDATE button to see the code.

I tried opening the database in another computer, with another sample of Access 2010, but the data macro still does not work. Maybe I am doing something wrong.

There is no ALIAS in the code, like the Lynda author say to do. Also there is no error message in the status bar, and I am not runing the macro. Just entering data in the DEPT field (PURCHASING or MARKETING). Then, the field SALARY should be filled with 15 or 17, but this is not happening.
 

Attachments

  • Two Trees Olive Oil_04_02.accdb
    884 KB · Views: 362

boblarson

Smeghead
Local time
Yesterday, 20:03
Joined
Jan 12, 2001
Messages
32,059
Which event is the data macro on? I don't have 2010 here at work so I can't check it out directly.

Also, when posting screenshots, please upload them HERE on the forum instead of some picture sharing site (those sites are blocked by my work and many others have that problem too).

See here for simple directions on how to upload them here and display them inline.
 

boblarson

Smeghead
Local time
Yesterday, 20:03
Joined
Jan 12, 2001
Messages
32,059
Oops, I guess you had it in the After Update event, it would appear (based on your post)
 

pantera

New member
Local time
Today, 00:03
Joined
Dec 2, 2010
Messages
9
Thanks Bob!

Pictures attached. The event is AFTER UPDATE. Still data macro is not working.
 

Attachments

  • datamacro1.jpg
    datamacro1.jpg
    26 KB · Views: 1,147
  • datamacro2.gif
    datamacro2.gif
    3.9 KB · Views: 986

boblarson

Smeghead
Local time
Yesterday, 20:03
Joined
Jan 12, 2001
Messages
32,059
Do you have a lookup defined on the department field at table level?
 

pantera

New member
Local time
Today, 00:03
Joined
Dec 2, 2010
Messages
9
Bob, the DEPT field is a TEXT data type. There is no lookup.

Give a look at the database attached please. This database is the EXAMPLE FILE for the LYNDA.COM video course.
 

Attachments

  • Two Trees Olive Oil_04_02.accdb
    884 KB · Views: 374

boblarson

Smeghead
Local time
Yesterday, 20:03
Joined
Jan 12, 2001
Messages
32,059
When I get home I will. I am right now on a bus using my cellphone.
 

boblarson

Smeghead
Local time
Yesterday, 20:03
Joined
Jan 12, 2001
Messages
32,059
Okay, not sure about the After Update event but if you put it in the Before Change event this works:

 

Attachments

  • BeforeChangeDataMacro.png
    BeforeChangeDataMacro.png
    15.4 KB · Views: 8,489

boblarson

Smeghead
Local time
Yesterday, 20:03
Joined
Jan 12, 2001
Messages
32,059
But be aware the changes don't occur immediately upon moving to the next field. It requires the record to be saved first.
 

pantera

New member
Local time
Today, 00:03
Joined
Dec 2, 2010
Messages
9
Thanks Bob!

Yesterday night I tried the same thing, using the BEFORE CHANGE event, and it works. I don't know for what reason it is not working with AFTER UPDATE, like occurs in the Lynda video, but that is ok.

Do you know a good book about Access 2010 that you could recommend? An intermediate book, not so large like somes I have seen, with about 1800 pages (!), and also not so small like books for novices.

Thanks very much, I appreciate your help.
 

AccessJunkie

Senior Managing Editor
Local time
Yesterday, 20:03
Joined
May 11, 2006
Messages
278
Hi Pantera,

Yesterday night I tried the same thing, using the BEFORE CHANGE event, and it works. I don't know for what reason it is not working with AFTER UPDATE, like occurs in the Lynda video, but that is ok.

I'm not familiar with the video you mention, but I believe it could be incorrect depending upon how the information was presented. We actually made a late change to the product in this specific area of data macros during the Beta phases. It's very possible that Lynda was using an older Beta version of the product and the specific example will not work in the released Access 2010 product.

Back to your questions concerning this space. Here is some information which I hope will help you understand what is going on. Based on what you are trying to do, you really should be using the BeforeChange event instead of the AfterUpdate event for data macros. Why? Good question. Here's a good rule of thumb that will be easy to remember:

If you need to edit some data in the *same* record based on values in other fields in the same record, you really should use the BeforeChange event.

Essentially in your case you want to change the value of the Salary field based on what Department is entered. So is it possible to use AfterUpdate for this? Yes it is, but I don't recommend it for several reasons.

1. It is inefficient. If you use the AfterUpdate event, you have to commit your record changes (the Dept change) and then you're asking Access to go change the same record yet *again* and update the Salary field. That's two passes on the same record when one is more efficient. With BeforeChange, you can do this type of checking and editing all at once and make one record save instead of two.

2. You're way more likely to fall into the trap of data macro recursion - the data macro logic running over and over again. For example, if you do your scenario in the AfterUpdate event, here's what would happen if you weren't paying attention to your logic:
- You make a change to the Dept field. Record gets saved.
- AfterUpdate fires now. Access looks at Dept field and writes the value you want to Salary field. Record is saved again.
- AfterUpdate fires yet again. Why? Well you just had it change the Salary field remember? Salary field gets written again based on your logic and record is saved for a third time.
- AfterUpdate fires yet again and again and again....
- Access repeats this loop for 10 times and then bombs out (stops any more loops) because it will be a never ending cycle.

So in order to prevent the data macro recursion loop, you would have to use the "Updated" function to determine if the Dept field has changed so it doesn't keep running the same AfterUpdate event over and over again.

3. For your scenario, you would also have to write some duplicate logic in the AfterInsert or BeforeChange to do the same thing. Why? As written, if you just had this logic in the AfterUpdate event, your salary field won't get updated when you enter a new record.

One debugging tool you should really take advantage of with data macros is the USysApplicationLog table. Do you know how to use that?
When running your original tests, you should see the words "New Application Errors" displayed in the Status Bar. This means Access ran into one or more problems executing your logic. If you click those words in the Status Bar, Access opens this log table where you can examine the errors. You can also open this table from the Backstage View. Click the button labeled "View Application Log Table" in the Backstage view.

If you look at your original error, you would have seen this text:
"EditRecord failed because the default alias represents a record which is read only."

This was the late change we made to the product for the AfterUpdate event.

Utilize this log table when debugging and working with your data macros.

So as already mentioned, use the BeforeChange event as suggested by the other responders. In case you're curious, here is how you can make it work in the AfterUpdate, *but* I don't recommend it and it won't cover new records. See attached screenshot.



I hope that helps clarify things a bit for you.

--------------------
Jeff Conrad - Access Junkie - MVP Alumnus
SDET II - Access Test Team - Microsoft Corporation

Author - Microsoft Access 2010 Inside Out
Co-author - Microsoft Office Access 2007 Inside Out
Access 2007/2010 Info: http://www.AccessJunkie.com

----------
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.mspx
----------
 

Attachments

  • AfterUpdate.png
    AfterUpdate.png
    16.4 KB · Views: 8,166

pantera

New member
Local time
Today, 00:03
Joined
Dec 2, 2010
Messages
9
Thanks very much, Jeff! :)

I agree with you, and will use the BEFORE CHANGE event to make change in the same record.

The Lynda video probably has been released with a beta version, like you say. I will write to the author, Alicia Pollock, telling her about the changes you mention in the final version.

Can you recommend a good intermediate book or video course about Access 2010?
 

pantera

New member
Local time
Today, 00:03
Joined
Dec 2, 2010
Messages
9
Jeff, after posting my reply I visited your links. With high probability, I will buy your book Microsoft Access 2010 Inside Out.
 

AccessJunkie

Senior Managing Editor
Local time
Yesterday, 20:03
Joined
May 11, 2006
Messages
278
Hi Pantera,

I'm glad to hear my information was helpful to you.

Do you know a good book about Access 2010 that you could recommend? An intermediate book, not so large like somes I have seen, with about 1800 pages (!), and also not so small like books for novices.

Who would write 1800 pages on Access 2010??
Oh wait.....never mind....
:)

Can you recommend a good intermediate book or video course about Access 2010?

There are several good books out there covering Access 2010. Each has their own strengths and weaknesses. It really all depends on what subject matter you want to learn more about. Some books cover certain topics in more detail.

Jeff, after posting my reply I visited your links. With high probability, I will buy your book Microsoft Access 2010 Inside Out.

Thanks. If you do, I'm sure you'll find lots of valuable information to assist with growing your general Access knowledge, as well as specific 2010 features. My book is designed to cover just everything so as such, it is a pretty hefty book. However, the chapters are organized such that you can zero in on the specific topics and features you'd like to learn more about. For example, data macros are covered in Chapter 7. There is also lots of extra material that serves as reference material, such as expressions, macros actions, etc.

Good luck with your Access studies.

--------------------
Jeff Conrad - Access Junkie - MVP Alumnus
SDET II - Access Test Team - Microsoft Corporation

Author - Microsoft Access 2010 Inside Out
Co-author - Microsoft Office Access 2007 Inside Out
Access 2007/2010 Info: http://www.AccessJunkie.com

----------
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.mspx
----------
 

Users who are viewing this thread

Top Bottom