Page 1 of 1

Want to pre-select combobox initial value

Posted: Sun Mar 30, 2008 1:13 am
by Rcaph
I have a main menu form on which I have put a combobox control. This is a dropdown list of the 12 months of the year.

I want the form to open with one of the months pre-selected. The pre-selected month will be the one I last chose.

I already have the combobox filled from a table containing all 12 months. I also have a table with the last selected monthname. I have written a macro which goes to the MonthSelected table, extracts the value and sets the value into the combobox, however, I cannot get it to run as the form starts up. I can get it to run correctly by pressing a pushbutton with the "Mouse Button Pressed" event set to my macro.

Code: Select all

REM
Sub MainMenu_Initialize(evt as Object)
	
REM Create an object to store the results
	dim rows
	rows = createUnoService("com.sun.star.sdb.RowSet")

REM Need a reference to our current database
	Dim frm As object
	frm = evt.Source.Model.Parent
'	frm = thisComponent.CurrentController.Frame 
	
REM get the connection for the database
	rows.ActiveConnection = ThisComponent.Parent.DataSource.getConnection("","")
	
REM Set the command type, create an sql statement, and execute it
	rows.CommandType = com.sun.star.sdb.CommandType.COMMAND
	rows.Command = "select ""MonthNo"", ""MonthName"" from ""SelectedMonth"""
	rows.execute()
	
REM Do something with the data
	dim MonthNo, MonthName
	
	while rows.next()
		MonthNo   = rows.getInt(1)
		MonthName = rows.getString(2)
	wend
	
'	Print MonthName
	
	Dim ComboBox As Object
	ComboBox=Evt.Source.Model.Parent.getByName("CurrentMonth")
	ComboBox.Text = MonthName

End Sub
Perhaps I am approaching this all wrong as I do not know in what order the various events occur at form startup, but I was expecting to include it in the form's "When Loading" event.

Can anyone suggest how this can be achieved.

Thanks