?
Solved

Arrays Again.....

Posted on 2003-03-21
30
Medium Priority
?
170 Views
Last Modified: 2010-05-01
Hi,
Can somebody please tell me how i can adjust this code so that i can add an unlimited number of items to the arrays.......thanx!!

Option Explicit
Option Base 1
Dim HotelName(4) As String
Dim hotelcharge(4) As Integer
Dim ix 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_dblClick()
    Txthotelcost.Text = hotelcharge(LstHotels.ListIndex + 1)
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
  • 13
  • 9
  • 3
  • +3
30 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 8180399
Option Explicit
Option Base 1

const Array_Start as Long = 1
dim Array_Size as long
Dim HotelName() As String
Dim hotelcharge() As Integer
Dim ix As Integer


Private Sub Form_Load()
   ChangeArraySize 4
End Sub

Public Sub ChangeArraySize(s as long)
  Array_Size = s
  if s >= Array_Start then
    redim preserve HotelName(Array_Start to ArraySize) as string
    redim preserve HotelCharge(Array_Start to ArraySize) as Integer
  end if
End Sub
----------------------------------------------------------------
Private Sub CmdHotelInfo_Click()

For ix = Array_Start To Array_Size
   HotelName(ix) = InputBox("Enter Hotel Name")
   hotelcharge(ix) = InputBox("Enter Hotel Charge Per Week")
Next
End Sub
----------------------------------------------------------------
Private Sub CmdViewHotels_Click()
For ix = Array_Start To Array_Size
   LstHotels.AddItem HotelName(ix)
Next
End Sub
----------------------------------------------------------------
Private Sub LstHotels_dblClick()
   Txthotelcost.Text = hotelcharge(LstHotels.ListIndex + 1)
End Sub


Now you can use the function ChangeArraySize to resize the array to "any" size

Cheers
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 8180408
Hi DaveChadwick,

Dim HotelName() As String
Dim HotelCharge() As Integer

Private Sub CmdHotelInfo_Click()
    ReDim Preserve HotelName(UBound(HotelName) + 1)
    ReDim Preserve HotelCharge(UBound(HotelCharge) + 1)
    HotelName(ix) = InputBox("Enter Hotel Name")
    hotelcharge(ix) = InputBox("Enter Hotel Charge Per Week")
End Sub

Private Sub cmdViewHotels_Click()
    LstHotels.Clear
    For ix = LBound(HotelName) To UBound(HotelName)
      lstHotels.AddItem HotelName(ix)
    Next
End Sub

But of course if your charge is always an integer, you can do away with that array and store it in the itemdata property of the list control:

Private Sub cmdViewHotels_Click()
    LstHotels.Clear
    For ix = LBound(HotelName) To UBound(HotelName)
      lstHotels.AddItem HotelName(ix)
      lstHotels.ItemDate(lstHotels.NewIndex) = HotelCharge(ix) 'adds the integer value to the itemdata property for each row
    Next
End Sub

Then in the next sub

Private Sub lstHotels_dblClick()
    txtHotelCost.Text = lstHotels.ItemData(lstHotels.ListIndex)
End Sub

This would do away with the arrays assuming that you simply added the two values directly into the listbox control.


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

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

Experts-Exchange Advisory Board Member
0
 

Author Comment

by:DaveChadwick
ID: 8180497
Hi there....
Is it possible to use a do loop to keep adding items to the array until say for example Hotelname = ""....i thought it was but for the life of me i can't get it to work.......i think thats what i'm looking for it to do
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 43

Expert Comment

by:TimCottee
ID: 8180537
Private Sub CmdHotelInfo_Click()
  Dim strHotelName As String
  Do
    strHotelName = InputBox("Enter Hotel Name")
    If strHotelName <> "" Then
      ReDim Preserve HotelName(UBound(HotelName) + 1)
      ReDim Preserve HotelCharge(UBound(HotelCharge) + 1)
      HotelName(ix) = strHotelName
      hotelcharge(ix) = InputBox("Enter Hotel Charge Per Week")
    End If
  Loop Until strHotelName = ""
