Solved

Visual Basic  Combobox

Posted on 2002-07-08
5
242 Views
Last Modified: 2013-11-25
I have created a combobox on my form.  I have added four items to the combobox.  Once one of these items in the combobox is clicked, I need a textbox to be populated with a price associated with the item in the combobox.  I am using a select case statement in the combobox click event to accomplish this.  Is this the right direction to go?  How can I get the selected item from the combobox to send a message to the textbox once it is clicked?
Thanks
0
Comment
Question by:scitesy
5 Comments
 
LVL 100

Accepted Solution

by:
mlmcc earned 50 total points
Comment Utility
Yes, you are thinking along the correct lines.  

There are two ways to do this.  If you are using a database, tie the textbox to a datasource or create a recordset in code.  The SQL will look something like

ADODC1.RecordSource = "SELECT price FROM MyData WHERE MyData.ItemName = '" & Me.ComboBox.Text & "'"
ADODC1.Refresh

If it hardcoded in code
SELECT CASE Me.ComboBox.Text
Case "ITEM1" Me.textBoxPrice= 3.50
Case "ITEM2" Me.textBoxPrice = 4.50
' One case for each item
.
.
.
end select

good luck
mlmcc
0
 
LVL 22

Expert Comment

by:rspahitz
Comment Utility
Personally, if there's no database in use, I'd load all prices into a module-level array variable.  On click in the combobox, take the price from the array and place into the textbox (although a label might be better if the price cannot be changed.)

Private m_curPriceArray() as Currency

Private Sub Combo1_Click()
  Text1.Text = m_curPriceArray(Combo1.ListIndex)
End Sub

Private Sub Form_Load()
  ' fill combo with items, and array with prices
  ReDim m_curPriceArray(3)

  Combo1.AddItem "Item1"
  m_curPriceArray(0) = 17.34

  Combo1.AddItem "Item2"
  m_curPriceArray(1) = 23.95

  Combo1.AddItem "Item3"
  m_curPriceArray(2) = 19.45

  Combo1.AddItem "Item4"
  m_curPriceArray(3) = 9.95
end sub

0
 
LVL 3

Expert Comment

by:Bahnass
Comment Utility
Or if U has DBase in use (storing Items, Prices)
U can make use of   itemdata Property of combobox to hold Unique ID of item record
0
 
LVL 49

Expert Comment

by:DanRollins
Comment Utility
Hi scitesy,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Accept mlmcc's comment(s) as an answer.

scitesy, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept this comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
==========
DanRollins -- EE database cleanup volunteer
0
 

Expert Comment

by:SpideyMod
Comment Utility
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now