Page 1 of 1

[Solved] Macro generated word to textbox on main form

Posted: Sat Apr 01, 2017 8:36 am
by coopdmc
I wrote a Basic macro that generates random words and show it in a msgbox. I'd like to send that random word to a textbox on the main form
Most of the solutions use drawpage to get the MainForm, but causes errors

Code: Select all

(dim oForm as object
dim oControl as object
'oForm = frm.getByName("MainForm")
'oForm = ThisDatabaseDocument.FormDocuments.getByName("MainForm")
'oControl = ofForm.GetByName("Text Box 1")
oForm = ThisDatabaseDocument.FormDocumens.getByName("myForm")
oListBox = oForm.getByName("myListBox")
msgbox(oListBox.CurrentValue)

'the random word code:
'Ascii codes
'48-57 0-9
'65-90 A-Z
'97-122 a-z
'33-38 special characters

Dim Rand As String
Dim hi as integer 
dim low as integer
dim loop2 as integer
for loop2 = 1 to 4
low = 65
hi = 90
Randomize
rndm1 = rndm1 & Chr$(Int((hi - low + 1) * Rnd + Low))

low = 48
hi = 57
Randomize
rndm1 = rndm1 & Chr$(Int((hi - low + 1) * Rnd + Low))

low = 97
hi = 122
Randomize
rndm1 = rndm1 & Chr$(Int((hi - low + 1) * Rnd + Low))

low = 33
hi = 38
Randomize
rndm1 = rndm1 & Chr$(Int((hi - low + 1) * Rnd + Low))

next
msgbox(rndm1)

Re: macro generated word to textbox on main form

Posted: Sat Apr 01, 2017 6:26 pm
by F3K Total
You have to differ between the Formdocument and the structural forms in it

Code: Select all

oFormdocument = ThisDatabaseDocument.FormDocumens.getByName("myForm").open
oForm = oFormdocument.Drawpage.forms.getByName("MainForm")
oListBox = oForm.getByName("myListBox")
msgbox(oListBox.CurrentValue)
R

Re: macro generated word to textbox on main form

Posted: Sat Apr 01, 2017 9:03 pm
by coopdmc
Thanks R3K Total, these two lines cause errors:
oFormdocument = ThisDatabaseDocument.FormDocuments.getByName("MainForm").open
oForm = oFormdocument.Drawpage.forms.getByName("MainForm")

Error:
BASIC Runtime Error.
object variable not set

I think the trouble is My system doesn't recognize "Drawpage" or "Drawform" (?), as I tried to explain in my original inquiry (although not very well explained).

The original example of displaying a List Box text, I believe used UNO Services. Is anyone familiar with UNO?

Re: macro generated word to textbox on main form

Posted: Sat Apr 01, 2017 9:43 pm
by UnklDonald418
Substitute "myForm" for "MainForm" and I believe you will eliminate the error(s) you are seeing.

Is "myListBox" really a List Box or just a poorly chosen name for a Text Box?

Re: macro generated word to textbox on main form

Posted: Sat Apr 01, 2017 9:54 pm
by UnklDonald418
If it is a Text Box use

Code: Select all

oListBox.setString(rndm1)
to display the results in the Text Box.
If it is actually a List Box the code gets a bit more complicated.

Re: macro generated word to textbox on main form

Posted: Mon Apr 03, 2017 8:53 pm
by coopdmc
Thanks, all for responding, sorry F3K Total, (not R3k Total). I still don't have a complete answer (how does one set a form text box value from a macro result?) however I have found a small part of the puzzle (how to get a form text box value and display in msgbox?)
I found this bit of code at Open Office Forums
viewtopic.php?f=20&t=84203

'Sub Macro1
'Dim x as string
'Dim oDoc as object
'oDoc = ThisComponent
'x = oDoc.CurrentController.Model.DrawPage.Forms.getByName("Form").getByName("Text Box 1").string
'msgbox x
'End Sub
'Sub Macro2(oEvent as object)
'Dim x as string
'x= oEvent.source.Text
'msgbox x
'end sub

which I have modified to work as follows:

Dim x as string
Dim oDoc as object
Dim oForm as object
oDoc = ThisComponent
x = oDoc.CurrentController.Model.DrawPage.Forms.getByName("MainForm").getByName("Text Box 1").string
msgbox x

This shows the Text Box value from a form to a msgbox.
New question:
How do I use a variable (token?) from the macro to populate a unbound Text Box on the form?
Thoughts, how to extract the Text Box (Dim otextbox as object, otextbox.value = ?), in order to address the Text Box directly?

Re: macro generated word to textbox on main form

Posted: Mon Apr 03, 2017 10:20 pm
by UnklDonald418
Maybe this will help. I uploaded a small demonstration with a simple form (myForm) containing a text box and a button. The button runs a macro that generates a random string (from your code) and displays the result in the unbound text box.

Re: macro generated word to textbox on main form

Posted: Mon Apr 03, 2017 11:53 pm
by coopdmc
Looks promising, I just get in the text box "G4e"H2m"E2g#E1h", subsequent button pushes don't affect the text box. Maybe it needs a form.refresh() in the code?
I am unable to modify your code, it's locked (certainly understandable).

Re: macro generated word to textbox on main form

Posted: Tue Apr 04, 2017 12:23 am
by UnklDonald418
I am unable to modify your code, it's locked
Must be something on your end. I just downloaded it to another computer and it generates a different value each time I click on the button, and it is not locked.
Create a new database and drag the form onto the new database. Then copy the macro code. You may have to re-link the button event to the location where you saved the macro code.

Re: macro generated word to textbox on main form

Posted: Thu Apr 06, 2017 10:44 am
by coopdmc
I was able to get working the load a forms unbound textbox with macro data (UnklDonald418, Demo08.odb)
viewtopic.php?f=45&t=20279

Code: Select all

REM  *****  BASIC  *****
'set textbox to macro data (macro to text)
Sub Main
'Function Randm(Num1 As Integer, Num2 As Integer)
'48-57 0-9
'65-90 A-Z
'97-122 a-z
'33-38 spec
Dim Rand As String
Dim hi as integer 
dim low as integer
dim loop2 as integer
for loop2 = 1 to 4
low = 65
hi = 90
Randomize
rndm1 = rndm1 & Chr$(Int((hi - low + 1) * Rnd + Low))

low = 48
hi = 57
Randomize
rndm1 = rndm1 & Chr$(Int((hi - low + 1) * Rnd + Low))

low = 97
hi = 122
Randomize
rndm1 = rndm1 & Chr$(Int((hi - low + 1) * Rnd + Low))

low = 35
hi = 38
Randomize
rndm1 = rndm1 & Chr$(Int((hi - low + 1) * Rnd + Low))

next
'msgbox rndm1
oDoc = ThisComponent
oDocView = oDoc.getCurrentController()

oForm = oDoc.drawpage.forms(0)
oTextBox = oForm.getByName("Text Box 1")

oDocView.getControl(oTextBox).setFocus()
oTextBox.text = rndm1
'end of macro data to textbox

'Get textbox value:  This code gets the forms text box and displays the text in a msgbix
Dim x as string
Dim oDoc as object
'Dim oForm as object
oDoc = ThisComponent
x = oDoc.CurrentController.Model.DrawPage.Forms.getByName("MainForm").getByName("Text Box 1").text
msgbox x
'end Get Textbox value.
For us "new to OOBasic":

If you copy code from a volunteer, that code is particular to that persons form. Build your own form and be sure that the form is running (not 'design' mode).