"Welcome" userform
Private Sub LookUpRef_Click()
If Me.EZBook_Ref.Value = "" Then
MsgBox "You must enter an EZBook Reference number to locate a booking"
Else
Range("SEARCH_REF:SEARCH_REF") = ""
Dim LR As Long
Dim FindString As String
LR = Sheets("BookingsList").Range("A" & Rows.Count).End(xlUp).Row
FindString = EZBook_Ref.Value
If Trim(FindString) <> "" Then
With Sheets("BookingsList").Range("A2:A" & LR)
Set Rng = .Find(What:=FindString, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Rng Is Nothing Then
Sheets("Data").Select
Application.Goto Rng.Offset(0, 0), True
Range("SEARCH_REF:SEARCH_REF") = Rng.Offset(0, 0).Value
Range("SEARCH_Name:SEARCH_Name") = Rng.Offset(0, 1).Value
Range("SEARCH_Type:SEARCH_Type") = Rng.Offset(0, 7).Value
Range("SEARCH_PartyDate:SEARCH_PartyDate") = Rng.Offset(0, 4).Text
Range("SEARCH_BookingTime:SEARCH_BookingTime") = Rng.Offset(0, 5).Text
Range("SEARCH_DateBooked:SEARCH_DateBooked") = Rng.Offset(0, 6).Text
Range("SEARCH_BookedBy:SEARCH_BookedBy") = Rng.Offset(0, 8).Text
Range("SEARCH_EmailAddress:SEARCH_EmailAddress") = Rng.Offset(0, 3).Text
Range("SEARCH_Telephone:SEARCH_Telephone") = Rng.Offset(0, 2).Text
Range("SEARCH_ChildAge:SEARCH_ChildAge") = Rng.Offset(0, 9).Text
Range("SEARCH_ChildName:SEARCH_ChildName") = Rng.Offset(0, 10).Text
Range("SEARCH_Attendees:SEARCH_Attendees") = Rng.Offset(0, 11).Text
Range("SEARCH_NumLanes:SEARCH_NumLanes") = Rng.Offset(0, 13).Text
Range("SEARCH_NumGames:SEARCH_NumGames") = Rng.Offset(0, 14).Text
Range("SEARCH_SubType:SEARCH_SubType") = Rng.Offset(0, 15).Text
Range("SEARCH_Notes:SEARCH_Notes") = Rng.Offset(0, 16).Text
Welcome.Hide
ViewBooking.Show
Else
MsgBox "No Results for that booking reference number '" & Me.EZBook_Ref.Value & "'"
End If
End With
End If
End If
End Sub
This searches "BookingsList" for the value of "EZBook_Ref". If there are no results it says "No Results for that booking reference number 'xxxx'", otherwise, it selects the booking in the "BookingsList" sheet. It then selects the information from each column and transfers the information to the relevent cell names (which refer to cells in the sheet "Data").
For the very first search, this works. It then shows the User form "ViewBooking".
Private Sub UserForm_Initialize()
ViewBooking.Caption = "View Booking - Reference Number " & Range("SEARCH_REF:SEARCH_REF") & " - " & Range("SEARCH_Name:SEARCH_Name")
Me.EZBook_RefNum.Text = Range("SEARCH_REF:SEARCH_REF").Text
Me.PartyType.Text = Range("SEARCH_Type:SEARCH_Type").Text
Me.PartyDate.Text = Range("SEARCH_PartyDate:SEARCH_PartyDate").Text
Me.PartyTime.Text = Range("SEARCH_BookingTime:SEARCH_BookingTime").Text
Me.NumAttendees.Text = Range("SEARCH_Attendees:SEARCH_Attendees").Text
Me.NumLanes.Text = Range("SEARCH_NumLanes:SEARCH_NumLanes").Text
Me.NumGames.Text = Range("SEARCH_NumGames:SEARCH_NumGames").Text
Me.CustomerName.Text = Range("SEARCH_Name:SEARCH_Name").Text
Me.PhoneNum.Text = Range("SEARCH_Telephone:SEARCH_Telephone").Text
Me.EMailAddr.Text = Range("SEARCH_EmailAddress:SEARCH_EmailAddress").Text
Me.BookedBy.Text = Range("SEARCH_BookedBy:SEARCH_BookedBy").Text
Me.DateBooked.Text = Range("SEARCH_DateBooked:SEARCH_DateBooked").Text
Me.BookingNotes.Text = "System Notes"
Me.User_Notes.Text = Range("SEARCH_Notes:SEARCH_Notes").Text
'define variables
Dim varSourceRow As Integer
Dim varTargetRow As Integer
Dim varRowCount As Integer
Dim varColumn As Integer
Dim varReferenceNumber As String
Dim WS As Object
Dim WStemp As Object
Dim FirstCell As Range
Dim LastCell As Range
Dim varListViewRange As Range
Set WS = Sheets("Confirmation")
Set WStemp = Sheets("ConfSearch")
'cleardown previous search
WStemp.Range("A2:D5000").Value = ""
'count the total number of records
varRowCount = Sheets("Confirmation").Cells(50000, 1).End(xlUp).Row
'define search string
varReferenceNumber = Range("SEARCH_REF:SEARCH_REF")
'reset loop counters
varTargetRow = 2
For varSourceRow = 2 To varRowCount
If WS.Cells(varSourceRow, 1).Value = varReferenceNumber Then
For varColumn = 1 To 4
WStemp.Cells(varTargetRow, varColumn).Value = WS.Cells(varSourceRow, varColumn).Value
Next varColumn
varTargetRow = varTargetRow + 1
End If
Next varSourceRow
'Cancel if there are no results found
If WStemp.Cells(2, 1).Value = "" Then
Exit Sub
End If
'Count the number of results
varLastRow = Sheets("ConfSearch").Cells(50000, 1).End(xlUp).Row
'Define the range for the listbox
With WStemp
.Columns("A:D").EntireColumn.AutoFit
Set FirstCell = .Cells(2, 1)
Set LastCell = .Cells(varLastRow, 4)
Set varListViewRange = .Range(FirstCell, LastCell)
End With
'Link the listbox to the range and adjust column widths
With Me.ConfirmationHistory
.RowSource = varListViewRange.Address(external:=True)
ColWidth = ""
For c = 1 To 4
ColWidth = ColWidth & varListViewRange.Columns(c).Width & ";"
Next c
.ColumnWidths = ColWidth
.ListIndex = 0
End With
End Sub
ViewBooking works perfectly. If i click on the "Close Booking" button, it runs the following macro.
Private Sub CloseBooking_Click()
'cleardown previous search
Sheets("ConfSearch").Range("A2:D5000").Value = ""
Sheets("BookingsList").Select
Range("A1:A1").Select
Sheets("Data").Select
Range("SEARCH_REF:SEARCH_REF") = ""
Range("SEARCH_Type:SEARCH_Type") = ""
Range("SEARCH_PartyDate:SEARCH_PartyDate") = ""
Range("SEARCH_BookingTime:SEARCH_BookingTime") = ""
Range("SEARCH_Attendees:SEARCH_Attendees") = ""
Range("SEARCH_NumLanes:SEARCH_NumLanes") = ""
Range("SEARCH_NumGames:SEARCH_NumGames") = ""
Range("SEARCH_Name:SEARCH_Name") = ""
Range("SEARCH_Telephone:SEARCH_Telephone") = ""
Range("SEARCH_EmailAddress:SEARCH_EmailAddress") = ""
Range("SEARCH_BookedBy:SEARCH_BookedBy") = ""
Range("SEARCH_DateBooked:SEARCH_DateBooked") = ""
Range("SEARCH_Notes:SEARCH_Notes") = ""
Range("SEARCH_ChildAge:SEARCH_ChildAge") = ""
Range("SEARCH_ChildName:SEARCH_ChildName") = ""
Range("SEARCH_SubType:SEARCH_SubType") = ""
Me.EZBook_RefNum.Text = ""
Me.PartyType.Text = ""
Me.PartyDate.Text = ""
Me.PartyTime.Text = ""
Me.NumAttendees.Text = ""
Me.NumLanes.Text = ""
Me.NumGames.Text = ""
Me.CustomerName.Text = ""
Me.PhoneNum.Text = ""
Me.EMailAddr.Text = ""
Me.BookedBy.Text = ""
Me.DateBooked.Text = ""
Me.BookingNotes.Text = ""
Me.User_Notes.Text = ""
Sheets("BookingsList").Select
ViewBooking.Hide
Welcome.Show
End Sub
So you end up back on the Welcome userform, back to square one.
If you then enter a new reference number that isn't in the system it will show the error message that no booking was found.
If you then enter a different reference number that is in the booking system, it does not transfer the information from each column into the cells in "Data" like it does on the first search and I am not sure why.
Can anyone help pleeeease? Thanks :-)
Bookmarks