End Sub
0
 

Author Comment

by:DaveChadwick
ID: 8180597
Thats it but i keep getting run time error 9 on this line
ReDim Preserve HotelName(UBound(HotelName) + 1), nightmare!!
----------------------------------------------------
0
 

Author Comment

by:DaveChadwick
ID: 8180603
Thats it but i keep getting run time error 9 on this line
ReDim Preserve HotelName(UBound(HotelName) + 1), nightmare!!
----------------------------------------------------
0
 

Author Comment

by:DaveChadwick
ID: 8180606
Thats it but i keep getting run time error 9 on this line
ReDim Preserve HotelName(UBound(HotelName) + 1), nightmare!!
----------------------------------------------------
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 8180622
Of course, if it is not dimensioned to start with you will.

You can add a check like this:

If Len(Join(HotelName,"")) = 0 Then
  ReDim HotelName(1)
  ReDim HotelCharge(1)
Else
  ReDim Preserve HotelName(UBound(HotelName) + 1)
  ReDim Preserve HotelCharge(UBound(HotelName) + 1)
End If

The join statement simply creates a string from the array which if it has no length implies the array had no elements so we are starting from empty. Otherwise redim preserving the previous elements and add a new element.
0
 
LVL 5

Accepted Solution

by:
avya2k earned 1500 total points
ID: 8180630
i tried this as follows


Option Explicit
Option Base 0
Dim HotelName() As String
Dim hotelcharge() As Integer
Dim ix As Integer
'----------------------------------------------------------------
Private Sub CmdHotelInfo_Click()
'For ix = 1 To 4
If (UBound(HotelName) < 0) Then
    ix = 1
Else
    ix = UBound(HotelName) + 1
End If
ReDim Preserve HotelName(ix) As String
ReDim Preserve hotelcharge(ix) As Integer
   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 UBound(HotelName)
   LstHotels.AddItem HotelName(ix)
Next
End Sub

Private Sub Form_Load()
    ReDim Preserve HotelName(0) As String
    ReDim Preserve hotelcharge(0) As Integer
End Sub

'----------------------------------------------------------------
Private Sub LstHotels_dblClick()
   Txthotelcost.Text = hotelcharge(LstHotels.ListIndex + 1)
End Sub

0
 
LVL 7

Expert Comment

by:fluglash
ID: 8180638
Private Sub CmdHotelInfo_Click()
 Dim strHotelName As String
On Error Resume Next

 Do
   strHotelName = InputBox("Enter Hotel Name")
   If strHotelName <> "" Then
     ReDim Preserve HotelName(UBound(HotelName) + 1)
       If Err.Number = 9 Then ReDim Preserve HotelName(0)
     ReDim Preserve HotelCharge(UBound(HotelCharge) + 1)
       If Err.Number = 9 Then ReDim Preserve HotelName(0)
     HotelName(ix) = strHotelName
     hotelcharge(ix) = InputBox("Enter Hotel Charge Per Week")
   End If
 Loop Until strHotelName = ""
End Sub
0
 

Author Comment

by:DaveChadwick
ID: 8180969
ok, so this is my new code.....however now it won't add my array to my listbox....how i wish i hadn't agreed to do this!!!


Option Explicit
Option Base 1
Dim HotelName() As String
Dim hotelcharge() As Integer
Dim ix As Integer
-----------------------------------------
Private Sub CmdHotelInfo_Click()
Dim strHotelName As String
On Error Resume Next
Do
  strHotelName = InputBox("Enter Hotel Name")
  If strHotelName <> "" Then
    ReDim Preserve HotelName(UBound(HotelName) + 1)
      If Err.Number = 9 Then ReDim Preserve HotelName(0)
    ReDim Preserve hotelcharge(UBound(hotelcharge) + 1)
      If Err.Number = 9 Then ReDim Preserve HotelName(0)
    HotelName(ix) = strHotelName
    hotelcharge(ix) = InputBox("Enter Hotel Charge Per Week")
  End If
