Ask Your Question
0

access current record of table in macro

asked 2015-11-08 23:17:06 +0100

Pansmanser gravatar image

I am trying to write a Basic macro in Base for the first time. Please bear with me!

I want to run my macro from a line of a table, called by a customised keystroke, which will access that current record of the table. I can make the macro do what I want once it finds the data, but I can't see how to say 'use this record.'

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
1

answered 2015-11-09 02:43:35 +0100

doug gravatar image

You can access the record in two ways, both of which require you to use a Form rather than the Table GUI. You can use a columnar form that looks exactly like the table GUI.

There probably is a way to explore down through the object hierarchy to figure out how to get the information from the table GUI, but I would not recommend spending the time trying to figure it out. If you still insist on using the Table GUI for this do MsgBox ThisComponent.DBG_properties and then explore up the object hierarchy (e.g. Parent) and down (e.g. getByIndex(0)) until you find whatever it is that has the data. You will find this to be time consuming and confusing, but if you are persistent eventually you may well find it.

If, on the other hand, you will switch to a Form:

Option A: alter the values of data in controls on the open form. For the simple principles involved, here is an example. Once you access a value from a Form Control, you can manipulate the data in Basic. To execute your vision, you could make all of the changes in the form controls, assuming you just want to change the current record. Note that numeric, date, and text controls all have different properties and methods, and date in particular is an UNO object, so look around for various recipes of how to access those objects, or explore the .DBG_methods and .DBG_properties of the objects. When you commit the whole record, the new values that committed to the various controls will be saved to the table.

Option B: alter the values directly in the underlying table using a SQL query, and then reload the form record. For an example of SQL queries in a macro, see this example. That query also shows the structure of a UNO date object. Instead of the SELECT query there, you would do an UPDATE query, or whatever was desired, with the limitation to the primary key of the record you want to change. Then .reload the form and move it back to the correct record if necessary, which is the last code example in this answer.

edit flag offensive delete link more

Comments

Thank you so much Doug, for taking the time to make a thorough answer. No time today, but I'll try it very soon, and I'm sure I'll be able to mark my question as solved.

Pansmanser gravatar imagePansmanser ( 2015-11-09 14:01:59 +0100 )edit

Doug, I have stumbled at the first hurdle. I have created my form to be identical to the underlying table. How do I read specific cells (controls?) in the form, and how do I address the current record in the form?

Pansmanser gravatar imagePansmanser ( 2015-11-09 17:03:12 +0100 )edit

It is a little detailed to explain the object hierarchy. See in each code example where it starts with ThisComponent and then creates a MainForm object and then a Ctrl object? When you open the form and look in the form navigator, the first object is what I sometimes call a container of forms, the second is the MainForm and from that are subforms and controls. Your data will be in a Table control (I think) and the sub-controls I think will be the columns of the current row.

doug gravatar imagedoug ( 2015-11-09 18:34:10 +0100 )edit

If you post your form as an attachment, I (or someone else) would (eventually) be able to give more specific tips.

doug gravatar imagedoug ( 2015-11-09 18:35:21 +0100 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2015-11-08 23:17:06 +0100

Seen: 910 times

Last updated: Nov 09 '15