?
Solved

Arrays....

Posted on 2003-03-20
3
Medium Priority
?
145 Views
Last Modified: 2010-05-01
Hi,
Can someone please explain for me why Txthotelcost.Text keeps coming out as 0....what should be happening is the correct hotel charge should be displayed for the selected hotel in my list box...
thanks In advance

Dim HotelName(4) As String
Dim hotelcharge(4) As Integer
-------------------------------------------------------------------
Private Sub CmdHotelInfo_Click()
For ix = 1 To 4
    HotelName(ix) = InputBox("Enter Hotel Name")
    hotelcharge(ix) = InputBox("Enter Hotel Charge Per Week")
Next
End Sub
-------------------------------------------------------------------
Private Sub CmdViewHotels_Click()
For ix = 1 To 4
    LstHotels.AddItem HotelName(ix)
Next
End Sub
------------------------------------------------------------------
Private Sub LstHotels_Click()
    HotelName(0) = LstHotels.Text
    ix = Size
Do While HotelName(ix) <> LstHotels.Text
    ix = ix - 1
Loop
    Txthotelcost.Text = hotelcharge(ix)
End Sub
0
Comment
Question by:DaveChadwick
3 Comments
 
LVL 43

Accepted Solution

by:
TimCottee earned 1000 total points
ID: 8173011
Hi DaveChadwick,

An easier way is this:

Private Sub LstHotels_Click()
    Txthotelcost.Text = HoteCharge(LstHotels.ListIndex + 1)
End Sub

The .ListIndex property of the list control tells you which one has been selected, this starts at zero so you need to add one to it.

I am assuming that you have Option Base 1 set for this program as otherwise your array will start at 0 and you are looping from 1 to 4 which would normally generate a subscript error.

Tim Cottee MCSD, MCDBA, CPIM
http://www.timcottee.tk 

Brainbench MVP for Visual Basic
http://www.brainbench.com

Experts-Exchange Advisory Board Member
0
 
LVL 5

Expert Comment

by:Cimperiali
ID: 8173013
Option Explicit

Dim HotelName(4) As String
Dim hotelcharge(4) As Integer
'-------------------------------------------------------------------
Private Sub CmdHotelInfo_Click()
Dim ix As Integer
For ix = 0 To 3
   HotelName(ix) = InputBox("Enter Hotel Name")
   hotelcharge(ix) = InputBox("Enter Hotel Charge Per Week")
Next
End Sub
'-------------------------------------------------------------------
Private Sub CmdViewHotels_Click()
Dim ix As Integer
For ix = 0 To 3
   LstHotels.AddItem HotelName(ix)
Next
End Sub

'------------------------------------------------------------------
Private Sub LstHotels_Click()
Dim ix As Integer
ix = UBound(HotelName)
   'HotelName(0) = LstHotels.Text
 '  ix = size
Do While HotelName(ix) <> LstHotels.Text
   ix = ix - 1
Loop
   Txthotelcost.Text = hotelcharge(ix)
End Sub

0
 

Author Comment

by:DaveChadwick
ID: 8173042
That works perfectly....thankyou very much!!
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

578 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