Restricting button until required data entered

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I want to restrict an "Accept" button to a Tracking note on the same form
until it has at least 5 characters in it. I thought the code below would
work in the KeyDown or Keypress event of the textbox (Me.TrackingNote). But
I'm still missing something. What is it?

If Len(Me.TrackingNote) > 5 Then
Me.Accept.Enabled = True
Else
Me.Accept.Enabled = False
End If
 
Jonefer,

I think what you are missing is that Len(Me.TrackingNote) will be 0
until the data you are entering is saved. If appropriate, you coule try
to save the record after every keystroke. There may be a more elegant
way around this, but I can't think of one at the moment. Something like
this...
Me.Dirty = False
Me.Accept.Enabled = Len(Nz(Me.TrackingNote,"")) > 5
 
try the following, as

Private Sub TrackingNote_Change()

Me!Accept.Enabled = Len(Me!TrackingNote.Text) > 5

End Sub

the single line of code above acts as a toggle, replacing the If, Then, Else
statement. note that the code runs on the control's Change event, and counts
the characters in the control's Text property. see this note from the Text
Property topic in Access VBA Help:

"While the control has the focus, the Text property contains the text data
currently in the control; the Value property contains the last saved data
for the control."

hth
 
I want to restrict an "Accept" button to a Tracking note on the same form
until it has at least 5 characters in it. I thought the code below would
work in the KeyDown or Keypress event of the textbox (Me.TrackingNote). But
I'm still missing something. What is it?

If Len(Me.TrackingNote) > 5 Then
Me.Accept.Enabled = True
Else
Me.Accept.Enabled = False
End If

You did say "at least 5 characters".
As written above, you are asking for at least 6 characters.

Until the data is saved, you need to reference it's Text property.

Use the [TrackingNote]'s Change event:

If Len(Me.TrackingNote.Text) >= 5 Then
Me.Accept.Enabled = True
Else
Me.Accept.Enabled = False
End If

Also, place the following code in the Form's Current event:

If Len(Me.TrackingNote) >= 5 Then
Me.Accept.Enabled = True
Else
Me.Accept.Enabled = False
End If

Now you are referring to the control's Value property as the data has
already been saved.
 
Ah, yes, the Text property. Darn, I just *knew* there was something I
couldn't thing of!
 
That was excellent. Thank you

tina said:
try the following, as

Private Sub TrackingNote_Change()

Me!Accept.Enabled = Len(Me!TrackingNote.Text) > 5

End Sub

the single line of code above acts as a toggle, replacing the If, Then, Else
statement. note that the code runs on the control's Change event, and counts
the characters in the control's Text property. see this note from the Text
Property topic in Access VBA Help:

"While the control has the focus, the Text property contains the text data
currently in the control; the Value property contains the last saved data
for the control."

hth
 
Thank you for that explanation.

fredg said:
I want to restrict an "Accept" button to a Tracking note on the same form
until it has at least 5 characters in it. I thought the code below would
work in the KeyDown or Keypress event of the textbox (Me.TrackingNote). But
I'm still missing something. What is it?

If Len(Me.TrackingNote) > 5 Then
Me.Accept.Enabled = True
Else
Me.Accept.Enabled = False
End If

You did say "at least 5 characters".
As written above, you are asking for at least 6 characters.

Until the data is saved, you need to reference it's Text property.

Use the [TrackingNote]'s Change event:

If Len(Me.TrackingNote.Text) >= 5 Then
Me.Accept.Enabled = True
Else
Me.Accept.Enabled = False
End If

Also, place the following code in the Form's Current event:

If Len(Me.TrackingNote) >= 5 Then
Me.Accept.Enabled = True
Else
Me.Accept.Enabled = False
End If

Now you are referring to the control's Value property as the data has
already been saved.
 
LOL, been there! in my case, i just blame it on age (the only advantage to
getting older) <bg>
 
oops, my bad, Fred's right on both counts. my "at least five characters"
code in the Change event should be

Me!Accept.Enabled = Len(Me!TrackingNote.Text) > 4

and it should run in the form's Current event with a slight change to read
the Value property rather than the Text property, as

Me!Accept.Enabled = Len(Me!TrackingNote) > 4

Fred's code and mine show you two different ways to accomplish the same
result.

hth
 
Back
Top