Loop Until strHotelName = ""
--------------------------------------------------------
End Sub
Private Sub CmdViewHotels_Click()
 LstHotels.Clear
   For ix = 1 To UBound(HotelName)
     LstHotels.AddItem HotelName(ix)
   Next
End Sub
--------------------------------------------------
Private Sub LstHotels_dblClick()
    Txthotelcost.Text = hotelcharge(LstHotels.ListIndex + 1)
End Sub
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 8181086
this line:

If Err.Number = 9 Then ReDim Preserve HotelName(0)


has the effect of completely CLEARING the array, and I doubt that that is what is intended

You need to identify WHY you get the Runtime Error 9, and NOT simply handle it in this VERY UNUSUAL manner.

in fact, I have used the full code avya2K posted most recently and cannot get it to fail...go figure.

AW
0
 
LVL 7

Expert Comment

by:fluglash
ID: 8181087
Private Sub CmdHotelInfo_Click()
Dim strHotelName As String
On Error Resume Next
Do
 strHotelName = InputBox("Enter Hotel Name")
 If strHotelName <> "" Then
   ReDim Preserve HotelName(UBound(HotelName) + 1)
     If Err.Number = 9 Then ReDim Preserve HotelName(0)
   ReDim Preserve hotelcharge(UBound(hotelcharge) + 1)
     If Err.Number = 9 Then ReDim Preserve HotelName(0)
   HotelName(Ubound(HotelName)) = strHotelName
   hotelcharge(Ubound(HotelCharge)) = InputBox("Enter Hotel Charge Per Week")
 End If
Loop Until strHotelName = ""
End Sub

ix in your sub does not change!!! you assign value only to ix element of array. Above is a correct version
0
 
LVL 7

Expert Comment

by:fluglash
ID: 8181150
Arthur_Wood:

You speak like a man who had never done it!!!

Program runs this code only if err.number = 9...

Err.Number = 9 only for the first time, i don`t know why but it`s a fact. Every time except first programs doesn`t do it!!!
0
 

Author Comment

by:DaveChadwick
ID: 8181301
Well i am completeley confused!!
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 8181349
fluglash---I copied the code I identified(03/21/2003 05:36AM PST above), (which DOES NOT have the "If err.Number=9 ..."lines) and NEVER got any error to occur...  I have run that code, in VB 6 IDE, without any error happening, so there is something else going on.  And in any case, that way of handling the error is not a very good idea.

For instance, what are the vlues of the relevant paramerters, if and when the error condition is detected?  That may lead you to figuring out WHY the error is happening.

And I have been programming (and developing apps in VB 6) long enough to know what I am doing....

AW

0
 

Author Comment

by:DaveChadwick
ID: 8181444
But i thought i had to use a DO UNTIL loop.....to acheive what i want to acheive.......
0
 
LVL 7

Expert Comment

by:fluglash
ID: 8181446
i think this error raised when you`re trying to ReDim not initialized array

how do you handle errors???
0
 
LVL 7

Expert Comment

by:fluglash
ID: 8181492
what`s the difference for you use Do Until...Loop or Do...Loop Until


Dave:
Does the code work?
0
 

Author Comment

by:DaveChadwick
ID: 8181525
ok right........the code works...it will keep letting me enter data untill the input box is blank....however it won't display the array in my list box now...
0
 
LVL 7

Expert Comment

by:fluglash
ID: 8181610
you mean you don`t want to display it, or it isn`t displayed?
0
 

Author Comment

by:DaveChadwick
ID: 8181649
ok i have two command buttons.....the first runs the code to allow me to enter items into my array using inputboxes (this should keep allowing me to enter items into the array until the input box is blank)....then i have another command button to add the items of the hotelname array into a list box....on clicking the hotel name in the list box the charge for this hotel is displayed in a text box......the original code i entered on this psot acheives this fine....however it will only allow me to enter 4 items into the array......whereas i want to be able to keep adding items until the input box is blank eg hotelname = "".....
0
 
LVL 7

Expert Comment

by:fluglash
ID: 8181823
if you`ve changed this two lines:

Dim HotelName(4) As String
Dim hotelcharge(4) As Integer

