Page 1 of 1

[Solved] Button for macros

Posted: Fri Mar 03, 2017 4:51 am
by dewayne ashley
Hello,

I am trying to design a form that has 10 push buttons to insert the numbers 0-9 into the text field, more or less like a number data entry pad built into the form, ""think calculator buttons""

Example#1 = if i push button 1, it inserts 1 into the field.
Example #2 = if I push button 1 and then button 2 and then button 5, I should then see 125 in the text field.

I am trying to find examples of how to do this with macros.
The form will also have a ok button to save the record in the database field.

Any insight on how to do this would be greatly appreciated as this is for a touchscreen database form.

Thanks, 8-)

Re: button macros

Posted: Fri Mar 03, 2017 8:08 am
by Zizi64
You can assign your macro to the click event of the button.
And you can get the Event in the macro routine - to determine, which one of the buttons was clicked. Then you can get the Label of the clicked button (0; 1; 2; 3; ...9). And then you need get the Text field object, and append the actual character to the existing text content of the Text field.

You need study and use the API (Application Programming interface) functions of the office suite...

Re: Button for macros

Posted: Fri Mar 03, 2017 9:24 am
by Zizi64
Here is a small example for Draw application (I never used the Base, but I want to show you an example outside the "trivial" Writer or Calc applications).
Buttons_and_Events.odg
(11.24 KiB) Downloaded 427 times
The macro code:

Code: Select all

REM  *****  BASIC  *****

Sub Put_a_char(oEv as object)

 Dim oButton as object
 Dim oButModel as Object
 Dim oParent as Object
 Dim oTextBox as Object
 Dim sChar as String
   
	oButton = oEv.Source
	oButModel = oButton.Model	
	oParent = oButModel.getParent()
	
	oTextBox = oParent.GetbyName("Text Box 1")
	
	sChar = oButton.Model.Label
	oTextBox.Text = oTextBox.Text & sChar

End Sub



Sub Del_a_char(oEv as object)

 Dim oButton as object
 Dim oButModel as Object
 Dim oParent as Object
 Dim oTextBox as Object
 Dim sChar as String
   
	oButton = oEv.Source
	oButModel = oButton.Model	
	oParent = oButModel.getParent()
	
	oTextBox = oParent.GetbyName("Text Box 1")
	
	oTextBox.Text = Left(oTextBox.Text,Len(oTextBox.Text)-1)

End Sub



Sub Enter_button(oEv as object)

 Dim oButton as object
 Dim oButModel as Object
 Dim oParent as Object
 Dim oTextBox as Object
 Dim sText as String
 Dim iNumber as Long
   
	oButton = oEv.Source
	oButModel = oButton.Model	
	oParent = oButModel.getParent()
	
	oTextBox = oParent.GetbyName("Text Box 1")
	sText = oTextBox.Text
	iNumber = Val(sText)
Print "What to do with the entered string '", stext, "' and the converted ", inumber, " number?"

End Sub

Re: Button for macros

Posted: Sat Mar 04, 2017 3:46 am
by dewayne ashley
Thanks for the reply and suggestions Zizi64 , but it doesn't seem to work correctly
in a base form, its close though. The data i input with the buttons does show up in the text box. But after having set the data source in the properties of the form and text box to the correct table and column of the database, the record navigation indicates I have entered nothing at all, I think this is the key to the problem ,,,if i enter the data with a real keyboard the save icon instantly indicates that data has been entered , whereas with the onscreen buttons it doesn't , even after refreshing the form and checking the table to see if the record has changed, nothing to save and no entry made into the underlying table data.
Ideally, I need a way to mimic real keyboard input of data with onscreen buttons, My OS "Linux Mint" does have onscreen keyboard capabilities, but it seems a awful waste of screen real estate
and ill suited, as all I need is the 0-9 buttons and a decimal point button.
Any and all suggestions are appreciated,,, THANKS

Re: Button for macros

Posted: Sat Mar 04, 2017 8:34 am
by Zizi64
Ideally, I need a way to mimic real keyboard input of data with onscreen buttons, My OS "Linux Mint" does have onscreen keyboard capabilities, but it seems a awful waste of screen real estate
and ill suited, as all I need is the 0-9 buttons and a decimal point button.
There are full featured virtual keyboard emulator softwares, and there are virtual numpads too.
Google it.

Re: Button for macros

Posted: Sat Mar 04, 2017 9:10 am
by Zizi64
Thanks for the reply and suggestions Zizi64 , but it doesn't seem to work correctly
It was a simple example code only. You can modify, and adapt the macro code for your task (or you can write an another one) based on my idea - and based on the API functions.

As I wrote: I never used the BASE application, but if you upload YOUR example .odb file here (with the form, buttons and the text field), maybe somebody will help you.

Re: Button for macros

Posted: Sat Mar 04, 2017 10:55 pm
by dewayne ashley
Thank You Zizi64,
I think the macros you wrote for the Buttons_and_Events.odg are "OUTSTANDING" , and are precisely what I am looking for, with the only exception being that after many experiments, and many errors, I am unable to add to the macro code anything that puts the "button-input" into the underlying database table, but the macro code does put data into the text box on the form beautifully :super: .

I have put together a example for you, and anyone else for that matter, To tinker with and examine what could be the issue, as to what code needs to be added or settings tweaked.

