Base Macro - Click on Table Row to Open Record

here is an example of this.
What you are interested in is the line Key =…

sub onClickViewEdit( oEv as object )
	dim aForm as object
	dim Key as integer
	' get the inovice record
	' the user just double clicked on
	Key = oEv.Source.Model.Parent.Columns.getByName( "InvoiceID" ).getInt
	ItemEntry.setKey(Key, "MSIS/frmItemEntry")
	hideMe()
end sub

This grabs the PK from the datasource attached to a grid UI, sets the value in a different form “MSIS/frmItemEntry”, launches that form, then hides the current form.

oEv.Source.Model.Parent gives you the datasource (resultset) the grid is connected to.

The UI control is repsonsible for moving the record pointer in the resultset, your code only needs to read the column value for the current record.

It is not true to say you can’t get this by using the UI controls ‘view’ but that ends up being way more work than is necessary.

@drewjensen Having trouble with last two lines of your code. ItemEntry gives:

BASIC runtime error.
Object variable not set.

and cannot find any info anywhere on it. Also 'hideMe():

BASIC runtime error.
Property or method not found: hideMe.

Your answer is interesting but can’t figure out how to get last two lines working.

They are db specific function. Hideme() is

sub hideMe()
	thisComponent.CurrentController.Frame.getContainerWindow.setVisible( FALSE ) 
end sub

which is easier to use than the full line.

Each form has a corresponding basic module, The Item Entry form has a module ItemEntry, which has a function setKey. Second parameter is optional… more

and looks like this:

sub setKey( aKey as variant, optional aFormName as string )
	dim oForm as object
	if isMissing( aFormName ) then
		oForm = thisComponent
	else
		oForm = OpenDBForm(aFormName)
	end if
	with oForm.Drawpage.Forms(0)
		.filter = "InvoiceID = " & aKey
		.Applyfilter = True
		.Reload
	end with
	ItemEntry.setTaxControls(oForm, oForm.Drawpage.Forms.getByName("Standard").getByName("Items").getByName("Totals"))
end sub

To be complete setTaxControls expects a dataform object and will have one or more databound UI controls. In this case the db application has to support multiple tax fields, needed in some countries. The db includes a configuration function that lets the end user set different tax types and rates and allows for custom strings for field labels in the forms, etc.

Thank you. Didn’t realize your routine needed code in each form.

Need? It is how I designed it - actually I did misspeak there are some forms with no corresponding module, as the form is fairly simple. In the case of this db app there are no forms which do not have some call into the basic library though, as the user never has to see the Base default window, when the odb is opened the ‘switchboard’ if you will is opened and the Base window is hidden.

It all depends on what you are trying to accomplish.

@drewjensen It turns out I misspoke. I believe your original answer simply is about obtaining the needed key from the table grid which I agree with. I thought the remainder (last two lines) was a simple method to open the new form at the record with the obtained key. Further examination of your additional code indicates you have other routines which accomplish this portion.

Sorry for the confusion.

Thanks guys. I’m sure your back and forth discussion will help anyone in the future looking to do this kind of thing. I’ll look through both answers and try to learn from your guys’ experience. I accepted @Ratslinger 's answer simply because there was an attached working example. But I appreciate both answers.

Hello @PhLo,

Have attached a Q&D sample. Using the form CustomersGrid (again Q&D - no filter used) when you click on any row, that record will open form CustomerEdit at the record chosen.

Code can be improved upon for your needs and includes code to resize the newly opened form. The new form is opened at the required record by setting the filter to the ID value requested.

Sample ---- OpenFormFromGridSelection.odb

Note: Ignore comment in code about storing variable in Global. Mistakenly left in from copied code. No globals used in macro.

This looks like exactly what I need. Awesome to see it working. I figured it was possible somehow. Thank you sir. Now to inspect the code and figure out how it’s done. It’s a good way for me to learn a few things.

There’s one weakness to this approach. Strangely, a macro attached to a table control’s click event triggers no matter where the click occurs, even on scrollbars, nav buttons at bottom, etc. If there are more records than can display, you can’t scroll down without clicking to trigger a record edit launch. Too bad the developers didn’t think of that and only trigger inside the cells. I think I’ll not worry about it and tell users to use mouse scroll wheel instead of clicking on scrollbars.

One goofy way to get around this click problem is to attach the macro to the table’s “when losing focus” event instead. Then it requires a click anywhere outside the table control. While that might seem strange, a “load” or “open” button (with no event) adjacent to the table might tempt a click and feel like a sensible user experience. Don’t know why click events attached to table columns don’t work. They literally do nothing.

No need for ‘goofy’ approaches. Just add oEvent ( Sub GetID(oEvent) ) and you can check for conditions. Assign to middle button, or left button with a modifier ( such as + Ctrl or Alt key) or even right button although this is mostly for context menus. You need more knowledge on using events and MRI to examine properties and methods.

See → struct MouseEvent

and → constants group MouseButton

and → struct InputEvent

So with oEvent present, then:

If oEvent.Buttons <> 4 Then exit sub

at the beginning of the routine will only execute the routine if the middle button was pressed.

Also, click events in columns do work. What is probably confusing is the method in which they work. If you click in a table control field, it selects the field. They next click in that field activates the mouse click event. Now I can’t say this is the way it should work but only that is the current process.

In my case, the column clicks don’t work at all, no matter how many times I click - 1, 2…30. If I attach the same Subroutine to the table control mouse click event, works every time. Even if I write a Sub that has only msgbox "Hello" and call it on column event mouse click… nothing. That’s why I chose the table control click event instead. Your attached file also has table control click event, so I figured you encountered a similar problem. Otherwise column click makes far more sense.

Strange. I can get the column click event to work by switching it from the table control on YOUR file… but not in mine. Even if I write an entirely new subroutine, it won’t trigger it on a column click. Can’t think of any other explanation than a very obscure bug I’m encountering. Like you said, it takes two clicks on your file. But in mine, it just doesn’t work. I’ll just tolerate the table control click for now since it works.

Since I couldn’t ever get the events attached to columns to work, I ended up making a separate “Edit” button that triggered the event instead. Works great and is probably more “expected” behavior than clicking inside the table control anyway.