# Can an embedded macro be copy and pasted?

Hi, I have found a macro that seems to do exactly what i want it to do in an example database. I tried to copy, paste and edit to suit my requirements. The macro does not seem to run when called for. The macro also has references to source.model.parent. The macro is embedded and is shown below, how can I use this in my database?

Sub Refresh_MainForm (oEvent As Object) 'List Box > Execute (event)
oForm = oEvent.Source.Model.Parent 'MainForm
oEvent.Source.Model.commit() 'commits the selected List Box value to the Form only
'   IF oForm.isnew THEN oForm.insertRow() ELSE oForm.updateRow() 'handles both cases to avoid errors
oForm.updateRow() 'commits the List Box Bounfield; only oForm.updateRow() is required in this case due to Form filter criteria ID=0
oForm.getByName("SubForm1").getByName("listboxClass").refresh 'necessary to update the List Box filter (listboxClass") on SubForm1
End Sub

Sub Refresh_SubForm1 (oEvent As Object) 'List Box > Execute (event)
oForm = oEvent.Source.Model.Parent
oEvent.Source.Model.commit()
oForm.updateRow() 'MainForm current record update
End Sub


(Slightly edited for readability by @Lupp )

edit retag close merge delete

Did you assign the Sub to an appropriate ListBox event? Are the DB and the Form the ListBox is contained in of same structure as far as needed for the Sub to make sense?

(Ideally the Sub should test itself for applicability if made available in contexts it was not originally written for.)

( 2018-04-06 13:45:49 +0100 )edit

Hi, i need to look into this. I tried to get thins working several times but when i noticed the embedded reference I stopped as this is something i had not seen in other macros that i have copy, pasted and edited. I assumed that the embedded reference meant that this was not going to work for me?

( 2018-04-09 10:37:32 +0100 )edit

Sort by » oldest newest most voted

Hello,

After a little research (and questioning just why you would use that code) it seems you got it from here -> Students2 sample.

You really need to understand what code does before you just plop it into your .odb. Even if you don't fully understand, lines such as oForm.updateRow() should indicate a record is being written.

This routine & sample use record #0 as a form filter. This is then used to refresh the sub form. If your system is not set for this, problems will occur & data can be lost. Better to use a separate table when using it for filtering.

You also seem to question source.model.parent which is another way of accessing the form and/or controls. From oEvent you can tell what control initiated the event, get model (see -> The Model-View Paradigm) and the next level up - the form. Fairly basic stuff you need to understand when using LO macros for Base.

more

Hi, yes i did take the macro from the source you mentioned. It looked to be doing the exact thing i required for my database, so copied, pasted and try to edit it. I generally print the macro and screen shots of the forms, tables and form navigator for the database and my own database so i can hi-light the various names to be edited. Generally for simpler macros this has worked well for me, not with this one. This is why i questioned the embedded part as I believed that may be the issue.

( 2018-04-09 10:34:41 +0100 )edit

There is nothing wrong with the initial code based on the tests done. If it doesn't work for you, you have probably missed some important factors. The form also have three queries built into the controls and/or the internal forms. These dictate the record numbers used. There are also events in the list boxes tying them to the macro. Record ID #0 is used in this process to hold the list box data for use by the queries.

( 2018-04-09 14:22:19 +0100 )edit

This is a terrible way to do filtering as it stores non-relative data in your table (record # 0). As stated in the answer, it is much better to use a separate table (not part of you table data) to store this selective info.

More on filtering here -> Filter/Search with Forms (leveraging SubForms).

( 2018-04-09 14:29:59 +0100 )edit