Please try input into # box with both the onscreen buttons and a physical keyboard, Paying close attention to the record navigation icons at the bottom of the form, Specifically "Save Record" icon.

THANKS :D

Re: Button for macros

Posted: Sun Mar 05, 2017 2:25 pm
by Zizi64
As I wrote: I never used the Base application... I can not switch the form control element od the form to Edit mode.
(I can to do it in Calc or in Writer...)
Therefore I can not see, what 'bulit-in' or 'user defined' function is assigned to the event/key "Enter" of the # box, and how it was assigned.

Sorry... I can not help you.

Re: Button for macros

Posted: Sun Mar 05, 2017 4:40 pm
by Arineckaig
as to what code needs to be added or settings tweaked.
At the end of the "Enter_button" macro add these lines:

Code: Select all

	oForm = oParent
	oCol = oForm.findColumn("Number")
	oForm.updateString(oCol, sText)
	oForm.insertRow()
Remove the After Record action and change events from the Form as they will tend to be triggered rather too frequently.

Re: Button for macros

Posted: Sun Mar 05, 2017 6:27 pm
by Villeroy
ByteButtons.odt
(11.45 KiB) Downloaded 469 times

Re: Button for macros

Posted: Sun Mar 05, 2017 8:29 pm
by Arineckaig
Many thanks Villeroy for posting this fascinating Writer file - ByteButtons

Re: Button for macros

Posted: Mon Mar 06, 2017 2:02 pm
by Villeroy
You're welcome.
Arineckaig wrote:Many thanks Villeroy for posting this fascinating Writer file - ByteButtons

Re: Button for macros

Posted: Tue Mar 07, 2017 1:03 am
by F3K Total
Hello Villeroy,
would you mind to explain, why in this code

Code: Select all

Sub NumboxModified(e)
'mri = createUnoService("mytools.Mri")
'mri.inspect(e)
x = e.Source.Value
frm = e.Source.Model.Parent
for each n in Array(1,2,4,8,16,32,64,128)
	btn = frm.getByName("Btn_"& n)
	btn.State = iif(x and n, 1,0)
next
End Sub
especially by this line

Code: Select all

btn.State = iif(x and n, 1,0)
only the correct buttons were pressed?
I do not understand why

Code: Select all

x and n 
give true or not.

Re: Button for macros

Posted: Tue Mar 07, 2017 9:24 am
by Villeroy
Basic help > Command Reference > Run-time functions > Logical Operators
The following logical operators are supported by OpenOffice Basic.
Logical operators combine (bitwise) the contents of two expressions or variables, for example, to test if specific bits are set or not.
[...]
7 and 4 => 4

0 1 1 1
0 1 0 0
--------
0 1 0 0

Re: Button for macros

Posted: Tue Mar 07, 2017 9:43 am
by F3K Total
Thank you, got it, that's really tricky!

Re: Button for macros

Posted: Tue Mar 07, 2017 9:46 am
by Villeroy
(x and n) returns either 0 (False) or n. Every non-zero number in evaluates to True.
More explicitly:
if x and n = n then
btn.State = 1
else
btn.State = 0

Re: Button for macros

Posted: Thu Mar 09, 2017 1:47 am
by dewayne ashley
Arineckaig wrote:
as to what code needs to be added or settings tweaked.
At the end of the "Enter_button" macro add these lines:

Code: Select all

	oForm = oParent
	oCol = oForm.findColumn("Number")
	oForm.updateString(oCol, sText)
	oForm.insertRow()
Remove the After Record action and change events from the Form as they will tend to be triggered rather too frequently.
Did not work for me.

Re: Button for macros

Posted: Thu Mar 09, 2017 2:15 am
by UnklDonald418
I've been following this and I put together a little demonstration based on the code that was posted on this thread. Perhaps it will help you.
Hopefully, I commented out all of my debugging code. Form1 uses a basic MainForm/SubForm arrangement and stores the value entered in an integer field in Table1. Form 2 uses a Dialog to get the data entry but it only displays the entered data in a text box on the Form after the Dialog ends.

Re: Button for macros

Posted: Thu Mar 09, 2017 2:40 am
by dewayne ashley
Looks Really Promising :D ,Give me a little while to shoehorn the code into a test copy of my .ODB ,,, and we will see how it goes...

Re: Button for macros

Posted: Thu Mar 09, 2017 12:40 pm
by Arineckaig
dewayne ashley:
Did not work for me.
Strange. The attached file is simply a copy of the one that you posted with the suggestions applied.

The suggested code was relevant to insertion of new records and thus applicable to Form1 of your posted file that was set for new entries only. If the macro is to apply also to updating existing records then a somewhat more complex code will be required:

Code: Select all

oForm = oParent
	With oForm
		oCol = .findColumn("Number")
		.updateString(oCol, sText)
		IF .IsNew THEN 
			.insertRow() 
		ELSE .updateRow()
		END IF
	End With

Re: Button for macros

Posted: Sat Mar 18, 2017 10:52 pm
by dewayne ashley
dewayne ashley wrote:Looks Really Promising :D ,Give me a little while to shoehorn the code into a test copy of my .ODB ,,, and we will see how it goes...
I can now confirm that UnklDonald418's and Arineckaig's solutions both work for me, Many THANKS to UnklDonald418 and Arineckaig :bravo: I will mark as Solved.