Well I use this to answer inside foxite forum earlier but then I just thought maybe some does not know yet how to use a cursor from an SQL SELECT as RowSource of a combobox, or if some do, they are maybe wondering how to update such when there is a change from the source table/cursor, in result keeping them away over its usage on a combobox; so I transferred it over here as part of my simple tutorials.
You see, I prefer to work on cursors on my end as that makes my app works faster, less prone to corruption (possibility of corruption may happen only during transferring of data from cursor to actual table), and easier to maintain.
So I use cursors on grids, listbox, combobox, etc. However, some may have been wondering that if they use a cursor for a combobox RowSource, how to update said combobox then when there is a change in data from the actual table it is coming from? Or in other words, how to show the changes back to the combobox when it is already set to run on a cursor that was created earlier than when the change on the actual source table/cursor happened?
The answer to that is a simple .Requery() event. No additional codes needed, just plain 'ol .Requery(). See the images below:
You see, I prefer to work on cursors on my end as that makes my app works faster, less prone to corruption (possibility of corruption may happen only during transferring of data from cursor to actual table), and easier to maintain.
So I use cursors on grids, listbox, combobox, etc. However, some may have been wondering that if they use a cursor for a combobox RowSource, how to update said combobox then when there is a change in data from the actual table it is coming from? Or in other words, how to show the changes back to the combobox when it is already set to run on a cursor that was created earlier than when the change on the actual source table/cursor happened?
The answer to that is a simple .Requery() event. No additional codes needed, just plain 'ol .Requery(). See the images below:
And here are the sample codes I used on that:
loTest = Createobject("frmCombo")
loTest.Show(1)
DefineClassfrmCombo AsForm
AutoCenter = .T.
AddObject text1 AsTextBoxWith ;
top = 50,;
left = 10,;
width = 60
AddObject text2 AsTextBoxWith ;
top = 50,;
left = 90,;
width = 200
AddObject grid1 AsGridWith;
top = 90,;
left = 10,;
width = 300,;
Height = 100,;
RecordSource = 'MyTable'
AddObject command1 AsCommandButtonWith ;
top = 210,;
left = 10,;
Caption = '\<Update Combobox',;
AutoSize = .T.
ADDOBJECT label1 aslabelWITH;
top = 210,;
left = 150,;
Width = 200,;
Caption = 'Change content of grid and click Update Combobox button',;
Wordwrap = .T.,;
Height = 60
* This time I am using RowSourceType 3 or an SQL SELECT statement. I am
really creating a cursor now out of the source table/cursor
AddObject combo1 AsComboBoxWith ;
RowSourceType= 3,;
RowSource = 'Select * from MyTable order by 1 into cursor MyCursor NOFILTER',;
Style = 2,;
top = 10,;
left = 10,;
Width = 200,;
ColumnCount = 2,;
ColumnWidths = '30'
ProcedureLoad
* I use cursor here so a physical table won't be created on your
end. Pretend this is a table instead of cursor
CreateCursor MyTable (xCurrency c(3), xCountry c(30))
InsertInto MyTable Values ('PHP','Philippines')
InsertInto MyTable Values ('PGK','Papua New Guinea')
InsertInto MyTable Values ('AUD','Australia')
GoTop
Endproc
Procedure combo1.InteractiveChange
Thisform.text1.Value = MyTable.xCurrency
Thisform.text2.Value = MyTable.xCountry
Endproc
Procedure command1.Click
Thisform.combo1.Requery()
Endproc
Enddefine
*********** End
And there you are! Cheers!