Is there any way to define event procedures for form controls such as the case with ActiveX objects?
I have a GUI which currently adds/removes ActiveX command buttons by the user but ran into Error 91 when trying to add an item to a global collection with each button added. My best guess is because of this much hated phenomenon. With the current state of the project, my best option is to switch to using form control buttons if I am able to define their event procedures. If not, I may need to somehow save the global variables and read them back after manipulating ActiveX control objects.
If you just want to add buttons and capture clicks, you can assign a common macro to them, and switch what action is taken based on the name of the button (which you would set when you create it, and which can be accessed via Application.Caller in the called procedure)
Sub AddButtons()
With ActiveSheet.Buttons.Add(100, 100, 50, 50)
.Name = "button1"
.OnAction = "ClickHandler"
End With
With ActiveSheet.Buttons.Add(200, 100, 50, 50)
.Name = "button2"
.OnAction = "ClickHandler"
End With
End Sub
Sub ClickHandler()
Dim bName As String
bName = Application.Caller
Select Case bName
Case "button1": MsgBox "Clicked First button"
Case "button2": AnotherSub
End Select
End Sub
Related
I cannot figure out (or find an example) how to perform the following simple thing in the LibreOffice Calc 6.2:
I have a drawing shape (e.g. a simple rectangle) in a sheet (call it ShapeA) and a textbox shape in another sheet (call it TextboxB). I want to do the following: when I click on the ShapeA, the TextboxB must appear on the screen (without changing the current sheet, maybe in a dialog box) and then be closed with a mouse click.
I guess the macro associated with ShapeA could look something like this:
Sub Main
oDrawPage = ThisComponent.getDrawPage()
oTb = oDrawPage.getByName("TextBoxB")
oTb.show()
End Sub
Could someone advise what I should put into this macro to accomplish the described task?
UPDATE: What I want to accomplish ( in reply to Jim K.).
I have a very cluttered diagram with many shapes. Each shape has some textual information associated with it. There is not enough space on each shape or around it to contain this info. So there is must be a way to display this info about each shape. Also this information should be displayed in a preformatted way (it contains code and other structured info).
My plan is to create a textbox with the relevant information for each diagram shape, place these textboxes in other sheet and have a possibility, when viewing diagram, to click on any shape and view the associated info in the poped up textbox without leaving the diagram, and then close the textbox with a simple action (e.g. by clicking on it).
Does this task sound feasible to be realized with the LO's shapes and macros?
How about this: Put everything on the same sheet but keep the text boxes hidden until needed.
Use the following code adapted from https://ask.libreoffice.org/en/question/93050/how-can-i-hideshow-a-shape-of-my-spreadsheet-using-a-macro/.
Sub ShapeClickedA
ShowHideShape("TextBoxA")
End Sub
Sub ShapeClickedB
ShowHideShape("TextBoxB")
End Sub
Sub ShowHideShape(shapeName As String)
oDrawPage = ThisComponent.getSheets().getByName("Sheet1").getDrawPage()
For iShape = 0 To oDrawPage.Count - 1
oShape = oDrawPage.getByIndex(iShape)
If oShape.Name = shapeName Then
If oShape.Visible Then
oShape.Visible = 0 'Not Visible
Else
oShape.Visible = 1 'Visible
End If
End If
Next iShape
End Sub
If you haven't yet, set the names of the text boxes by right-clicking and choosing Name... Then right click on both ShapeA and TextBoxA and assign the macro ShapeClickedA. Do likewise for other pairs of shapes. The result works like this:
Before anything is clicked.
Click on ShapeA. (To close it again, click on either ShapeA or TextBoxA). ShapeB functions similarly.
It's also possible to display both at the same time.
The question and answer LibreOffice Base; Tab order from mainform to subform almost solve the issue I have, but not completely.
I have a table mytable (id, name, textfield). I'm displaying id and name in a form with a table layout (table control). I've added the column textfield from the same table as a subform with a text box control (the reason is that I want to enter text with newlines, while being able to navigate the records quickly in the main table). Here's what it looks like in design view:
I've added this Basic macro, based on the two answers linked above:
Sub Main
Dim root_doc As Object
Dim form_container, form_ctrlr As Object
Dim main_frm, sub_frm, tab_target As Object
root_doc = ThisComponent
form_container = root_doc.Drawpage.Forms
form_ctrlr = root_doc.getCurrentController()
main_frm = form_container.getByName("MainForm")
sub_frm = main_frm.getByName("SubForm")
tab_target = sub_frm.getByName("TextField")
form_ctrlr.getControl(tab_target).setFocus()
End Sub
Now, if I add the macro on the event When losing focus of the name column, I do get focus on the textbox when pressing Tab, but on the next row.
If I add the macro to the event On key press of the name column, I get what I want when pressing e.g. Space, but Tab or Enter only take me to the next row in the main form.
Is there a way to get this to work with Tab?
Options to solve this, from the answers over at ask.libreoffice.org:
Just use the standard Ctrl + Tab to switch focus.
Assign a macro to a custom key combination and use that. Not all combinations work, I settled on Shft + Enter.
And the macro (provided by user Ratslinger):
Sub Main
Dim oForm, oCtrlr, oField As Object
oForm = ThisComponent.Drawpage.Forms.getByName("MainForm")
oCtrlr = ThisComponent.getCurrentController()
oField = main_frm.getByName("TextField")
oCtrlr.getControl(oField).setFocus()
End Sub
I need to create a small MS Access touchscreen POS (point of sale) application.
If someone has a rudimentary touchscreen MS Access template they can point me to, so that I can learn from it, that would help tremendously.
I have been able to find code for a touchscreen keyboard, which I will use.
The person using the interface needs to click on the name of a person as part of the POS process. The people change often, so I need to update the list often.
I am looking for a way to use VBA to create the onscreen buttons dynamically based on a list of people I update in a separate form.
The form will naturally be full-screen, and will never have more than 50 buttons on the screen representing the list of people. Only a few (less than 5) other controls will be present on this form, so the buttons representing the list of people needs to be constrained to an area on the form. The form must read the list of people, then create fixed sized, large square buttons automatically and be arranged alphabetically from left to right.
Thank you.
Something like this should work for you then. You'll need to make the max amount of buttons you will need on the form and space them and make an OnClick event for each like the one here for Command0. Then when the form is opened the array is initialized and all the buttons are captioned and made visible up to the amount of records in the table.
When each button is clicked it calls the HandleClick Sub to interact with the data as you need.
I made the recordset global so that it is only filled once and avoid weird data issues if the customers table is updated while this form is in use.
Dim Buttons(1 To 4) As Control
Dim rst As DAO.Recordset
Private Sub Command0_Click()
HandleClick (1) 'number that corresponds to this button in the array as
'initialized in the Form_Load sub, hard coded
End Sub
Private Sub Form_Load()
'initialize the buttons
Buttons(1) = Me.Command0
Buttons(2) = Me.Command1
Buttons(3) = Me.Command2
Buttons(4) = Me.Command3
Dim sql As String
sql = "SELECT * FROM tblCustomers"
Set rst = CurrentDb.OpenRecordset(sql)
FillButtons
End Sub
Sub FillButtons()
If (rst.EOF And rst.BOF) = False Then
rst.MoveFirst
Dim itr As Integer
Do
Buttons(itr).Caption = rst("CustomerName")
Buttons(itr).Visible = True
rst.MoveNext
Loop While rst.EOF = False
End If
End Sub
Sub HandleClick(CustomerNumber As Integer)
'Here is where you do what you intended with the button using
'customer number to know which record was clicked
End Sub
It's a messy approach but it should work.
I have a form with a mainform and a subform. When the user is in the textbox, which is the closest to the subform, and the user press Tab, it has to jump into the subform, but it doesn't. It jumps to the textbox AFTER the subform. When the user is in the last textbox of the mainform and te user press tab, then it jumps into the subform.
How do I make sure, that the user will jump to the subform when he is in the textbox, which is the closest one to the subform?
Example image:
Tab order in the UI does not account for controls on subforms, but this can be done programmatically. Set a LO Basic macro on the When losing focus Event for the control that is closest to the grid/table control on the subform. That is the control that, when you tab past it, you want to go to the grid. For that event, run a macro like this, where grid1 is the table/grid control:
root_doc = ThisComponent
form_container = root_doc.Drawpage.Forms
form_ctrlr = root_doc.getCurrentController()
sub_frm = form_container.getByName("Sub_Form")
tab_target = sub_frm.getByName("grid1")
form_ctrlr.getControl(tab_target).setFocus()
You also will need to set up a similar macro when leaving grid1 as, because it is in the subform, it is not accounted for in the tab order.
Hat tip to probe1#ooForum.
I had to add one more line to get the code to work. See code.
Dim root_doc As Object
Dim form_container, form_ctrlr As Object
Dim main_frm, sub_frm, sub_frm_grd As Object
root_doc = ThisComponent
form_container = root_doc.Drawpage.Forms
form_ctrlr = root_doc.getCurrentController()
main_frm = form_container.getByName("MainForm")
sub_frm = main_frm.getByName("SubForm")
sub_frm_grd = sub_frm.getByName("SubForm_Grid")
'set focus to grid control
form_ctrlr.getControl(sub_frm_grd).setFocus()
I have a form with multiple sub-forms.
I would like to have the following behaviour: pressing the tab key in the last field of the main form or in the last field of a subform the focus moves to the next subform or back to the main form according to the defined Tab order.
To achieve this, all subforms as well as the main form have the Cycle property set to Current page.
All subforms are shown within the main form as Datasheets except one that is shown in Form view.
Now I get the behaviour I want only for the main form and for the subforms shown as datasheets but not for the subform shown as Form.
Is this the normal behaviour in access 2003, of is there some other property that I have to change?
Note: if I change the default view of the "misbehaving" form to Datasheet as the others, it also behaves as I want...What am I doing wrong?And... if this is the way access 2003 works... what would be the best way to obtain the desired behaviour using vba given that I cannot find an OnTab method?
Edit: While waiting to understand if this can be done setting the right properties in the forms, I acheved the desired behaviour with this code in the first and last fields of the "misbehaving" subform.
'In the last field
Private Sub Posizione_KeyDown(KeyCode As Integer, Shift As Integer)
If (KeyCode = vbKeyTab And Shift = 0) Then
KeyCode = 0
Me.Parent!Autore_Subform.SetFocus
End If
End Sub
'in the first field
Private Sub Stanza_KeyDown(KeyCode As Integer, Shift As Integer)
If (KeyCode = vbKeyTab And Shift = 1) Then
KeyCode = 0
Me.Parent!Pagine.SetFocus
End If
End Sub
I was just looking for an answer to the same question, and found this via Google: Ctrl-Tab will move from the last control in the subform to the next control in the main form. I found from experimenting (with Access 2007) that it doesn't matter which control has focus in the subform, Ctrl-Tab will pop you out of the subform to the next control in the main form.