Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem With Adding New Data To ComboBox

Status
Not open for further replies.

MY3MCS

Technical User
Apr 17, 2003
49
0
0
GB
Dear Everyone,

One of the choices from my combobox is "Add New". The moment this was chosen, another form pops up and get the information and save it to my dbf. My problem is after I release the said form (the one popped up). I want the new record to automatically appear on the combobox of the original form so that I don't have to click the combobox again and look for the new added record. How can I do this? Can you please help me with this?

Thank you very much.
 
You could set the DisplayValue property of the combobox or better yet you could set the ListIndex property of the combobox to the index number of the newly appended item.

Slighthaze = NULL
[sub]craig1442@mchsi.com[/sub][sup]
"Whom computers would destroy, they must first drive mad." - Anon​
[/sup]
 
A lot depends on how you load the combobox, but if your RecordSourceType is Fields, then just add ThisForm.mycombo.Requery() in the form Activate() method. This will only be "costly" if you are constantly switching off this form - even when this table isn't being updated.

The alternative is to pass a ThisForm reference to the modal form and call the Requery method from there after the record is added.

Rick
 
Slighthaze,

Thank you very much but pardon me for not knowing how to do it. Can you please tell me what to do or what to write?

Thanks again.
 
MY3MCS,

Cut-n-paste the following code into a prg file and run it from within VFP...it is a working example and you can just grad the parts of code from it that you need and implement it into your project.

Code:
PUBLIC oForm
oForm = CREATEOBJECT("clscomboadditem")
oForm.show()

DEFINE CLASS clscomboadditem AS form

	DoCreate = .T.
	Caption = "COMBOBOX EXAMPLE"
	Name = "Form1"
	AutoCenter = .T.
	
	ADD OBJECT combo1 AS combobox WITH ;
		RowSourceType = 6, ;
		RowSource = "MyTable.names", ;
		Height = 24, ;
		Left = 60, ;
		Sorted = .F., ;
		Style = 0, ;
		Top = 84, ;
		Width = 204, ;
		Name = "Combo1"

	ADD OBJECT command1 AS commandbutton WITH ;
		Top = 84, ;
		Left = 273, ;
		Height = 24, ;
		Width = 68, ;
		Caption = "ADD", ;
		Name = "Command1"

	PROCEDURE Load
		CREATE CURSOR MyTable (names c(30))
		INSERT INTO MyTable (names) VALUES ("JOHN")
		INSERT INTO MyTable (names) VALUES ("BILL")
		INSERT INTO MyTable (names) VALUES ("DONALD")
		INSERT INTO MyTable (names) VALUES ("REGGIE")
		INSERT INTO MyTable (names) VALUES ("TOBY")
		INSERT INTO MyTable (names) VALUES ("MARK")
	ENDPROC

	PROCEDURE command1.Click
		LOCAL lcNewItem
		lcNewItem = INPUTBOX("Enter a new first name", "ADD ITEM")
		IF !EMPTY(lcNewItem)
			INSERT INTO MyTable (names) VALUES (lcNewItem)
		ENDIF
		thisform.combo1.SetFocus() &&Refreshes the list
		thisform.Combo1.ListIndex = thisform.Combo1.ListCount
		*!* Or instead of those two lines
		*!* you could use this next line
		*!* thisform.Combo1.DisplayValue = lcNewItem
	ENDPROC

ENDDEFINE

Slighthaze = NULL
[sub]craig1442@mchsi.com[/sub][sup]
"Whom computers would destroy, they must first drive mad." - Anon​
[/sup]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top