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

ListBox problem!!!!!!

Status
Not open for further replies.

Cody

Programmer
Jan 20, 2000
4
US
I have a list box that lists medications and meds codes. The meds codes are decimal numbers (e.g. 123.4). The itemdata property does not accept decimal number. How can I make a selection from the list and put the selection in a textbox?<br>
Can I use the Tag property? If, how? Please help!!!!! Thanks to everyone who can help!
 
Are the medications in a database?<br>
if so are you using dblist?<br>
I always have trouble getting the dblistbox to work<br>
If so try using the dbgrid instead of listbox<br>
it is much more flexible<br>
Attach it to the datacontrol<br>
then right click on it to retrieve fields<br>
<br>
<br>
To click on the item to select it:<br>
use the RowColChange Event not the Click event see below<br>
<br>
Private Sub DBGrid1_RowColChange(LastRow As Variant, ByVal LastCol As Integer)<br>
Text1 = DBGrid1.Columns(0).Value<br>
End Sub<br>
<br>
To set up more than 2 columns<br>
Right Click in the DBGrid to show options<br>
<br>
---------------------------Other Grid stuff------------------<br>
' get value from one cell<br>
PageGroup = TDBGridS1.Columns(0).Value<br>
<br>
'set the caption of the Grid<br>
DIALER.TDBGridS2.Caption = &quot;[GROUP (&quot; & PageGroup & &quot;) PAGE Mode]&quot;<br>
<br>
' set SQL to select group<br>
DIALER.Data2.RecordSource = &quot;SELECT * FROM PagerPhoneNum WHERE [Group] = &quot; & Chr$(34) & PageGroup & Chr$(34) & &quot;;&quot;<br>
DIALER.Data2.Refresh
 
Thanks DougP,<br>
yes, the meds come from an oracle database and I am actually using a regular listbox<br>
not a dblistbox. Is there any way I can use the regular listbox?( I am also not using adodc, I'm writing everything in sql code.) Or would it be easier to just use the <br>
dbgrid? Thanks for your comments.<br>

 
Yes you can use a regular list box<br>
<br>
List1.additem Med1 & “¦“ Med2<br>
Notice there is a PIPE symbol in between them<br>
To get an item out of there <br>
To get Med1 back out<br>
<br>
Private Sub Form_Load()<br>
List1.AddItem &quot;Stuff1 ¦ Stuff2&quot;<br>
End Sub<br>
<br>
Private Sub List1_Click()<br>
Dim findPipe As Integer<br>
findPipe = InStr(1, List1.Text, &quot;¦&quot;)<br>
‘ this gets Med 1 back out of list<br>
Text1 = Left(List1.Text, findPipe - 1)<br>
‘ this gets med2 back out<br>
Text2 = Right(List1.Text, Len(List1.Text) - findPipe - 1)<br>
End Sub<br>
<br>

 
Hi DougP,<br>
that looks good but I still don't know how to implement this in my code. Here is my <br>
formload code:<br>
List1.Clear<br>
rsMeds.MoveFirst<br>
With rsMeds<br>
While Not .EOF<br>
vntTemp = rsMeds!desc_txt & Space(55 - Len(rsMeds!desc_txt) - Len(rsMeds!meds_cd)) & rsMeds!meds_cd<br>
If IsNull(vntTemp) Then<br>
vntTemp = ""<br>
End If<br>
List1.AddItem vntTemp<br>
List1.ItemData(List1.NewIndex) = rsMeds!meds_cd<br>
rsMeds.MoveNext<br>
Wend<br>
End With<br>
<br>
<br>
and here is my click event:<br>
For i = 0 To List1.ListCount - 1<br>
MsgBox List1.ItemData(i) & ": " & List1.Selected(i)<br>
If List1.Selected(i) = True Then<br>
SearchMeds = List1.ItemData(i)<br>
Unload Me<br>
Set frmMedsList = Nothing<br>
LoadPickMeds.LoadPickMeds<br>
frmLipidTabs.Show<br>
Exit Sub<br>
End If<br>
Next<br>
<br>
and here is my LoadPickMeds module which puts my selection into the textbox:<br>
If rsMeds.RecordCount &gt; 0 Then<br>
rsMeds.MoveFirst<br>
While Not rsMeds.EOF<br>
frmLipidTabs.txtMeds.Text = rsMeds!desc_txt<br>
rsMeds.MoveNext<br>
Wend<br>
End If<br>
<br>
<br>

 
I think you complicating it more than it is.<br>
add both of your items in the additem and separate them with a PIPE symbol<br>
List1.AddItem vntTemp & &quot;¦&quot; & rsMeds!meds_cd<br>
<br>
So forget the List1.itemdata<br>
Do you not want people to see the &quot;rsMeds!meds_cd<br>
&quot; field?<br>
Then make the list box skinny so only the item you to see is exposed (which is the first item in the additem) the other piece is still there but invisible cause the list box is too narrow to view it.<br>
<br>
Now when you click in the list box it returns something in List1.text. <br>
So to get the &quot;rsMeds!meds_cd&quot; back out of list1.text <br>
<br>
use this<br>
<br>
findPipe = InStr(1, List1.Text, &quot;¦&quot;)<br>
frmLipidTabs.txtMeds.Text = Right(List1.Text, Len(List1.Text) - findPipe - 1)<br>
<br>
It gets the value back out of the list1.text when it is selected.<br>
<br>
See you are opening a recordset, then populating a listbox, then manipulating a listbox selection to get a value back out.<br>
<br>
Instead just connect the recordset to a DBGrid and you won't need all that code to make a listbox do what a DBgrid is designed to do in the first place (show rows and columns of data)<br>
the only code you need is when you select the item you want in a dbgrid.<br>
<br>
Private Sub DBGrid1_RowColChange(LastRow As Variant, ByVal LastCol As Integer)<br>
frmLipidTabs.txtMeds.Text = DBGrid1.Columns(1).Value<br>
End Sub<br>
<br>
were (0) is the first column (1) is the second<br>

 
Thanks so much DougP!!!!<br>
The grid works just fine and is a lot easier! I was just so fixated on that listbox because I have been working with it all along. But you solved my problem and I thank you very much for that. I'll remember you in the future with "my" problems.<br>

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top