Ahsan Amin

I have to to fill a combo box control with a table fiels with 'Please Select' at top.

Re: ComboBox Control

Naomi Nosonovsky

There are several ways to achieve your functionality.

I would probably go with

create cursor curItems (cItem C(30)) &&, cOrder C(1))

*index on cOrder + cItem

insert into curItems values ('(Please Select)')

insert into curItems select Item from myTable order by 1

and use this cursor as the RowSource for your combobox.

Re: ComboBox Control


thisform.COMBO.DisplayValue = "Please Select"

thisform.ControlSource = FieldName

Re: ComboBox Control

Ahsan Amin

it gives error 'Property Control Sourse is not found'

Re: ComboBox Control

Ahsan Amin

and when i use 'thisform.cmdCourse.ControlSource' the following error occurs:

the data sourse for this object must be a variable name

Re: ComboBox Control

Naomi Nosonovsky


It should have been written

thisform.combo.ControlSource = 'fieldname'

if you want to do it in code. Or you can set it in the Properties sheet.

Do you want to have Please Select only the first time the form starts of have it always in your combobox

If the former, then use dni's idea (set DisplayValue). If the latter, use my idea.

BTW, for comboboxes the prefix should be cbo and for command buttons it would be cmd, e.g.

combobox for Names would be


and command button for Search would be


Re: ComboBox Control

Ahsan Amin

i want the combo box to be filled with table columns on form load. i have code on form's Init event.

Re: ComboBox Control


Ahsan Amin wrote:

I have to to fill a combo box control with a table fiels with 'Please Select' at top.

Add an array property to your form called aFields

Then put this code in the combo's init():

Code Snippet


lnFields = AFIELDS( Thisform.aFields, [<< alias that you want to list the fields for >>] )

AINS( Thisform.aFields, 1 )

Thisform.aFields[ 1, 1 ] = [Please Select]

.RowSourceType = 5

.RowSource = [Thisform.aFields]

.ColumnCount = 1


.ListIndex = 1


Re: ComboBox Control

Naomi Nosonovsky

You didn't answer on my question. Do you want (Please Select) to always be in the combobox or only the first time when the form appears. Once they selected an item, it is no longer will be there.

If the latter, then set up your combobox in the design time normally (add a table into form's DE, set it to be combobox RowSource) and in the form's Init put in last line of code

thisform.cboCourse.DisplayValue = '(Please Select)'

I put it in () because AFAIK this is that most websites use (Please Select) in parentesis.

Re: ComboBox Control

Ahsan Amin

Please Select will always be there on top of the list.

Re: ComboBox Control

Naomi Nosonovsky

Then I would suggest to use my approach from my first reply in this thread with a cursor for the RowSource. I assume you want to use this combobox unbound, e.g. only to pick values, right

What is your table name and the fields you want to show in your combobox

Re: ComboBox Control

Ahsan Amin

This form inserts a record in the table. The form's name is students. ComboBox must fill the field from other table. This fieild's name is Courses. When use will fill this form, he will also select a course from this combo box.

Re: ComboBox Control

Naomi Nosonovsky

I see.

Ok, in the Load of the Form:

create cursor curCourses (CourseName C(30), CourseCode C(2))

*VFP8 + code

inser into curCourses values ('(Please Select'," ")

insert into curCourses select CourseName, CourseCode from Courses

In the Init of the form

Code Snippet

with thisform.cboCourses

.RowSourceType = 2 && Alias

.RowSource = 'curCourses'

.BoundColumn = 2

.ControlSource = 'Students.CourseCode'


From the top of my head, not tested. You may need to set few more propeties of your combobox.

Re: ComboBox Control

Vladimir Zografski

Hi Ahsan
Would this work for you
Code Snippet
PUBLIC oform1


*-- Form: form1 (e:\Test_VFP\form1.scx)
*-- ParentClass: form
*-- BaseClass: form
*-- Time Stamp: 08/10/07 08:29:09 PM
DEFINE CLASS form1 AS form
Top = 0
Left = 0
Height = 287
Width = 478
DoCreate = .T.
Caption = "Form1"
Name = "Form1"

ADD OBJECT combo1 AS combobox WITH ;
ControlSource = "", ;
Height = 29, ;
Left = 112, ;
Style = 2, ;
Top = 65, ;
Width = 254, ;
Name = "Combo1"


CREATE TABLE Students (Id I, Courses C(30))
INSERT INTO Students VALUES( 1, [Course 1])
INSERT INTO Students VALUES( 5, [Course 5])
INSERT INTO Students VALUES( 3, [Course 3])
INSERT INTO Students VALUES( 9, [Course 9])
INSERT INTO Students VALUES( 2, [Course 2])

SELECT ID, Courses ;
FROM Students;

INSERT INTO crsCombo VALUES(0 ,[Please Select])

PROCEDURE combo1.Init
ThisForm.Combo1.BoundTo = .t.
ThisForm.Combo1.BoundColumn = 1
ThisForm.Combo1.RowSource =[crsCombo.Courses,Id]
Thisform.combo1.RowSourceType = 6
thisform.combo1.ListIndex = 1

PROCEDURE combo1.InteractiveChange
IF crsCombo.Id = 0
MESSAGEBOX([Please select ....])
MESSAGEBOX([SELECTED ID ]+TRANSFORM(crsCombo.Id)+[. Search in Students Id= ]+TRANSFORM(crsCombo.Id))

*-- EndDefine: form1

Re: ComboBox Control

Naomi Nosonovsky

Hi Vladimir,

Courses and Students are two different tables.

I would assume that Courses table has a PK and a CourseName (and may be a FK to the Teacher) and the Students table may be StudentID, Name, and Course ID assuming each student can only take one course (otherwise we obviously need StudentCourses table M:M)