Click to See Complete Forum and Search --> : Hot to add an item to a combo box from a textbox?


CCDriver
March 21st, 2001, 05:31 PM
Hi All

How do i add an item from a textbox on one form to a combobox on another form?
I thought you called the form name at the start like this:
frmMain.cboCombo1.AddItem (TextToAdd$)
but this doesn't work, can someone help please.

I need to do this when i click the Ok button and close the form. There are four other textbox's on the form as well which need the items in them to be placed in the corresponding textbox's on the frmMain form.

John G Duffy
March 21st, 2001, 06:18 PM
I have no problems at all with this simple sub being executed from Form1

private Sub Command1_Click()
Form2.Combo1.AddItem Text1.Text
End Sub



What is TextToADD$? It's not a TextBox name. If it's a variable, it probably is not getting set properly.

John G

CCDriver
March 21st, 2001, 07:16 PM
Thanks for the quick responce John G.

Dim TextToAdd$

Yes it's a variable i have set and yes i figured it's not getting set properly but i'm not sure why.

I notice you are calling the Text1 textbox directly and puting the item in it, maybe thats why....doh!! :)
I'll try it and let you know later on.

Thank you

d.paulson
March 21st, 2001, 07:58 PM
The reason that your variable is not being accessed in form2 is that you have only dimmed it as module level scope (it will only work in the form that it was dimmed). You could dim this variable as public (Public TextToAdd$)
and in form 2 you can reference it as form1.TextToAdd$, but in your circumstance I would reference the text box as John showed you. It will leave you with a much cleaner and more understandable code.

David Paulson

CCDriver
March 22nd, 2001, 06:02 AM
Thanks for your help guys

I too was able to get the example like you did John G to work ok and add text from one form to the other and then back again, but it wouldn't work in the program. Stuffed if i could work out why.
What the problem was, i was naming the "Form1" i think and when i created this project the wizard named the main form as frmMain in the module1 (which i didn't see).
It all works now and places text in the right box, the variable works as i have set it originally, as TexToAdd$

David:

The variable was in form2 and it works ok in form2 but not in form1 (other way round to what you have got it). But that explains why i can't access that variable now in frmMain, if i make that Public instead of Private i should be able to use it, shouldn't i?

Thank you two very much for your help, i was most likely tired after staring at lines of code and couldn't see the wood for the trees.

CCDriver
March 24th, 2001, 05:48 AM
Thanks for your help guys, but i ran into another problem.
How do i get the text that i put in the combo1 box to be already selected? all the other text box's fill up ok with the data i type in the dialog box but i can't get the text to display straight away in the combo1 box after coming back from the dialog box.

d.paulson
March 24th, 2001, 07:54 AM
Add this. Change to suit your needs.
combo1.text = texttoadd$

David Paulson