Correct Event for textbox (1 Viewer)

kirkm

Registered User.
Local time
Tomorrow, 04:04
Joined
Oct 30, 2008
Messages
1,257
I think this is the wrong event
Code:
Private Sub txtArtist_KeyDown(KeyCode As Integer, Shift As Integer)
    With Me
        If .txtParent > "" And .txtYears > "" And .txtArtist.Text > "" Then
                .cmdAction.Enabled = True
            Else
                .cmdAction.Enabled = False
        End If
    End With
End Sub

I want cmdAction enabled to toggle as text is added or removed from txtArtist.
But the above only works after the second letter is entered, and/or a second delete in empty text box.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 08:04
Joined
Oct 29, 2018
Messages
21,359
Try using the Change event.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 23:04
Joined
May 7, 2009
Messages
19,175
as advised use Change Event or KeyUp event.
 

kirkm

Registered User.
Local time
Tomorrow, 04:04
Joined
Oct 30, 2008
Messages
1,257
Thanks again guys, Change event is working nicely. But now, can I call that code for each of the 3 text boxes Change?
Because you need .text on the curent one, but not those without focus.
Is there a way around that. Apart from repeating the procedure for each ?
 

theDBguy

I’m here to help
Staff member
Local time
Today, 08:04
Joined
Oct 29, 2018
Messages
21,359
Thanks again guys, Change event is working nicely. But now, can I call that code for each of the 3 text boxes Change?
Because you need .text on the curent one, but not those without focus.
Is there a way around that. Apart from repeating the procedure for each ?
Hi. I'm not sure I understand the question. You can only type into one Textbox at a time, so you'll need to use the event from each.

If you end up having the same code in all events, then you can create a Public Sub to consolidate the common code and pass the Text value as an argument.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 23:04
Joined
May 7, 2009
Messages
19,175
create a Public function in your form, and call it on Change event:

=fncEnableButton()


Code:
Public Function fncEnableButton()
    Dim arrControl(1 To 3) As String
    arrControl(1) = Me!txtParent & ""
    arrControl(2) = Me!txtYear & ""
    arrControl(3) = Me!txtArtist & ""

    Select Case Screen.ActiveControl.Name
        Case "txtParent"
            arrControl(1) = Me!txtParent.Text & ""
        Case "txtYear"
            arrControl(2) = Me!txtYear.Text & ""
        Case "txtArtist"
            arrControl(3) = Me!txtArtist.Text & ""
    End Select
    Me.cmdAction.Enabled = (Len(arrControl(1) & arrControl(2) & arrControl(3)) > 0)
End Function
 

kirkm

Registered User.
Local time
Tomorrow, 04:04
Joined
Oct 30, 2008
Messages
1,257
Thanks DBGuy you understood the issue perfectly and I just cobbled togther this
Code:
Private Sub txtArtist_Change()
    Liven txtParent, txtYears, txtArtist.Text
End Sub

Private Sub txtParent_Change()
    Liven txtParent.Text, txtYears, txtArtist
End Sub

Private Sub txtYears_Change()
    Liven txtParent, txtYears.Text, txtArtist
End Sub

Function Liven(p, y, a)
    With Me
        If p > "" And y > "" And a > "" Then
                .cmdAction.Enabled = True
            Else
                .cmdAction.Enabled = False
        End If
    End With
End Function
But now I see Arnes solution so about to try that. It's a lot more sophisicated.
 

kirkm

Registered User.
Local time
Tomorrow, 04:04
Joined
Oct 30, 2008
Messages
1,257
Arne thanks, it was worth seeing a differrnt approach, and I learnt from it. But it wasn't setting enable to false. Then I had to change your last line to
Code:
If Len(arrControl(1)) > 0 And Len(arrControl(2)) > 0 And Len(arrControl(3)) > 0 Then
        Me.cmdAction.Enabled = True
        Else
        Me.cmdAction.Enabled = False
    End If
You may have another idea ? Is working as hoped.
 

Users who are viewing this thread

Top Bottom