Macros and Programming


CardboxForumsMacros and Programming > "Macros and editing of records"

Macros and editing of records

Has anyone suggestions about how to make macros that do not run when somebody else is editing a record.

Posted By Post

Charles Welling

2-May-2005 14:08

I've made a macro that does some routine jobs and then writes the result to a record. This macro must not run when the record is already being edited. As far is I understand, the macro language is not able to report if someone else is already editing the record.
The macro is activated by a pushbutton that only works when the record is viewed, but the button is only greyed in the active window. Other windows on the same PC or other PC's may still activate the macro, so that does not solve the problem.
Does anyone know a (simple) solution?


5-May-2005 06:19

Does something like this not work?

if ActiveRecord.userediting = true then
  msgbox "Record wordt bewerkt!"
end if
'then here starts the rest macro

If that does not work than this will work I think:

on error resume next
if err.number > 0 then
    msgbox "sorry, record is edited by someone"
end if
'rest van de macro

bert stortenbeker

Charles Welling

5-May-2005 19:12

Sorry Bert, but neither one works. The first one works, but only for the PC that's actually editing the record. So, for instance, when you're editing the record in Window#1 and you activate the macro in Window#2 (or vice versa). Editing done by another user is invisible to the macro.

The second solution doesn't work at all, because editing the record by means of a macro when it's already being edited does not generate an error. The possibility to have several record objects editing the same record is specifically mentioned in the Macro Help. For me, the real trouble starts if:
- another user is editing the record
- a second user starts the macro which makes the changes and thinks it saves them
- the first user saves the record too
In this case the changes made by the macro are discarded, which is of course undesirable.

So, there ought to be a way to communicate with the server and it ask it about the status of the record.

However, while writing this reply, I've discovered an undocumented way to solve the problem. If, instead of "Editrecord" you use the non-existing "Command Editrecord" then the macro will NOT edit the record if ANYONE ELSE is already editing it. I need to do some more testing because the user will not understand why the macro doesn't run. I've not yet been able to produce some error that can be used to trigger a MsgBox. I will report any progress.

Regards, Charles


6-May-2005 19:16

There isn't a way of communicating with the server to ask it about the edit status of a record because it is impossible to get a response you can trust. A fraction of a second after the server has replied "not editing", someone else may start to edit the record... so that the "not editing" answer is untrue even before the questioner has received it.

Charles, Command EditRecord doesn't need documenting, but it doesn't really make sense either. VBScript interprets it as follows:

1. Execute the method EditRecord.
2. Take the value that was returned by EditRecord and convert it to a number.
3. Pass this number to the Command method.

The result will be an error at step 1 if the record was already being edited (EditRecord fails); but if EditRecord succeeds then the result will be an error at step 2, because EditRecord does not return a value and VBScript will discover that converting *nothing* to a number is impossible.

Bert's second solution is correct, except that he assumed that error codes are positive whereas they are usually negative. Here is a corrected version, which I have tested:

Function PossiblyEditRecord
 On Error Resume Next
 PossiblyEditRecord=err.number=0 ' return True if there was no error.
End Function

If PossiblyEditRecord Then
  MsgBox "You are now editing the record."
  MsgBox "Someone else is editing the record."
 End If

Charles Welling

6-May-2005 20:09

Martin, I've fitted your macro into mine and it works perfectly. Thanks! I'm sure it will be of use to many others.

© 2010 Cardbox Software Limited   Home