+ Reply to Thread
Results 1 to 11 of 11

Userform runs Initialize sub @ beginning of textbox AfterUpdate event w/out being called

Hybrid View

  1. #1
    Registered User
    Join Date
    06-22-2016
    Location
    Minnesota
    MS-Off Ver
    2013
    Posts
    20

    Userform runs Initialize sub @ beginning of textbox AfterUpdate event w/out being called

    I have a textbox where I have setup some code in the AfterUpdate event that is intended to take the value entered into the box and "do stuff". I have struggled to get it to do stuff, and finally realized that one of the problems is because my me.textbox.value is always coming up empty even when there is text entered into the textbox on the form and I've pressed enter. I know very little about the step-through functions in VBA and finally figured out how to F8 my way through my textbox AfterUpdate sub. I discovered that the very first thing it does, before even making it to any of my "do stuff" code, is to run the userform initialize sub. In the initialize sub I had previously written code to explicity set the value of the textbox to "" so that it would be clear upon opening. What is tripping me up is that my textbox afterupdate sub does not call the Intialize sub anywhere in its code. Why is it making that jump?

    Below is the beginning of the AfterUpdate code, which is a hot mess right now because of all the different coding things I'd tried prior to discovering the null textbox value issue.

    Private Sub fPanelNumi_AfterUpdate()
     Dim testcell
     Dim i As Integer 'row position of a panel number
     Dim j As Integer 'the quantity of discrete values with a panel number string
     Dim k As Integer 'loop counter for the string values
     Dim m As Integer 'low value of a range in the panel number string
     Dim n As Integer 'high value of a range in the panel number string
     Dim forstopper As Boolean
     Dim nArray As Variant
     Dim nPanelArray As Range
     Dim RowCounter As Integer
     
     
     'fix this so that entering a part number doesn't pull in random data in instances such as when creating anew panel.
    If OBtnNewPanel.Value = True Then
        Else
            OBtnLoadPanelI.Value = True Or OBtnLoadPanelN = True
    End If
        
     'Make DataEntry active
     Worksheets("DataEntry").Activate
    
     RowCounter = WorksheetFunction.CountA(Range("D5:D200"))
    
    Set nPanelArray = Range("D5:D200")
     
    i = 1
    
    forstopper = True
        Do While i <= RowCounter And forstopper
            
            'MsgBox InStr("106-108", "-")
            Me.fPanelNumi.Value = Me.fPanelNumi.Text
            MsgBox Me.fPanelNumi.Value
            nArray = Split(nPanelArray(i, 1).Value, ",", , vbTextCompare)
            j = WorksheetFunction.CountA(nArray)
            k = 0
            Do While k <= j - 1 And forstopper 'This searches for a number within a string of comma delineated numbers, ex 106,107,108,109
                If InStr(nArray(k), "-") = 0 Then
                    If nArray(k) = fPanelNumi Then
    Thanks.
    Last edited by chirp08; 08-18-2016 at 12:13 PM. Reason: Trying to clean up explanations

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Userform runs Initialize sub @ beginning of textbox AfterUpdate event w/out being call

    When you step through the code what line is triggering the Initialize event?
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    06-22-2016
    Location
    Minnesota
    MS-Off Ver
    2013
    Posts
    20

    Re: Userform runs Initialize sub @ beginning of textbox AfterUpdate event w/out being call

    Quote Originally Posted by Norie View Post
    When you step through the code what line is triggering the Initialize event?
    I placed the cursor at right before "Private Sub fPanelNumi_AfterUpdate()" and hit F8 and it immediately jumped over to the Initialize sub.

  4. #4
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,290

    Re: Userform runs Initialize sub @ beginning of textbox AfterUpdate event w/out being call

    First of all you don't need to activate DataEntry sheet to work with it's data.
    Secondly without an example file with some data and your userform with all it's coding it's very hard to say where your code jumps and why.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  5. #5
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Userform runs Initialize sub @ beginning of textbox AfterUpdate event w/out being call

    Hi,

    What is the following line supposed to achieve

    OBtnLoadPanelI.Value = True Or OBtnLoadPanelN = True
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  6. #6
    Registered User
    Join Date
    06-22-2016
    Location
    Minnesota
    MS-Off Ver
    2013
    Posts
    20

    Re: Userform runs Initialize sub @ beginning of textbox AfterUpdate event w/out being call

    Quote Originally Posted by xlnitwit View Post
    Hi,

    What is the following line supposed to achieve

    OBtnLoadPanelI.Value = True Or OBtnLoadPanelN = True
    Nothing yet :-) As I said, that code is quite a mess right now, but stepping through with F8 shows me that it never even reaches the code in question. It instead hops to the Initialize sub which clears out the text I'd just entered into the textbox. After Initialize runs, my form pops up and I am then able to re-enter the text that I'd entered to begin with. The After_update sub then works correctly. So right now, here is what I think is happening:

    1. enter text into textbox and hit enter
    2. textbox starts the AfterUpdate event, and for some unknown reason calls the userform Initialize sub
    3. the Initialize sub performs as intended and clears all data (including the text I just entered in step 1) from the form
    4. I can then re-enter the text into the textbox and hit enter and the AfterUpdate sub performs correctly.

    I'd like to include a better example file, but there is some proprietary stuff in the file and it is all linked together enough that it'll be difficult to pull out just the problematic area.

  7. #7
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Userform runs Initialize sub @ beginning of textbox AfterUpdate event w/out being call

    It has to load the form to run that sub

  8. #8
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Userform runs Initialize sub @ beginning of textbox AfterUpdate event w/out being call

    Was the form already loaded when you pressed f8? If it was not, the Initialize code should run because the form will be loaded automatically.

  9. #9
    Registered User
    Join Date
    06-22-2016
    Location
    Minnesota
    MS-Off Ver
    2013
    Posts
    20

    Re: Userform runs Initialize sub @ beginning of textbox AfterUpdate event w/out being call

    Quote Originally Posted by xlnitwit View Post
    Was the form already loaded when you pressed f8? If it was not, the Initialize code should run because the form will be loaded automatically.
    I should have thought of that. No, it wasn't already loaded. Please disregard my previous reply to you, I was typing while you were replying. So I entered the Breakpoint with F9 at the beginning of the sub as suggested by Norie. Stepping through after that, I discovered that the
     OBtnLoadPanelI.Value = True Or OBtnLoadPanelN = True
    you'd asked about earlier is the culprit. I had intended for that to be another conditional if statement, not a result of the else. There is code linked to those button clicks that initializes the form. It appears to be working now that I fixed that. Thanks all for the responses on what turns out to be a dumb mistake. Thanks also for the tips on the debugging tools, those will be very helpful.

  10. #10
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Userform runs Initialize sub @ beginning of textbox AfterUpdate event w/out being call

    Sounds like you are manually opening the userform yourself.

    I just added a userform and put the cursor in the blank code window, when I pressed F8 the form opened.

    If you want to debug the AfterUpdate event put a breakpoint on the sub heading with F9, open the form and do something with the control in question.

    Code should then be interrupted at the point you but the break and you can F8 from there.

  11. #11
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Userform runs Initialize sub @ beginning of textbox AfterUpdate event w/out being call

    Do you have any other event code programmed for that textbox?

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] How to deal with multiple sets of code in the Initialize Event of a userform
    By chin67326 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-18-2015, 06:54 AM
  2. AfterUpdate Event triggered without updating the textbox
    By mschatteles in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-06-2015, 02:24 PM
  3. [SOLVED] Userform called from another userform textbox - unload problem
    By barryleajo in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 03-27-2013, 01:09 PM
  4. Unload UserForm Runs Initialize
    By McNultyK in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-20-2011, 11:51 AM
  5. Initialize Textbox on Userform
    By Jonathan78 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-18-2011, 01:55 PM
  6. AfterUpdate event for dynamic TextBox
    By moyo in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-20-2007, 02:23 AM
  7. [SOLVED] UserForm Initialize event doesn't fire reliably
    By RB Smissaert in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 02-15-2005, 09:41 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1