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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Combobox List Items Replaced 1

Status
Not open for further replies.

vbaprogammer

Programmer
Sep 16, 1999
59
US
First, this problem is in WORD VBA -- I have a combobox which is driven by the value in a textbox.<br><br>Based on the value in a textbox, the original combobox list should be replaced with new list items.&nbsp;&nbsp;Instead, the new items are being added to the existing list.<br><br>I believe that the values would be updated IF the form were to be initialized, but that can't be done without losing other values in the dialog.<br><br>I have written a little code in a form which demonstrates this problem.&nbsp;&nbsp;The command button represents the textbox value change.<br>============<br>Option Explicit<br>Dim MyList() As String<br>Dim x As Integer<br><br>Private Sub CommandButton1_Click()<br>&nbsp;&nbsp;ReDim MyList(2)<br>&nbsp;&nbsp;MyList(0) = &quot;NewTest1&quot;<br>&nbsp;&nbsp;MyList(1) = &quot;NewTest2&quot;<br>&nbsp;&nbsp;MyList(2) = &quot;NewTest3&quot;<br>For x = 0 To 2<br>&nbsp;&nbsp;Me.ComboBox1.AddItem MyList(x)<br>Next<br>End Sub<br><br>Private Sub UserForm_Initialize()<br>&nbsp;&nbsp;Dim MyList(2)<br>&nbsp;&nbsp;MyList(0) = &quot;Test1&quot;<br>&nbsp;&nbsp;MyList(1) = &quot;Test2&quot;<br>&nbsp;&nbsp;MyList(2) = &quot;Test3&quot;<br>For x = 0 To 2<br>&nbsp;&nbsp;Me.ComboBox1.AddItem MyList(x)<br>Next<br>End Sub<br><br>Can anyone help me refresh/update the combox list to new information, getting rid of old list items somehow?<br><br>(Perhaps some of you are knowledgeable in both Word and Access VBA ... as I discover I must be.&nbsp;&nbsp;I can't find a forum as good as this one on Word VBA ... anybody know of one?)<br><br>Thanks,<br>Dan
 
You shouldn't have the code on the change event, as it will fire every time you type a letter. Try the lost focus event, like this: (assume your text box is named txtValue, and the combo box is named cboValues)<br><br>-----<br><br>Private Sub txtValue_LostFocus()<br>&nbsp;&nbsp;&nbsp;Dim MyList() As String<br>&nbsp;&nbsp;&nbsp;Dim x As Integer<br><br>&nbsp;&nbsp;&nbsp;Me.cboValues.Clear<br>&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;ReDim MyList(1 To 3)<br>&nbsp;&nbsp;&nbsp;MyList(1) = &quot;NewTest1&quot;<br>&nbsp;&nbsp;&nbsp;MyList(2) = &quot;NewTest2&quot;<br>&nbsp;&nbsp;&nbsp;MyList(3) = &quot;NewTest3&quot;<br>&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;For x = 1 To 3<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Me.cboValues.AddItem MyList(x)<br>&nbsp;&nbsp;&nbsp;Next<br>End Sub<br><br><br>This worked when I tested it. <p>Jim Lunde<br><a href=mailto:compugeeks@hotmail.com>compugeeks@hotmail.com</a><br><a href= Application Development
 
Thank you, you solved the problem.<br><br>You suggested placing the code in the &quot;lost focus&quot; event but Word VBA has no &quot;lost focus&quot; event.<br><br>So, I put your code into the &quot;BeforeUpdate&quot; event, and it works fine.&nbsp;&nbsp;I also took note of the &quot;Me.[Control].Clear&quot; code, which may have done it as well.<br><br>All of you guys are so clever ... thanks.<br><br>Dan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top