(.Value) doesn't show textbox Value on Change() event (1 Viewer)

Nicha

New member
Local time
Today, 21:49
Joined
Jul 14, 2025
Messages
14
Hi,
I have two text boxes in a login form, and both have the same issue. I'm using the [Change()] event to check the number of characters typed manually to change the Text Box's background color.
The problem is that the (.Value) property doesn't reflect changes made while the focus is on the respective Text Box. Therefore, the color change only occurs when the focus moves to another control.
Similarly, whenever the text box is updated, the [AllControls_Change()] procedure is called to check if the fill is Ok and activate the [Cmd_Ok] button.

I found that if I change the property from (.Value to .Text) in the Change() event, it works, but it fails in the [AllControls_Change()] procedure, because of the Focus control.




The code below doesn't work in the [usertxt] control:
Code:
Private Sub usertxt_Change()
'Occurs evry key Pressed
'Stop
'   MsgBox Len(Trim$(Me.usertxt.Value))
   If Len(Trim$(Me.usertxt.Value) & vbNullString) > 0 Then
      Me.usertxt.BackColor = RGB(255, 255, 204)     '#FFFFFF            '&H80000005
   Else
      Me.usertxt.BackColor = RGB(255, 255, 255)     '&HC0FFFF
   End If


    AllControls_Change

End Sub

Public Sub AllControls_Change()
'Function to activate Ok Button.

Dim flg As Boolean
    
flg = (Len(Trim$(Me.usertxt.value)) = 7 And Len(Trim$(Nz(Me.passwtxt.value, ""))) > 0)


''Tenho de usar, por precaução, a propriedade text. (altera no evento Change), pois o Value só é alterado no
''AfterUpdate. Com isto o user pode deletar o dado e sem retirar o cursor podia ainda clicar no Botão Activo...
'Enabled_Event:
Me.Cmd_Ok.Enabled = flg
        

End Sub


P.s. I am attaching a file with the Form, for test purpouses.

Many thank's in advance.
 

Attachments

there is no .Value on change event (or it's .Value is the OldValue).
use the textbox .Text property instead.
 
i added two "hidden" textbox to put the .Text of each textbox to the hidden textbox.
 

Attachments

Hi @arnelgp, thank you very much for your answer. There is no way to make it work without having to create supporting text boxes?

If, in the Change() event, you change (.value) to (.Text), this event works, but the [AllControls_Change()] procedure does not perform the correct validation. Your solution works perfectly, but I would like to know if there is another way to solve it without having to create a text box for each textbox.

Many thank's to you.
 
If you don't want to use a textbox, you could try using TempVars.
 
Can you say if, in Access UserForms, the (.Value) will have the value updated in the Change() event?
 
Can you say if, in Access UserForms, the (.Value) will have the value updated in the Change() event?
Sorry, No. The Value property is usually updated only once the focus leaves the control.

PS. I don't use UserForms in Access. You might give it a try and maybe it behaves differently than regular forms.
 
There is no way to make it work without having to create supporting text boxes?
Yes, there is.

I guess Arnel was using hidden textboxes to demonstrate how it works. (I didn't look at his example)

During the Change event the value in the textbox is 'uncommited' and you must use the .Text property to get the contents. The .Text property is only available when the textbox has focus (which it will have during the Change event).

Adjust your code:
Code:
Private Sub usertxt_Change()
'Occurs evry key Pressed

   MsgBox Len(Trim$(Me.usertxt.Text))
   If Len(Trim$(Me.usertxt.Text) & vbNullString) > 0 Then
      Me.usertxt.BackColor = RGB(255, 255, 204)     '#FFFFFF            '&H80000005
   Else
      Me.usertxt.BackColor = RGB(255, 255, 255)     '&HC0FFFF
   End If


    AllControls_Change

End Sub

You may have to adjust AllControls_Change() as well, but I don't what the purpose of that code is.
 
Make the following changes in your code:
Code:
Private Sub passwtxt_Change()

  With Me
    If Len(Trim$(.passwtxt.Text & vbNullString)) > 0 Then
      .passwtxt.BackColor = RGB(255, 255, 204)    '&H80000005
    Else
      .passwtxt.BackColor = RGB(255, 255, 255)    '&HC0FFFF
    End If
    .Cmd_Ok.Enabled = EnableOK(.usertxt, .passwtxt.Text)
  End With

End Sub

Private Sub usertxt_Change()
  
  With Me
    If Len(Trim$(.usertxt.Text) & vbNullString) > 0 Then
      .usertxt.BackColor = RGB(255, 255, 204)     '#FFFFFF            '&H80000005
    Else
      .usertxt.BackColor = RGB(255, 255, 255)     '&HC0FFFF
    End If
    .Cmd_Ok.Enabled = EnableOK(.usertxt.Text, .passwtxt)
  End With

End Sub

Private Function EnableOK(user As Variant, pw As Variant) As Boolean

  EnableOK = Len(Trim$(user & vbNullString)) = 7 And Len(Trim$(pw & vbNullString)) > 0

End Function

Public Sub AllControls_Change()
'Function to activate Ok Button.

  With Me
    .Cmd_Ok.Enabled = EnableOK(.usertxt, .passwtxt)
  End With

End Sub

You can probably remove all the AfterUpdate events and probably the KeyPress events as not necessary.
 
I chose to use a UserForm, and everything worked out fine.
The purpose of the [AllControls_Change()] procedure is to validate whether the conditions are met to activate the [Cmd_Ok] button.

I want to Thank you all (@arnelgp, @theDBguy and @cheekybuddha), it was with your help that I was able to make a decision.

Can you tell me please, how to mark the @cheekybuddha (#11) post as the solution?
 
Can you tell me please, how to mark the @cheekybuddha (#11) post as the solution?
I think you just mark the whole thread as Solved (Is there a button at the top of the thread?)

I know you have said you are going to use UserForms, but I am attaching the Access version so you can see how simple it is in Access too.

(y)
 

Attachments

Users who are viewing this thread

Back
Top Bottom