Exiting a Textbox Problem

Pacman52

Active Member
Joined
Jan 29, 2009
Messages
319
Office Version
  1. 365
Platform
  1. Windows
Hi all, So after hours of trying to workout myself why I can't get this to work I've finally given up trying and would be really grateful if someone could tell me what I'm doing wrong please.

So in the code below I'm trying to exit the textbox 'textboxInvoiceDate' and show the msgbox asking has the invoice been paid. If the response is Yes then I need to be able to set the focus to the 'textboxDateInvoicePaid' if the response is no I want to disable the 'textboxDateInvoicePaid' and textboxPaymentMethod and move the focus to the next relevant box (textboxInvoiceDetails).

The problem I'm getting is the msgbox asking if the invoice has been paid appears as expected but the focus stays in the textbox and the msgbox appears again I then tab out of the textbox again and the msgbox again shows - its like the textbox isn't exiting initially and re-setting the focus back to itself.

Could anyone advise what I'm missing here or doing wrong.

Many thanks Paul


VBA Code:
Private Sub textboxInvoiceDate_Enter()

    textboxInvoiceDate.BackColor = RGB(204, 255, 255)

End Sub

Private Sub textboxInvoiceDate_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)

    If Not IsDate(textboxInvoiceDate) Then
        MsgBox "Your entry is not recognised as a date. Please only enter a date in the correct format e.g. 'DD/MM/YYYY'", vbExclamation, "Invalid Entry"
        Cancel = True
        textboxInvoiceDate = ""
        textboxInvoiceDate.BackColor = RGB(204, 255, 255)
        textboxInvoiceDate.SetFocus

    Else

        textboxInvoiceDate = Format(CDate(textboxInvoiceDate), "DD/MM/YYYY")

    End If

End Sub

Private Sub textboxInvoiceDate_Exit(ByVal Cancel As MSForms.ReturnBoolean)

    Dim MsgReply1 As VbMsgBoxResult
    
    If textboxInvoiceDate.Value = "" Then

        Cancel = True
        MsgBox "You must enter the date of the invoice.", vbExclamation, "Date Required"
        textboxInvoiceDate.SetFocus
        textboxInvoiceDate.BackColor = RGB(204, 255, 255)
        
    Else
        
    If textboxInvoiceDate.Value >= "" Then
    
        MsgReply1 = MsgBox("Has this invoicealready been paid?", vbQuestion + vbYesNo, "Payment Method")
        
        If MsgReply1 = vbYes Then
    
        textboxDateInvoicePaid.Enabled = True
        textboxDateInvoicePaid.SetFocus
        textboxPaymentMethod.Enabled = True
        textboxInvoiceDate = Format(CDate(textboxInvoiceDate), "DD/MM/YYYY")
        textboxInvoiceDate.BackColor = RGB(255, 255, 255)

Else 
        textboxDateInvoicePaid.Enabled = False
        textboxPaymentMethod.Enabled = False
        textboxInvoiceDetails.SetFocus
     
    End If
    End If
        End If

End Sub
 
Did you try my suggestion in post #5?
Hi I'm so sorry I thought I'd responded to your reply - I had tried the AfterUpdate as well but that gave the same result but thankfully have sorted it now, the issue appeared to be a corrupt textbox but I do apricate your reply all the same. Paul
 
Upvote 0

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!

Forum statistics

Threads
1,215,212
Messages
6,123,654
Members
449,113
Latest member
Hochanz

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top