Hi all I'm really lost on this one and just can not workout where I'm going wrong as in my mind what I've tried should work logically but that is not the case although it most likely my inexperience with coding thats letting me down again. So after hours of trying to sort it out myself I've finally decided to ask for some help and advice please.
So I have a userform with a textbox call 'textboxEventCode'. This textbox is disabled when the userform is opened as its content is automatically generated once a user completes two other textboxes: 'textboxEventName & textboxEventStartDate' with the following code. All of this works ok.
with the 'textboxEventStartDate' I have the following events that also work fine.
Lastly, I have some code that stops a user entering a duplicate event.
So my main question is where can I put this event so that is triggers as soon as the 'textboxEventCode' is populated?
logically I thought it should work in the exit event, which it did to a degree by showing the msgbox to say the event already exists but when I clicked ok on that it also showed the other msgbox in the event.
Hopefully that makes sense and hopefully someone will be kind enough to tell me what I'm doing wrong and how to sort it.
Many thanks Paul
So I have a userform with a textbox call 'textboxEventCode'. This textbox is disabled when the userform is opened as its content is automatically generated once a user completes two other textboxes: 'textboxEventName & textboxEventStartDate' with the following code. All of this works ok.
VBA Code:
Private Sub textboxEventStartDate_Change()
Dim textboxEventName_Exit As String
Dim textboxEventStartDate As Date
textboxEventCode.Value = Trim(Me.textboxEventStartDate.Value) & " - " & textboxEventName.Value
textboxEventName.Value = Application.WorksheetFunction.Proper(textboxEventName.Value)
End Sub
with the 'textboxEventStartDate' I have the following events that also work fine.
VBA Code:
Private Sub textBoxEventStartDate_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
If Not IsDate(textboxEventStartDate) Then
MsgBox "It looks like you've either entered a text value or a blank space, Please only enter a date in the correct format e.g. 'DD/MM/YYYY'", vbExclamation, "Invalid Entry"
Cancel = True
textboxEventStartDate.SetFocus
textboxEventStartDate = ""
textboxEventStartDate.BackColor = RGB(204, 255, 255)
Else
textboxEventStartDate = Format(CDate(textboxEventStartDate), "DD/MM/YYYY")
End If
End Sub
Private Sub TextBoxEventStartDate_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If textboxEventStartDate.Text = "" Then
Cancel = 1
MsgBox "You must enter the events start date.", vbExclamation, "Date Required"
textboxEventStartDate.SetFocus
textboxEventStartDate.BackColor = RGB(204, 255, 255)
Else
textboxEventStartDate.BackColor = RGB(255, 255, 255)
End If
End Sub
Lastly, I have some code that stops a user entering a duplicate event.
VBA Code:
Dim a As Long
a = Application.WorksheetFunction.CountIf(shTestData.Range("B:B"), _
Me.textboxEventCode.Text)
If a >= 1 Then
Me.textboxEventCode = ""
MsgBox "You already have a record of an event with the same name starting on the same date. Please select it from the Main Events List.", vbInformation, "Duplicate Entry Detected"
End If
So my main question is where can I put this event so that is triggers as soon as the 'textboxEventCode' is populated?
logically I thought it should work in the exit event, which it did to a degree by showing the msgbox to say the event already exists but when I clicked ok on that it also showed the other msgbox in the event.
Hopefully that makes sense and hopefully someone will be kind enough to tell me what I'm doing wrong and how to sort it.
Many thanks Paul