A couple of errors...
#1 The Mid$ function returns a sub string as follows
subSt=Mid$(WholeSt, Begining, length)
so say WholeSt="This is a test" and I wanted "a test" I would do
subSt=Mid$(WholeSt, 9, 6)
since it looked like you were just trying to get the last bit of the passengerID "WallaceW48P01" --> "W48P01" you can change things to Right$(PassemgerId,6)
(Edit) OOpse it lookes like the FinalID is supposed to be the "first 6 letters of the ID." So you would want to use the Left$() function.
Never could tell my left from my right...#2 In Command1_Click you call create_arrays... this is BAD!!!
Each time you were clicking the button you were eracing all of your booking info. So I was able to book a seat for Wallace over and over and over... because each time it checked the seat was open.
#3 The array Bookings is dimentioned 0 to 4... yes there are 5 positions but they are not 1 though 5 so you have to scale you seatnumbers to match. Bookings(seatnumber -1)="booked" etc. BTW as written when you choose seat 5 you get an error.
#4 You took out the loop in Check_Id... and this meant that only the id "WallaceW48P01" worked...
#5 You use a Lot of Global Variables. Although it is possible to write perfect code with lots of Globals programmers have found that it is best to keep them to a minimum. The worst one is the Counter... as it is possable in event driven programming for Counter to be reassigned durring a loop. It would be a hard error to catch as you could only see it when the right event was triggered.
Now that I have said all of that let me say that you are not doing bad. The code you have written is better than some of the code I have seen in college classes. One thing that will hep you is to TRY TO BREAK your program when you run it. Type in wrong input and see what happens... I found most of the above bugs from trying out differnet inputs into you code. Sometimes you need to see the error condition to know something is wrong.
Here is what I found ironed out:
CODE
Option Explicit
Dim ids(4) As String '(0 to 4)
Dim bookings(4) As String '(0 to 4 not 1 to 5)
Dim counter As Integer '*** Global Counter! Bad idea...
Dim passengerid As String
Dim finalID As String
Dim seatnumber As Integer
Dim flightnumber As String
Private Sub Command1_Click()
'**** BIG MISTAKE!!! Each time you click command1 the arrays
' reset and you loose all booking information!!!
' This belongs in Form_Load event handeler Or we need to
' see if the arrays are already initalized.
'Call create_arrays
If ids(0) <> "WallaceW48P01" Then create_arrays
Call check_id
Call book_seat
Call display_results
End Sub
Private Sub create_arrays()
ids(0) = "WallaceW48P01"
ids(1) = "LauderH468P02"
ids(2) = "CarnegieA468P3"
ids(3) = "ScottW468P04"
ids(4) = "DeBruisR468P05"
flightnumber = "HA468"
'*** You forgot about bookings(0) ***
bookings(0) = ""
bookings(1) = ""
bookings(2) = ""
bookings(3) = ""
bookings(4) = ""
End Sub
Private Sub check_id()
Dim found As Boolean
Do
passengerid = InputBox("Please enter your user ID")
'*** We have to check the entire array... not just element 0
For counter = 0 To 4
If ids(counter) = passengerid Then
found = True
MsgBox ("The ID you entered was accepted")
Exit For
End If
Next counter
If Not found Then MsgBox ("The ID you entered was invalid, please re=enter")
Loop Until found = True
End Sub
Private Sub book_seat()
Dim found As Boolean
Dim TheInput As String
Do
Do
TheInput = InputBox("Enter the seat number you want between 1 and 5")
seatnumber = Val(TheInput)
If seatnumber < 1 Or seatnumber > 5 Then
MsgBox "Invalid seat number!"
End If
Loop Until seatnumber >= 1 And seatnumber <= 5
'*** Note: the seats are numbered 1 though 5
'*** and the array is numbered 0 though 4
If bookings(seatnumber - 1) = "" Then
found = True
bookings(seatnumber - 1) = "Booked"
MsgBox ("Your seat has been booked")
Else
MsgBox ("Seat is not available, please enter another number")
End If
Loop Until found
End Sub
Private Sub display_results()
'finalID = Right$(passengerid, 6)
finalID=Left$(passengerid,6 )
Label1.Caption = flightnumber
Label2.Caption = passengerid
Label3.Caption = finalID
Label4.Caption = seatnumber
End Sub
This post has been edited by NickDMax: 18 Feb, 2007 - 02:54 PM