How to change the value of a field using visual basic macro

Hello, I have a text filed called playerName, how can I read and write his value using visual basic?

@Villeroy Good, that wiki page will be handy. But I can’t find where it says how to get the the forms objects, am I forgetting something?
Also, do exist a documentation for all object classes so I can lookup the methods and types?

Create a macro with the following code:

SUB UpdateLevel(oEvent AS OBJECT)
	DIM oForm AS OBJECT
	DIM oField AS OBJECT
	DIM oField2 AS OBJECT
	DIM oField3 AS OBJECT
	oField = oEvent.Source.Model
	oForm = oField.Parent
	oField2 = oForm.getByName("txtPlayerName")
	oField3 = oForm.getByName("txtPlayerLevel")
	
	IF oField2.getCurrentValue() = "Hero" THEN
		oField3.BoundField.UpdateDouble(100)
	ELSEIF oField2.getCurrentValue() = "Enemy" THEN
		oField3.BoundField.UpdateDouble(50)
	ELSE
		oField3.BoundField.UpdateDouble(-999)
	END IF
END SUB

This assumes you have two fields with the names txtPlayerName and txtPlayerLevel. The code checks if txtPlayerName contains e.g. the text “Hero” and assigns a double value to txtPlayerLevel.

Now you only have to assign the macro with right click > control > events.
oField2.getCurrentValue() reads the value and
oField3.BoundField.UpdateDouble(100) writes the value

1 Like

Hi @TheSmallest , I can’t find the menu right click > control > events you are talking about, once I right click there is no control option.
However why should this function be connected to an event? I have the macro that starts with a Main function, is possible to get the forms from the ThisComponent global variable?

If you right click on a field there is a control properties option:

If you click it a new window will open. Choose events in that window.

If you connect it to an event then your macro gets executed every time the event from your field (e.g. “When receiving focus”) gets activated.

Maybe the following macro is what you want?

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

REM every variable must be explicitly declared with DIM statement
OPTION EXPLICIT
GLOBAL oForm AS OBJECT
GLOBAL oStatement AS OBJECT
GLOBAL valueB AS INTEGER



Sub Main
	DIM oColumns AS OBJECT
	
	IF isNull(ThisDatabaseDocument.CurrentController.ActiveConnection) THEN
		ThisDatabaseDocument.CurrentController.connect
	ENDIF
	oStatement = ThisDatabaseDocument.CurrentController.ActiveConnection.createStatement()
	
	REM see form properties for your form name
	oForm = ThisComponent.getDrawPage().Forms.getByName("MainForm")
	oColumns = oForm.getColumns()
	REM see your table for your column name.
	valueB = oColumns.getByName("ValueB").value
	
	
	REM get the value from the table. msgbox displays it inside of a message box
	getValue()
	msgbox "Value was " & valueB
	
	REM update value.
	updateValue()
	REM get value again, so that the msgbox displays the correct value.
	getValue()
	msgbox "After Update value is " & valueB
End Sub

FUNCTION getValue
	DIM selectSQL AS STRING
	DIM oResult AS OBJECT
	
	REM use SQL to get the actual value from ValueB at row id 0.
	selectSQL = "SELECT ""ValueB"" FROM ""blueArea"" WHERE ""ID""=0"
	oResult = oStatement.executeQuery(selectSQL)
	oResult.next
	valueB = oResult.getInt(1)
END FUNCTION

FUNCTION updateValue
	DIM sSQL AS STRING
	
	REM we can update it with SQL. blueArea is the table in my example.
	REM we set valueB as 1000 (but in the row with ID 0). You can use a variable for ID if you need to.
	sSQL = "UPDATE ""blueArea"" SET ""ValueB"" = 1000 WHERE ""ID""=0"
	oStatement.executeUpdate(sSQL)
END FUNCTION

I included comments with the REM statement. Hope that helps.

@TheSmallest oh, sorry but my field does not look like that on my document, that’s not the kind of field I want to modify with a macro. I’ve created my field with this menu:


Insert -> Field -> More Fields... -> Select "User Field", set a name and a value.

Once the field has been inserted the value that is stored can be modified.

What I want to modify with a macro is the field I’m showing in the last image, that’s was I was confused about the events, this field type has no events.

LibreOffice called it field, so I also called it field. Now do not confuse them with an input field like text or buttons.

I hope I made myself clear now. :wink:

Yes, it’s clearer now.
Have you already downloaded the book by Andrew Pitonyak? Please take a look at chapter 5.18.2. Text Fields - isn’t that what you’re looking for by any chance?

If the link given does not work, try to find this book on the Internet - there are many copies

1 Like

My bad, I mixed things up a little bit. I can try to create a working macro if the one @JohnSUN referenced doesn’t solve your problem.

@TheSmallest I found a solution thanks to the document @JohnSUN show me.

Using the work of Andrew Pitonyak I wrote this script that get the field by his name and changes his text content.

Sub Main
	Dim vEnum As Object
	vEnum = thisComponent.getTextFields().createEnumeration()
	
	' check if fields are used in the document.
	If Not IsNull(vEnum) Then
	
		' loop trough all fields.
		Do While vEnum.hasMoreElements()
		
			' get the field.
			Dim field as Object
			field = vEnum.nextElement().TextFieldMaster
			
			' check field name we care about.
			if field.Name = "playerName" Then
			
				' modify the field.
				field.Content = "My custom value"
				
				' print content.
				Print field.Content

				Exit Do ' exit While loop
			End If
		Loop
	End If
End Sub
1 Like