Page 1 of 1

Populate list box with records from database

Posted: Fri Mar 14, 2008 7:45 pm
by Lazy-legs
Hello,

Here is a simple macro that populates the ListBox1 in the Dialog1 dialog window with words from the "Word" column in the "wordlist" table in the BaseDB database.

Code: Select all

Sub PopulateListBox()

DBContext=createUnoService("com.sun.star.sdb.DatabaseContext")

 If not DBContext.hasByName("WriterDB") then
   MsgBox (ConnectionFailedMessage, , "Connection failed!") : End
 End If

DataSource=DBContext.getByName("BaseDB")
ConnectToDB=DataSource.GetConnection ("","")

SQLResult=createUnoService("com.sun.star.sdb.RowSet")

SQLQuery="SELECT ""ID"", ""Word"" FROM ""wordlist"""
SQLResult.activeConnection = ConnectToDB
SQLResult.Command = SQLQuery
SQLResult.execute

exitOK=com.sun.star.ui.dialogs.ExecutableDialogResults.OK
DialogLibraries.LoadLibrary("Standard")
Library=DialogLibraries.GetByName("Standard")
TheDialog=Library.GetByName("Dialog1")

Dialog=CreateUnoDialog(TheDialog)

DialogField=Dialog.GetControl("ListBox1")

While SQLResult.next
ListBoxItem = SQLResult.getString(2)
DialogField.additem(ListBoxItem, DialogField.ItemCount)
Wend

If Dialog.Execute=exitOK Then
CurrentItemName=DialogField.SelectedItem
End If

End Sub
I hope you will find it useful.

Kind regards,
Dmitri

Re: Populate list box with records from database

Posted: Mon Jun 14, 2010 7:43 pm
by batonac
Hey, this code is great! You've saved me alot of time from figuring this out manually.

There's just one extra thing I'd like to do that I can't quite seem to figure out how.

In a form, you can use a listbox to display one value (or database column) but record another. (as an example, display cutomerName, record customerID)

I don't know how to do this with macro code.

Any help?