?
Solved

Arrays....

Posted on 2003-03-20
3
Medium Priority
?
144 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
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…
Suggested Courses
Course of the Month13 days, 19 hours left to enroll

801 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