Solved

# Arrays....

Posted on 2003-03-20
Medium Priority
145 Views
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...

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
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

LVL 43

Accepted Solution

TimCottee earned 1000 total points
ID: 8173011

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

0

LVL 5

Expert Comment

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
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

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

## Featured Post

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
Course of the Month12 days, 23 hours left to enroll