Check for Duplicate Appointment Times

  • Thread starter Thread starter geo039
  • Start date Start date
G

geo039

I have a simple application that takes user input by text and time
selected by date time picker. It displays the appt description in one
list box and the time in another list box. I need a simple function
that checks the times for duplicates. I want to check for a duplicate
before it writes to the listbox by returning a boolean (true/false)
value. I only need to check hours and minutes not date or seconds. I'm
new to this so any help would be appreciated.

Thanks
 
Hello,

I am going to assume a Windows form.

You basically have to loop through the listbox. Examine each entry by setting it's selected value to true. Read the value of the selected row and then validate it.

Sample:
Dim i As Integer
Dim bolAddNew as Boolean

For i = 0 To Me.ListBox1.Items.Count
Me.ListBox1.SelectedIndex = i
If Me.ListBox1.SelectedValue = txtTime.Text Then
bolAddNew = True
Exit For
Else
BolAddNew = False
End If
Next

If boilAddNew Then
'Add New Time
Else
'Optional: Display MSGBOX
End If
 
Right now I have this but it's not finding duplicates, I think there's
a problem with the function? Any Suggestions?

Dim Time As Boolean

Time = TimeTaken(Me.dtmTime.Value.ToShortDateString)

If Time Then
MessageBox.Show("You already have an appointment at
this time")
Else

'display appointment in Listbox
lstApptResults.Items.Add(txtAppointment.Text)
txtAppointment.Clear() 'clear appointment from TextBox
txtAppointment.Focus() 'transfer focus to TextBox

'display appointment time in Listbox

lstTimeResults.Items.Add(Me.dtmTime.Value.ToShortTimeString)

End If
End If
End Sub 'btnAddAppt_Click
'function to check if an appointment time already exists in listbox
Public Function TimeTaken(ByVal ApptTime As DateTime) As Boolean

Dim DuplicateTime As Boolean = False

'loop that checks listed times for duplicates
For Each strItem As String In lstTimeResults.Items
If strItem = ApptTime Then
DuplicateTime = True

Exit For
End If
Next

Return DuplicateTime
End Function 'TimeTaken
 
If you step through the program, what value are you passing to TimeTaken?
It looks as though you are passing a date and not time.
 
Back
Top