[Gambas-user] ComboBox options from databse

Eduardo Huertas e.huertas at ...626...
Tue Sep 11 13:40:17 CEST 2007


Some code on how I finally did it.
This code is only an example, no error handling or things like that.


PUBLIC db AS NEW Connection
PUBLIC cboItems AS NEW Object[] 'This will be the array which holds
the data form the database

PUBLIC PROCEDURE connectDb()
  WITH db
    .Close
    .Type = "mysql"
    .Host = "localhost"
    .Login = "root"
    .Password = "password"
    .Name = "test"
    .Open
  END WITH
END

PUBLIC SUB Form_Open()
'Populate the ComboBox and the array holding the complete results (in
the same order)
  DIM rst AS Result
  connectDb()
  rst = db.Find("tblItems")
  FOR EACH rst
    ComboBox1.Add(rst!item)
    cboItems.Add([rst!item_id, rst!item])
  NEXT
END

PUBLIC SUB Button1_Click()
'Shows the item_id of the selected option
  PRINT ComboBox1.Text
  PRINT cboItems[ComboBox1.Index][0]
END




More information about the User mailing list