Take a look at the attached. This is a great example of using more than one userform in conjunction with each other. We're using the first form (fAppointment - note singular name) to load an appointment item from fAppointments (note the plural name).
I also took the time to add two properties to fAppointment. This has several advantages. First and foremost, you can load these properties up when calling the form, either by launch or by another form. You can set defaults if you don't want to use the system's (i.e. a Long variable will default with a 0 value, a String will be vbNullString or "", a Boolean will be False, etc.).
While this is starting to get into intermediate VBA, it still serves as a great example for how to use them. If you want to be good with VBA, these are excellent fundamentals to learn early on. Don't worry too much about how to structure custom properties or how to code them, there are tools to help you write them so you don't have to have the exact nomenclature memorized. The thing you need to know is 1) they all have a default based on the [variable] type, 2) you can have a read-only property, 3) you can have a read-write property, 4) you can perform any action within those properties, i.e. "do this when someone gets this property value".
File: Calendar Rev 3.1.2.xlsm
For transparency and ease of consumption, I'll post the code below.
fAppointment properties:
' Variables to hold [value] for writing property
Private pNewAppointment As Boolean
Private pAppointmentIndex As Long
Public Property Get NewAppointment() As Boolean
' Read property
NewAppointment = pNewAppointment
End Property
Public Property Let NewAppointment( _
ByVal Value As Boolean _
)
' Write property
pNewAppointment = Value
End Property
Public Property Get AppointmentIndex() As Long
' Read property
AppointmentIndex = pAppointmentIndex
End Property
Public Property Let AppointmentIndex( _
ByVal Value As Long _
)
' Write property
pAppointmentIndex = Value
End Property
fAppointment button commands:
Private Sub CancelButton_Click()
Unload Me
End Sub
Private Sub OkButton_Click()
Dim Table As ListObject
Dim DateTime As Date
Dim DatePart As Date
Dim TimePart As Date
Dim Index As Long
On Error Resume Next
DateTime = CDate(Me.tDateTime.Value)
DatePart = Int(DateTime)
TimePart = DateTime - DatePart
On Error GoTo 0
If DateTime = 0 Then
MsgBox "Please enter a date and time.", vbExclamation + vbOKOnly, "Date/Time"
Exit Sub
End If
If Me.tAppointment.Value = vbNullString Then
MsgBox "Please enter an appointment text.", vbExclamation + vbOKOnly, "Appointment"
Exit Sub
End If
Set Table = ThisWorkbook.Worksheets("Calendar").ListObjects("Table2")
If Me.NewAppointment Then
If Table.DataBodyRange Is Nothing Then
Table.ListRows.Add
Me.AppointmentIndex = 1
Else
For Index = 1 To Table.ListRows.Count
If WorksheetFunction.CountA(Table.DataBodyRange(Index, 1).Resize(1, 3)) = 0 Then
Me.AppointmentIndex = Index
Exit For
End If
Next Index
End If
If Index > Table.ListRows.Count Then
Table.ListRows.Add
End If
Else
Index = Me.AppointmentIndex
End If
If Me.AppointmentIndex > 0 Then
Table.DataBodyRange(Me.AppointmentIndex, 1).Value = Format(TimePart, "h:mm AM/PM")
Table.DataBodyRange(Me.AppointmentIndex, 2).Value = Me.tAppointment.Value
Table.DataBodyRange(Me.AppointmentIndex, 3).Value = Format(DatePart, "mm/dd/yyyy")
Unload Me
Else
MsgBox "Something went wrong.", vbExclamation + vbOKOnly, "Whoops!"
End If
End Sub
fAppointments buttom commands:
Private Sub lAppointments_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Dim Appointment As New fAppointment
Dim DateTime As Date
Dim Index As Long
Dim SelectedIndex As Long
For Index = 0 To Me.lAppointments.ListCount - 1
If Me.lAppointments.Selected(Index) Then
SelectedIndex = Index
Exit For
End If
Next Index
If SelectedIndex = 0 Then Exit Sub
On Error Resume Next
DateTime = Me.lAppointments.List(SelectedIndex, 2) + Me.lAppointments.List(SelectedIndex, 0)
On Error GoTo 0
Load Appointment
Appointment.tDateTime.Value = Format(DateTime, "m/d/yyyy h:mm AM/PM")
Appointment.tAppointment.Value = Me.lAppointments.List(SelectedIndex, 1)
Appointment.NewAppointment = False
Appointment.AppointmentIndex = SelectedIndex + 1
Appointment.Show
Me.Repaint
End Sub
Private Sub CloseButton_Click()
Unload Me
End Sub
Buttom calls/form loads:
Sub ShowAppointments()
Dim Appointments As New fAppointments
Load Appointments
Appointments.Show
End Sub
Sub ShowAddAppointment()
Dim Appointment As New fAppointment
Load Appointment
Appointment.NewAppointment = True
Appointment.Show
End Sub