Ask Your Question
0

Can an embedded macro be copy and pasted?

asked 2018-04-06 13:02:02 +0100

MaaX gravatar image

updated 2018-04-06 13:37:10 +0100

Lupp gravatar image

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.reload 'updates the SubForm link(s) as necessary to complete the Form 'refresh'
    oForm.getByName("SubForm1").getByName("listboxClass").refresh 'necessary to update the List Box filter (listboxClass") on SubForm1
    oForm.reload 'updates the SubForm link(s) as necessary to complete the Form 'refresh'
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
    oForm.reload
End Sub

(Slightly edited for readability by @Lupp )

edit retag flag offensive close merge delete

Comments

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.)

Lupp gravatar imageLupp ( 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?

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

1 Answer

Sort by » oldest newest most voted
0

answered 2018-04-07 06:27:57 +0100

Ratslinger gravatar image

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.

edit flag offensive delete link more

Comments

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.

MaaX gravatar imageMaaX ( 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.

Ratslinger gravatar imageRatslinger ( 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).

Ratslinger gravatar imageRatslinger ( 2018-04-09 14:29:59 +0100 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2018-04-06 13:02:02 +0100

Seen: 74 times

Last updated: Apr 07 '18