You can't reference a property or method for a control unless the control has the focus.

A

ATJaguarX

I get the following error message when running the following code:

Private Sub NewAccountNumber_Change()

If Len(Me.NewAccountNumber.text) > 0 Then
cmdAdd.Enabled = True
Else
cmdAdd.Enabled = False
End If

Exit Sub

I do not want to enable the add button until they atleast enter
something into the "NewAccountNumber" field.

I've done research and found people telling me to set focus to the
control. I have tried doing that with no success. Also... why would I
have to set focus on a control, who's event "changed" event has just
fired... it should already have focus.

This makes no sense.
 
T

Tom Lake

Private Sub NewAccountNumber_Change()
If Len(Me.NewAccountNumber.text) > 0 Then
cmdAdd.Enabled = True
Else
cmdAdd.Enabled = False
End If

Exit Sub

I do not want to enable the add button until they atleast enter
something into the "NewAccountNumber" field.

Try this:

If Len(Me!NewAccountNumber.text) > 0 Then
Me!cmdAdd.Enabled = True
Else
Me!cmdAdd.Enabled = False
End If

I believe you still have to qualify references to the cmdAdd button.
I use the bang for control names and the dot for properties. I
find it less confusing.

Tom Lake
 
A

ATJaguarX

No go... still doesn't work. It blows up on the following line:

"If Len(Me!NewAccountNumber.text) > 0 Then "
 
D

Douglas J Steele

Don't use the .Text property.

Either use the .Value property, or leave the property out all together:

If Len(Me!NewAccountNumber.Value) > 0 Then
Me!cmdAdd.Enabled = True
Else
Me!cmdAdd.Enabled = False
End If

or

If Len(Me!NewAccountNumber) > 0 Then
Me!cmdAdd.Enabled = True
Else
Me!cmdAdd.Enabled = False
End If

Of course, you can always make that more compact by:

Me!cmdAdd.Enabled = (Len(Me!NewAccountNumber.Value) > 0)

or

Me!cmdAdd.Enabled = (Len(Me!NewAccountNumber) > 0)

As well, you might want to append a zero-length string, in case the control
is actually Null:

Me!cmdAdd.Enabled = (Len(Me!NewAccountNumber & "") > 0)

or

Me!cmdAdd.Enabled = (Len(Me!NewAccountNumber & vbNullString) > 0)


Doug Steele, Microsoft Access MVP

(no e-mails, please!)
 
A

ATJaguarX

Let me give a little more detail. This page gets loaded from a "Store"
screen. A "Store" can have multiple account numbers with our vendors.
This screen allows the setup of Store Account Numbers for the Vendors.

If I open a page for a Store where account numbers already exist (this
is a "Continues" form that has all the account numbers repeated on the
2nd half of the screen), the event mentioned above fires fine.

If I open a page for a Store where NO account numbers exist (Access
Query returns 0 records), the event mentioned above generates the
error. As soon as the first account number is added and the form has a
1 record recordset, the event fires correctly for the remainder of the
session. If I remove all the Account numbers from the Store, I get the
same error (since there are no longer any account numbers assigned).

Based on these results, I'm assuming it has something to do with the
form not having any data. I don't know why because the textbox is just
a basic text box.

I appologize for my lack of knowledge. I have limited experience with
Access since I am a .NET developer and just trying add functionality to
an existing clients application.
 
A

ATJaguarX

The problem with this (correct me if I'm wrong... this is just what
I've learned scanning these forums) is that the "Value" is not set
until the textbox loses focus. Based on my testing, this holds to be
true as the "Value" is "Null" until the control loses focus.
 
A

ATJaguarX

The problem with this (correct me if I'm wrong... this is just what
I've learned scanning these forums) is that the "Value" is not set
until the textbox loses focus. Based on my testing, this holds to be
true as the "Value" is "Null" until the control loses focus.
 
J

Jayyde

It could be the btn.enabled that it doens't like. I've had it want focus
set on the button before enabling/disabling it.
 
G

George Nicholson

I've had it want focus set on the button before enabling/disabling it.

Just the opposite.

It's not possible to disable a control that has focus. You'd have to move
the focus first. And, by definition, a disabled control can't accept focus,
so you'd never be able to enable it, if, as you suggest, focus was a
requirement.

HTH,
 
A

ATJaguarX

it is not the btn.enabled event... it blows up on the following line:

"If Len(Me!NewAccountNumber.text) > 0 Then "

Like I had mentioned in a previous post... this ONLY happens when the
query to populate other controls on the form is empty. If there is on
record to display, no errors are generated.
 
G

George Nicholson

this ONLY happens when the
query to populate other controls on the form is empty.

Then the error is probably raised by the evaluation of: Len(Null). Try
changing the line to:

If Len(Nz(Me!NewAccountNumber.text,"")) > 0 Then
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top