Results 1 to 13 of 13
  1. #1
    Join Date
    May 2007
    Posts
    74

    Unanswered: Is there a way to determine when a text box changes?

    I am trying to write code to detect when someone has started to fill an empty text box. I want to disable/enable buttons based on whether the text box is empty or not. I want the detection to occur while the operator is typing in the text box.

    I thought the "On Change" event would do the trick, but it doesn't seem to work. While the "On Change" event is detected every time a key is pressed in the "txtField" text box, the Me.txtField value doesn't change until the text box loses focus. So, if Me.txtfield was empty when entered, it still shows up as empty during every "On Change" event.

    I would have thought that Access 2003 would support this, but I haven't been able to get it to work yet.
    AzJazz

    "I'm afraid the holodeck will be society's last invention." - Scott Adams

  2. #2
    Join Date
    Dec 2004
    Location
    Coon Rapids, Minnesota
    Posts
    41
    I would probably use the event On Key Up and just have it check the value of the textbox to see if it is blank or not, just in case someone presses a key that doesnt actually produce a character

  3. #3
    Join Date
    May 2007
    Posts
    74
    Quote Originally Posted by blade1981m
    I would probably use the event On Key Up and just have it check the value of the textbox to see if it is blank or not, just in case someone presses a key that doesnt actually produce a character
    blade1981m - I tried the "On Key Up" event, and saw the same result: The Me.txtField value didn't change until the text box lost focus.
    AzJazz

    "I'm afraid the holodeck will be society's last invention." - Scott Adams

  4. #4
    Join Date
    May 2004
    Location
    New York State
    Posts
    1,178
    Take a look at the IsDirty property in the help file, and see if it's appropriate for what you need.

    Sam

  5. #5
    Join Date
    Jun 2005
    Location
    Richmond, Virginia USA
    Posts
    2,763
    Provided Answers: 19
    Exactly what are you trying to do? Yes, the OnChange event is triggered as soon as a character is entered into the textbox, and, yes, the Value of a textbox that is empty when entered remains empty until the textbox is exited. The Text property, however, is updated with each keystroke in the textbox. Perhaps this is what you need.

    This code, for instance, will pop up a box after each keystroke, showing you the current content of the box

    Code:
    Private Sub YourTextBox_Change()
      MsgBox YourTextBox.Text
    End Sub
    Good Luck!
    Hope this helps!

    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  6. #6
    Join Date
    May 2007
    Posts
    74
    Quote Originally Posted by Missinglinq
    Exactly what are you trying to do? Yes, the OnChange event is triggered as soon as a character is entered into the textbox, and, yes, the Value of a textbox that is empty when entered remains empty until the textbox is exited. The Text property, however, is updated with each keystroke in the textbox. Perhaps this is what you need.

    This code, for instance, will pop up a box after each keystroke, showing you the current content of the box

    Code:
    Private Sub YourTextBox_Change()
      MsgBox YourTextBox.Text
    End Sub
    Good Luck!
    Thanks, Missinglinq - The .Text value worked, but raised a new problem. Now that I can detect when the text changes without leaving the field, I get an error while trying to ".Locked" an unrelated text field.

    The error is: "Run-time error '2166': You can't lock a control while it has unsaved changes."

    I don't think that anything is cached for this unrelated text field, or that it was even modified at all. I commented out all the code that may have modified the field, but I still get the 2166 error.

    ???
    Last edited by azjazz; 06-26-07 at 16:39.
    AzJazz

    "I'm afraid the holodeck will be society's last invention." - Scott Adams

  7. #7
    Join Date
    Jun 2005
    Location
    Richmond, Virginia USA
    Posts
    2,763
    Provided Answers: 19
    Just to be safe, in the line before locking any controls, put:

    Me.Dirty = False
    Hope this helps!

    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  8. #8
    Join Date
    May 2007
    Posts
    74
    Quote Originally Posted by Missinglinq
    Just to be safe, in the line before locking any controls, put:

    Me.Dirty = False
    Missinglinq - I tried your suggestion, and got the following message:

    Run-time error '2455': You entered an expression that has an invalid reference to the property Dirty.

    Just like my mom, I guess my computer doesn't want to see "Me.Dirty".
    AzJazz

    "I'm afraid the holodeck will be society's last invention." - Scott Adams

  9. #9
    Join Date
    Jun 2005
    Location
    Richmond, Virginia USA
    Posts
    2,763
    Provided Answers: 19
    Let's see the code for the entire sub where you placed the code. Inside of any sub in the form's code window, there should be no problem with it! It's standard code used thousands of times a day.

    You could also use the line

    docmd.RunCommand acCmdSaveRecord

    but I'm curious as to why Me.Dirty = False bombed out!It really shouldn't have!
    Hope this helps!

    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  10. #10
    Join Date
    May 2007
    Posts
    74
    You asked for it ...

    Code:
    Private Sub setFieldState()
    
        If IsNull(Me.cbxDocument) Then
        
            Me.txtFilePath = Null
            locTxtFilePath = ""
            Me.txtFilePath.Enabled = False
            Me.btnFindPath.Enabled = False
            Me.txtRev = Null
            locTxtRev = ""
            Me.txtRev.Enabled = False
            
            Me.btnExtract.Enabled = False
                
            Me.txtParaCounter = Null
            Me.txtParaCounter.Locked = False
            Me.txtParaCounter.Enabled = False
            
            Me.chkBtContainsNumber.Enabled = False
            Me.chkCaseSensitive.Enabled = False
            Me.chkCrEnd.Enabled = False
            Me.chkCrStart.Enabled = False
            Me.chkLfEnd.Enabled = False
            Me.chkLfStart.Enabled = False
            Me.chkPeriodEnd.Enabled = False
            Me.chkPeriodStart.Enabled = False
            
            Me.optSelect.Enabled = False
            
            clearLstParagraph
        
        Else
            
            If locTxtFilePath = "" Or locTxtRev = "" Then
    
                Me.txtFilePath.Enabled = True
                Me.btnFindPath.Enabled = True
                Me.txtRev.Enabled = True
                
                Me.txtParaCounter = Null
                Me.txtParaCounter.Locked = False
                Me.txtParaCounter.Enabled = False
                
                Me.btnExtract.Enabled = False
                
                Me.chkBtContainsNumber.Enabled = False
                Me.chkCaseSensitive.Enabled = False
                Me.chkCrEnd.Enabled = False
                Me.chkCrStart.Enabled = False
                Me.chkLfEnd.Enabled = False
                Me.chkLfStart.Enabled = False
                Me.chkPeriodEnd.Enabled = False
                Me.chkPeriodStart.Enabled = False
                
                Me.optSelect.Enabled = False
            
                clearLstParagraph
                
            Else
    
    '            Me.Dirty = False   <<== This causes a '2455' RT error.
    '            Me.txtParaCounter.Locked = True  <<== This causes a '2166' RT error.
                Me.txtParaCounter.Enabled = True
                
                Me.btnExtract.Enabled = True
                
                Me.chkBtContainsNumber.Enabled = True
                Me.chkCaseSensitive.Enabled = True
                Me.chkCrEnd.Enabled = True
                Me.chkCrStart.Enabled = True
                Me.chkLfEnd.Enabled = True
                Me.chkLfStart.Enabled = True
                Me.chkPeriodEnd.Enabled = True
                Me.chkPeriodStart.Enabled = True
                
            End If
            
        End If
    
        setParaFieldState
        
    End Sub
    For reference, "locTxtFilePath" and "locTxtRev" are variables (local to the Form) that contain the .Text values of the text boxes as they are filled up. All the text boxes are unbounded on the form.
    Last edited by azjazz; 06-27-07 at 15:16.
    AzJazz

    "I'm afraid the holodeck will be society's last invention." - Scott Adams

  11. #11
    Join Date
    Jun 2005
    Location
    Richmond, Virginia USA
    Posts
    2,763
    Provided Answers: 19
    All the text boxes are unbounded on the form.
    So the form is unbound then? That's why Me.Dirty = False bombed; it is only valid in a bound form! But this begs up the question of why you got the error message about trying to Lock a control that had data that hadn't been saved! If the textbox is unbound, this shouldn't make any difference! I was sure of this, but just ran up an unbound form to be sure. An unbound textbox can be locked whether empty or not.
    Hope this helps!

    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  12. #12
    Join Date
    May 2007
    Posts
    74
    I know - This is really confusing me. It sure looks like a bug in Access, or the description for RTE #2166 is misleading. I removed anything that could modify the data in the text box I am attempt to lock, and still saw the error.

    I wonder if the error is really complaining about the text box I am typing in instead of the one I am trying to lock? Though, that doesn't make much sense either.

    The thing that really surprises me is that I have found very few spots on the web where folks are complaining about the same thing. Nobody else had a solution to this, either.
    AzJazz

    "I'm afraid the holodeck will be society's last invention." - Scott Adams

  13. #13
    Join Date
    Jun 2005
    Location
    Richmond, Virginia USA
    Posts
    2,763
    Provided Answers: 19
    I don't think is a bug in Access; possibly a corrupted form or control. I'd delete the two controls involved and then recreate them. If that fails, I'd make a new form, copy the controls from the old to the new, then do the same for the code.

    Good Luck!
    Hope this helps!

    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

Posting Permissions

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