textBox AfterUpdate firing unexpectedly

mikeymay

Well-known Member
Joined
Jan 17, 2006
Messages
1,632
Office Version
  1. 365
Platform
  1. Windows
I have a UF that has a TextBox and some CommmandButtons.

The form is used to allow the amending of an entry is a list.

The process is
Form is loaded​
The entry to be amended is established​
The TextBox is populated with the entry to be amended​
The UF is shown​

This was working as expected but after some tweaks (which I unfortunately do not know what I have tweaked), as the UF is shown, the TextBox AfterUpdate event code executes.

The code to load and populate the form is below
VBA Code:
'--------The user right-clicks a cell and there is an option to Amend Driver which fires this code
Sub AmendDriver()

boolCancel = False

Set rngHeader = Range("Drivers_Header")

strDriver = ActiveCell

Load frmDriver

boolAmend = True

Set rngListCheck = Range("D_DriversList")

With frmDriver
   .Caption = "AMEND DRIVER"
  
   .txtDriver = strDriver
  
   .btnAdd.Caption = "AMEND"
  
   boolAmend = False
   
   .Show
   '------When I step through the code, the UF is displayed with no issue
   '------When I let the code execute whithout stepping through, the .txtDriver_AfterUpdate() code executes (see below) straight after .Show
End With

frmDriver.Hide

Unload frmDriver

If boolCancel = True Then
   Exit Sub
   Else
End If

Set rngAdd = Range("Drivers_Start")

Do Until rngAdd = strDriver
   Set rngAdd = rngAdd.Offset(1, 0)
Loop

rngAdd = strDriverAmend

Set rngAdd = Nothing

ActiveSheet.Unprotect Password:=strPassword

MsgBox ("The Driver details have been amended"), vbInformation, "AMENDED"

End Sub

TextBox code
VBA Code:
Private Sub txtDriver_AfterUpdate()

boolTrim = True

txtDriver = Trim(txtDriver)

boolTrim = False

Set rngListCheck = Range("D_DriversList")

If boolAmend = True Then
   strDriverAmend = Application.Proper(txtDriver)
  
   If strDriverAmend = "" Then
      Else
      If WorksheetFunction.CountIf(rngListCheck, strDriverAmend) > 0 Then
         MsgBox ("This DRIVER already exists and cannot be added"), vbExclamation, "ERROR"
        
         boolDuplicate = True
         Else
         boolDuplicate = False
      End If
   End If
   Else
   strDriver = Application.Proper(txtDriver)
  
   If strDriver = "" Then
      Else
      If WorksheetFunction.CountIf(rngListCheck, strDriver) > 0 Then
         MsgBox ("This DRIVER already exists and cannot be added"), vbExclamation, "ERROR"
        
         boolDuplicate = True
         Else
         boolDuplicate = False
      End If
   End If
End If

CheckAdd

End Sub

Whilst I know of a way to 'fix' this in a way that exits the AfterUpdate evenet code at the point of the UF being first shown, I know that this shouldn't be happening and I'd rather resolve the cause rather than implementing something to ignore it.

I can share the file as it is still an early dev so nothing confidential is included.


TIA
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
You are populating the control in your code so I would expect the AfterUpdate event to be triggered.
 
Upvote 0
I thought that as well but two things...
1 - I understood that the _Change event fires only when it's being populated and not the _AfterUpdate event
2 - Why does the _AfterUpdate event fire when the code is executing normally but when I step through the code to load the form, populate the controls, etc, the _AfterUpdate event doesn't trigger
 
Upvote 0
Stepping through would stop at the .Show command

If you update a control, you should expect its Update events to fire. ;)
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,250
Members
452,623
Latest member
Techenthusiast

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