My environment: Windows 10, LibreOffice Version: 6.4.0.3 (x64) HSQLDB Split Database
Scenario:
I use lots of macros in my forms to handle conditional situations, resulting in intervention in database tables via sql, or changing form field content or presentation (enable/visible or not).
I wish to make a sub-routine for getting a value from a field, and one for putting a value to a field.
That would simplify my coding.
See my code attempt below.
The coding to put or get a field’s value depends on the file type such as --text, label, listbox, checkbox, radio-option, image, button-name.
In the PutToField subroutine, the code below works for fieldtypes text, label, button-name and checkbox, I can even put Now() in the place of “mynewtext” and put a date.
However in spite of many alternative tests I haven’t figured out yet how to get content to a listbox, option or radio-option field.
In the GetFromField subroutine, the code below works for fieldtype listbox, text, label, button-name and checkbox.
However in spite of many alternative tests I haven’t figured out yet how to get content from option and radio-option fields.
Sub PutToField (sTargetName As String, sTargetContent As String, sTargetType As String)
Dim oForm As Object
Dim oPutField As Object
Dim Doc As Object
Dim DocCtl As Object
Dim CtlView As Object
oForm = ThisComponent.Drawpage.Forms.getByName("MainForm")
Doc = ThisComponent
DocCtl = Doc.getCurrentController()
oPutField = oForm.getByName(sTargetName)
CtlView = DocCtl.GetControl(oPutField)
If sTargetType = "Text" Or sTargetType = "Label" Then
CtlView.Text = sTargetContent
End If
If sTargetType = "Button" Then
CtlView.Label = sTargetContent
End If
If sTargetType = "CheckBox" Then
If sTargetContent = "True" Then
CtlView.State = "1"
Else
CtlView.State = "0"
End If
End If
End Sub
Sub TryPut
PutToField("txt-BriefDescription","mynewtext","Text")
End Sub
Sub GetFromField (sTargetName As String, sTargetType As String)
Dim oForm As Object
Dim oField As Object
Dim sGetValue As String
oForm = ThisComponent.Drawpage.Forms.getByName("MainForm")
oField = oForm.getByName(sTargetName)
If sTargetType = "ListBox" Then
sGetValue = oField.SelectedValue
End If
If sTargetType = "Text" Then
sGetValue = oField.Text
End If
If sTargetType = "Label" Or sTargetType = "Button" Then
sGetValue = oField.Label
End If
If sTargetType = "CheckBox" Then
If oField.State = "0" Then
sGetValue = "False"
Else
sGetValue = "True"
End If
End If
MsgBox(sGetValue)
End Sub
Sub TryGet
GetFromField("listbox-Coordinates","ListBox")
End Sub
As you can see it’s mostly a question of one line of syntax one needs to know.
Can I get some help please in getting the lines that need to be added to the subroutines in order to handle the other types of fields mentioned. Thank you