for:

Dim HotelName() As String
Dim hotelcharge() As Integer

then it will allow you to enter much more elements than you`ll have to
0
 

Author Comment

by:DaveChadwick
ID: 8181962
yeah i have......this is what i have changed my code to:
it won't add the contenets of HotelName to LstHotels

Option Explicit
Option Base 1
Dim HotelName() As String
Dim hotelcharge() As Integer
Dim ix As Integer
-----------------------------------------
Private Sub CmdHotelInfo_Click()
Dim strHotelName As String
On Error Resume Next
Do
strHotelName = InputBox("Enter Hotel Name")
If strHotelName <> "" Then
  ReDim Preserve HotelName(UBound(HotelName) + 1)
    If Err.Number = 9 Then ReDim Preserve HotelName(0)
  ReDim Preserve hotelcharge(UBound(hotelcharge) + 1)
    If Err.Number = 9 Then ReDim Preserve HotelName(0)
  HotelName(Ubound(HotelName)) = strHotelName
  hotelcharge(Ubound(HotelCharge)) = InputBox("Enter Hotel Charge Per Week")
End If
Loop Until strHotelName = ""
End Sub
-------------------------------------------
Private Sub CmdViewHotels_Click()
LstHotels.Clear
  For ix = 1 To UBound(HotelName)
    LstHotels.AddItem HotelName(ix)
  Next
End Sub
--------------------------------------------------
Private Sub LstHotels_dblClick()
   Txthotelcost.Text = hotelcharge(LstHotels.ListIndex + 1)
End Sub
0
 
LVL 7

Expert Comment

by:fluglash
ID: 8182004
Change this:

If Err.Number = 9 Then ReDim Preserve HotelName(0)

for:

If Err.Number = 9 Then ReDim Preserve HotelName(1)
0
 

Author Comment

by:DaveChadwick
ID: 8182104
Now it will add the last Item i added to the array into my list box....but ONLY the last item....but it won't show the charge....
i think the proble lies now:
Here:
Private Sub CmdViewHotels_Click()
LstHotels.Clear
 For ix = 1 To UBound(HotelName)
   LstHotels.AddItem HotelName(ix)
 Next
End Sub

and here:
Private Sub LstHotels_dblClick()
  Txthotelcost.Text = hotelcharge(LstHotels.ListIndex + 1)
End Sub


0
 

Author Comment

by:DaveChadwick
ID: 8182185
A ha it won't show the charge cos of this line:
ReDim Preserve hotelcharge(UBound(hotelcharge) + 1)
   If Err.Number = 9 Then ReDim Preserve HotelName(0)
                               should be hotelcharge(1)

So the proplem now is here:
Private Sub CmdViewHotels_Click()
LstHotels.Clear
For ix = 1 To UBound(HotelName)
  LstHotels.AddItem HotelName(ix)
Next
End Sub
0
 
LVL 7

Expert Comment

by:fluglash
ID: 8182225
I`m sorry:

Private Sub CmdHotelInfo_Click()
Dim strHotelName As String
On Error Resume Next
Do
strHotelName = InputBox("Enter Hotel Name")
If strHotelName <> "" Then
 ReDim Preserve HotelName(UBound(HotelName) + 1)
   If Err.Number = 9 Then ReDim Preserve HotelName(0)
 ReDim Preserve hotelcharge(UBound(hotelcharge) + 1)
   If Err.Number = 9 Then ReDim Preserve hotelcharge(0)
 HotelName(Ubound(HotelName)) = strHotelName
 hotelcharge(Ubound(HotelCharge)) = InputBox("Enter Hotel Charge Per Week")
End If
Loop Until strHotelName = ""
End Sub
0
 
LVL 5

Expert Comment

by:avya2k
ID: 8185300
my code never thrown me any error of number 9
0
 

Author Comment

by:DaveChadwick
ID: 8185962
But your code will only allow 4 entries into the array....or more if you change the code.....i need to be able to keep adding items untill the inputbox is blank.....
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month9 days, 17 hours left to enroll

762 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