meganles 10-07-2001, 02:56 AM Hi,
Can someone please help me with my problem? I have to search the 2d array: seating(1 to 10, 1 to 4), where to the user 1 To 4, is A to B. I have to search for a location that is entered in by the user,eg. I have to find seating(1,1) if the user enters (1,A). I am a bit confused as to how I convert the letter to a number, so that I can search the array!! Can someone please give me a clue??
Thanks
Banjo 10-07-2001, 04:32 AM Try this (65 is ascii code for 'A'):
<pre>seating(FirstDim, Asc(UCase(SecondDim)) - 64)</pre>
Banjo 10-07-2001, 04:38 AM To expand on my last post, FirstDim is a number to reference the first dimension of the array and SecondDim is the letter to reference the second dimension.
The code above converts the letter to upper case and then obtains the ascii value. A -> D gives ascii values of 65 -> 68, so you need to subtract 64 to get index values 1 -> 4.
meganles 10-07-2001, 06:45 AM Sorry, but I am finding this hard to do and extremely frustrating!!
This is the code:
seatPosition = InputBox(Enter seat location. Eg. 1,A)
For row = 1 To 10
For seat = 1 To 4
If Val(seatPosition) = Val(seating(row, Asc(UCase(seat)) - 64)) Then....
It is saying subscript out of range!! Can you please tell me what I am doing wrong??
Banjo 10-07-2001, 06:54 AM I assume the point here is to work out which element in array the user wants. If this is the case then you do not need the for loops. Just this:
<pre>Dim seatLetter as string
row = Val(Left(Trim(seatPosition), 1))
seatLetter = Trim(Mid(seatPosition, Instr(seatPosition, ",") + 1, 1))
seat = Asc(UCase(seatLetter)) - 64</pre>
meganles 10-07-2001, 07:33 AM thankyou so much for your help with this matter as it is now working,...but I have one more question that I hope you can answer,..:How do I search the array to see if the seat location chosen is vacant, and if so, get the user name into the location? I am so at a loss!! Can you help???
Banjo 10-07-2001, 07:40 AM If it is an array of strings then:
<pre>If seating(row, seat) = "" Then
seating(row, seat) = "Customer Name"
Else
Msgbox "This seat is taken!"
End If</pre>
meganles 10-07-2001, 09:43 AM It doesn't want to work because the error I keep getting is : <subscript out of range>?? Any ideas?
Volte 10-07-2001, 10:09 AM check to make sure that you have made the array big enough to hold all of the information.
meganles 10-07-2001, 10:15 AM I put it above the last command lines, that's why I kept getting the error!! But now after putting it in the right place it just doesn't add the passenger name to the list, let alone to the designated seat!! I'm at a total loss.. Can you help??
Thinker 10-07-2001, 10:23 AM You should post your code as it stands right now.
I think therefore I am... sometimes right. images/icons/wink.gif
meganles 10-07-2001, 10:27 AM Heren is the whole lot can you tell me where it has gone wrong? I know ot is something really simple but Pllleeaaaassssse humour me anyway: Option Explicit
Dim seating(1 To 10, 1 To 4) As String 'Array Declared
Dim waitlist() As String
'Purpose: To add or delete flight bookings and also add clients to a waiting list if flight full
'Input: Input from bookings.dat and waitlist.dat
'Output: Passenger flight details
Private Sub Form_Load()
Dim row As Single
Dim seat As Single
Dim bookings As String
bookings = App.Path & "\bookings.dat.txt"
Call PrintHeading
'Display passenger Names in seating locations
Open bookings For Input As #1
For row = 1 To 10
For seat = 1 To 4
Input #1, seating(row, seat)
Next seat
Call printBlank
picResults.Print Val(row); Tab(10); seating(row, 1);
picResults.Print Tab(33); seating(row, 2);
picResults.Print Tab(54); seating(row, 3);
picResults.Print Tab(79); seating(row, 4)
Next row
Close #1
End Sub
Private Sub CmdAdd_Click()
Dim bookings, prompt, title, title1, pmt, ttle, seatPosition, strMsg1 As String
Dim counter As Integer, row, seat As Single
Dim enterName, strMsg, prompt2, title2 As String
Dim buttons As String
Dim found As Boolean
Dim seatLetter As String
buttons = 1 + 0 + 16
'Prompt user to enter name in input box
prompt = "Enter the Passenger Name, Eg, Surname, FirstName"
title = "Passenger Information"
'Message if user is already booked
strMsg = "Passenger by that name has already been booked"
title1 = "Duplicate Entry"
'Prompt user to select the seat position they want
pmt = "Enter desired seat location. Eg. 1,A"
ttle = "Seat Location"
'Message if user selects a seat that is already booked
strMsg1 = "The seat you selected is already booked"
'Search array for passenger name
enterName = InputBox(prompt, title)
For row = 1 To 10
For seat = 1 To 4
If UCase(seating(row, seat)) = UCase(enterName) Then
found = True
Else
counter = counter + 1
End If
Next seat
Next row
If found = True Then
'Passenger is already booked
MsgBox strMsg, buttons, title1
Else
'Enter desired seat location
seatPosition = InputBox(pmt, ttle)
End If
row = Val(Left(Trim(seatPosition), 1))
seatLetter = Trim(Mid(seatPosition, InStr(seatPosition, ",") + 1, 1))
seat = Asc(UCase(seatLetter)) - 64
If seating(row, seat) = "" Then
seating(row, seat) = "Customer Name"
Else
MsgBox "This seat is taken!"
End If
End Sub
Private Sub cmdCancel_Click()
'Delete a booking
Dim prompt, strMsg1, strMsg2, title, enterName As String
Dim counter As Integer
Dim row, seat As Single
'Request passengers name
prompt = "Enter the Passenger Name"
title = "Passenger Information"
strMsg1 = "Passenger name could not be found"
'Confirmation that the booking has been deleted
strMsg2 = "Passenger booking has been removed"
'prompt user to enter name in Input box
enterName = InputBox(prompt, title)
'Search array for passenger name
counter = 0
For row = 1 To 10
For seat = 1 To 4
If UCase(seating(row, seat)) = UCase(enterName) Then
seating(row, seat) = ""
MsgBox strMsg2
Else
counter = counter + 1
End If
Next seat
Next row
If (counter = 40) Then
'Passenger name could not be found
MsgBox strMsg1
End If
picResults.Refresh
Call GetWaitlist
End Sub
Private Sub GetWaitlist()
Dim NumList, x As Integer, waiting As String
waiting = App.Path & "\waitlist.dat.txt"
NumList = 0
Open waiting For Input As #1
Do While Not (EOF(1))
Input #1, waitlist
NumList = NumList + 1
Loop
Close #1
ReDim Preserve waitlist(1 To NumList) As String
Open waiting For Input As #1
For x = 1 To NumList
Input #1, waitlist
Next x
Close #1
End Sub
Private Sub cmdQuit_Click()
End
End Sub
Private Sub cmdSave_Click()
'Save the Booking file
End Sub
Private Sub printBlank()
picResults.Print ""
End Sub
Private Sub PrintHeading()
'Print Headings
Dim i As Integer
Call printBlank
picResults.FontUnderline = True
picResults.FontBold = True
picResults.Print Tab(32); "Flight Booking Plan"
picResults.FontUnderline = False
picResults.FontBold = False
picResults.Print "" 'Print blank line
picResults.Print Tab(10); "A";
picResults.Print Tab(33); "B";
picResults.Print Tab(54); "C";
picResults.Print Tab(79); "D"
End Sub
Thinker 10-07-2001, 11:09 AM I can't see all the different problems you might be having but I know
there are more than one.
To begin with, you need to declare your variables more explicitly.
<pre>
Dim bookings, prompt, title, title1, pmt, ttle, seatPosition, strMsg1 As String
Dim counter As Integer, row, seat As Single
Dim enterName, strMsg, prompt2, title2 As String
</pre>
In this declaration, bookings, prompt, title, title1, pmt, ttle, seatPosition,
row, entername, strMsg, and prompt2 default to Variant because
you haven't explicitly said what they are.
Another thing is the assignment of the customer name to the array
<pre>
If seating(row, seat) = "" Then
seating(row, seat) = "Customer Name" ' This should be EnterName
Else
MsgBox "This seat is taken!"
End If
</pre>
Another problem with the buttons on your MsgBox
<pre>
Dim buttons As String
...
buttons = 1 + 0 + 16
</pre>
buttons as a string will now be equal to "1016". This is not what the
MsgBox is expecting at all.
It should be like this
<pre>
Dim buttons As VbMsgBoxStyle
...
buttons = vbOKCancel Or vbCritical '1 and 16
</pre>
Another problem is that you are doing no error checking when you convert
the letter to a number
<pre>
row = Val(Left(Trim(seatPosition), 1))
seatLetter = Trim(Mid(seatPosition, InStr(seatPosition, ",") + 1, 1))
seat = Asc(UCase(seatLetter)) - 64
</pre>
If row is 10, this code fails because it only gets the 1. If E is entered,
seat will be 5 which is outside the array bounds.
By the way, you declared row and seat as single, they will be more efficient
as Integer.
I am sure there are more problems I haven't pointed out but you are
going to need to fix things and debug each problem as you go along.
I think therefore I am... sometimes right. images/icons/wink.gif
meganles 10-07-2001, 11:32 AM I cannot thank you enough Thinker my gratitude is yours, THANKYOU!!!!!!!!!!!!!!
|