Enable/Disable controls on form

G

Gillian

Hi,

I have a command button, which, when clicked enables four other controls
using the following code:

Me.PPD2_Name.Enabled = True
Me.PPD2__Qualification.Enabled = True
Me.PPD2_Age.Enabled = True
Me.PPD2_Years_of_Experience.Enabled = True

The problem is that when scrolling to a new record, the controls remain
enabled even though there is no data in them.

What I want to happen is if the controls have data in them, then they are
enabled and shown with the data but if there is no data in them (i.e. in a
new record) then the controls are disabled.

Thanks in advance.
 
S

SusanV

Hi Gillian,

Set the enabled property using the OnCurrent event of the form, based on the
value of the field:

Me.PPD2_Name.Enabled = Me.PPD2_Name.HasData
Me.PPD2__Qualification.Enabled = Me.PPD2__Qualification.HasData
Me.PPD2_Age.Enabled = Me.PPD2_Age.HasData
Me.PPD2_Years_of_Experience.Enabled = Me.PPD2_Years_of_Experience.HasData

(I'm assuming you mean each control is enabled if THAT control has data - if
it is based on a different control, modify the above accordingly.)
 
G

Gillian

Hi SusanV,

I'm being a bit stupid here but where you've got .HasData, am I supposed to
replace that with something because when I pasted your code and ran it, a
message appears saying "Method or Data Member not found". The data in the
four controls would be different for each record by the way.

Here is what I pasted in:-

Private Sub Form_Current()

Me.PPD2_Name.Enabled = Me.PPD2_Name.HasData
Me.PPD2__Qualification.Enabled = Me.PPD2__Qualification.HasData
Me.PPD2_Age.Enabled = Me.PPD2_Age.HasData
Me.PPD2_Years_of_Experience.Enabled = Me.PPD2_Years_of_Experience.HasData

End Sub

Thanks.
 
D

Douglas J. Steele

Susan may have been thinking of some other development environment, because
as far as I'm aware, there is no HasData property for controls in Access.

Try:

Private Sub Form_Current()

Me.PPD2_Name.Enabled = Len(Me.PPD2_Name & vbNullString) > 0
Me.PPD2__Qualification.Enabled = Len(Me.PPD2__Qualification & vbNullString)
Me.PPD2_Age.Enabled = Len(Me.PPD2_Age & vbNullString) > 0
Me.PPD2_Years_of_Experience.Enabled = Len(Me.PPD2_Years_of_Experience &
vbNullString) > 0

End Sub

I am puzzled, though, as to what you're trying to do. If it's a new record,
obviously it's not going to have any data in it. Doing what you're doing
means you won't be able to provide the data. (Although, of course, you
should never be storing calculated fields such as Age and Year of Experience
since they're constantly changing)
 
S

SusanV

Sorry - HasData is for Reports, not Forms - too early in the morning!
<pours coffee>

My apologies - try the following instead:


Me.[PPD2_Name].Enabled = (Nz(Me.[PPD2_Name], "") <> "")

(repeat for other fields)


Again, sorry for any confusion!
 
S

SusanV

Hi Doug,

I use this quite often in Reports to hide labels when a subreport doesn't
have any data:

Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
Me.lblNewPrec.Visible = Me.subrepLCodeNewPrec.Report.HasData
End Sub


Good point on New Records!

SusanV
 
G

Gillian

Hey Susan,

No worries, I often have days like that!!!

Thanks again.

SusanV said:
Sorry - HasData is for Reports, not Forms - too early in the morning!
<pours coffee>

My apologies - try the following instead:


Me.[PPD2_Name].Enabled = (Nz(Me.[PPD2_Name], "") <> "")

(repeat for other fields)


Again, sorry for any confusion!
--
hth,
SusanV


Gillian said:
Hi SusanV,

I'm being a bit stupid here but where you've got .HasData, am I supposed
to
replace that with something because when I pasted your code and ran it, a
message appears saying "Method or Data Member not found". The data in the
four controls would be different for each record by the way.

Here is what I pasted in:-

Private Sub Form_Current()

Me.PPD2_Name.Enabled = Me.PPD2_Name.HasData
Me.PPD2__Qualification.Enabled = Me.PPD2__Qualification.HasData
Me.PPD2_Age.Enabled = Me.PPD2_Age.HasData
Me.PPD2_Years_of_Experience.Enabled = Me.PPD2_Years_of_Experience.HasData

End Sub

Thanks.
 
G

Gillian

Hey Douglas,

It's Ok, your code works really well. Perfectly in fact for what I want.

Just for info, what I have is the four fields: name, qualification, age and
years of experience which are already enabled so the inputter can put one
person's details in no problem because there will always be one person. But,
if there is another person's details to add, then by clicking the command
button, it brings up four same controls but named slightly differently so
that a second person's details can be entered. I'm going to use the same code
to add a third person and a fourth. What I didn't want is if there isn't a
third or fourth person, then those controls remain disabled but they are
enabled if another record has a second/third/fourth person.

I hope this straightens my question out and many thanks for your help.
 
D

Douglas J. Steele

The HasData property in that case is a property of the report being used as
a subreport.
 
Top