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

Retreiving Data from a List Box 2

Status
Not open for further replies.

InsaneProgrammer

Programmer
Jan 17, 2001
44
US
I am creating an application that will have a lot of users and I need to minimize the number of trips the program makes to the database. I am trying to make the program execute on the client machine as much as possible. When the user initially logs on the program retrieves a recordset from the database, dumps the data into a list box, and promptly terminates the connection. The list box isn't visible to the user, it is only being used to hold the recordset. I need to have the first row of the list box populate a label. When the user clicks on the next button I need the label to be populated with the second value in the list box and so on. Anyone know how to do this? Below is the code I use to populate the list box.
Code:
Do Until rsScope.EOF
        With lstQuestion
                .AddItem rsScope("Question")
        End With
        rsScope.MoveNext      
Loop
 
Hi!

There may be an easier way, but one thing you can do is add a hidden text box to the form. In the Form_Load put

txtCount = 0

and in the Form_Current put:

lblQuestion = lstQuestion.Columns(0, txtCount.Value)
txtCount = txtCount + 1

That should put the correct row in each time. You will probably want some way to stop the user from going foward if there are no more questions

hth
Jeff Bridgham
 
jebry,

I tried your solution and got an error. The message says, "Wrong number of arguments or invalid property assignment." It is highlighting ".columns". Any ideas?
 
Use the ListBox.ListIndex property. When the listbox first gets populated, set ListIndex = 0 (-1 means nothing is selected, 0 is the first item). Then when the user hits the Next button, increment the ListIndex by one. Once the ListIndex is set properly, you can use the ListBox.List(Index) property to get the text:

Private Sub cmdNext_Click()
lstQuestion.ListIndex = lstQuestion.ListIndex + 1
lblQuestion.Caption = lstQuestion.List(lstQuestion.ListIndex)
End Sub

If the last item in the list is selected and the user presses the cmdNext button you will get an error, so be sure to check for that. Otherwise that should get you started.

Good Luck!

-Mike Difference between a madman and a genius:
A madman uses his genius destructively,
A genius uses his madness constructively.
 
I don't know what you're using to get your record set, but if you were to use ADO, open a static type record set, close the connection(you now have a "Disconnected RecordSet), and use the rs instead of the listbox:

Private Sub cmdNext_Click()
If Not rsScope.EOF And Not rsScope.BOF
label1.Caption = rsScope("Question")
rsScope.MoveNext
End If
End Sub Tim

Remember the KISS principle:
Keep It Simple, Stupid!
 
Hi!

The problem is it should be column with no s. Sorry! Mike's solution looks easier anyway! :)

Jeff Bridgham
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top