A Better Way To Code This

D

DS

Is there a better way to code this, Like a global command or something?
Thanks
DS

Private Sub Command43_Click()
On Error GoTo Err_Command43_Click
DoCmd.RunCommand acCmdSaveRecord
Me.BusinessName.Enabled = False
Me.BusinessName.Locked = True
Me.FName.Enabled = False
Me.FName.Locked = True
Me.LName.Enabled = False
Me.LName.Locked = True
Me.Address.Enabled = False
Me.Address.Locked = True
Me.Apt.Enabled = False
Me.Apt.Locked = True
Me.City.Enabled = False
Me.City.Locked = True
Me.State.Enabled = False
Me.State.Locked = True
Me.ZipCode.Enabled = False
Me.ZipCode.Locked = True
Me.Tel.Enabled = False
Me.Tel.Locked = True
Me.Ext.Enabled = False
Me.Ext.Locked = True
Me.Fax.Enabled = False
Me.Fax.Locked = True
Me.EMail.Enabled = False
Me.EMail.Locked = True
Me.HouseAccount.Enabled = False
Me.HouseAccount.Locked = True
Me.CreditLimit.Enabled = False
Me.CreditLimit.Locked = True
Me.Command33.Visible = True
Me.Command34.Visible = True
Me.Command42.Visible = True
Me.Command42.SetFocus
Me.Command43.Visible = False
Me.Command44.Visible = True
Me.List40.Requery
Exit_Command43_Click:
Exit Sub

Err_Command43_Click:
MsgBox Err.Description
Resume Exit_Command43_Click

End Sub
 
T

tina

well, without knowing the context of what you're doing, it's hard to suggest
a specific "better" way. for instance, if the controls that you enumerated
constitute *all* the editable controls on the form (presumably you're
working with a form), it might work equally well to simply set the form's
AllowEdits property to False - and it's certainly much shorter and easier.
or, if all the enumerated controls are textboxes, you could write code to
loop through all the controls on the form, and set the Enabled and Locked
properties of all controls that are textboxes. or, if you need to pick and
choose certain controls to be disabled and locked, you might set the Tag
property of those specific controls to....anything, actually, such as "lock"
or just an "x", and run a loop through all form controls, setting the
Enabled and Locked properties of those controls whose Tag property equals
"lock" or "x" or "whatever".

keep in mind that, whatever method you choose to lock certain controls, you
will probably need to also unlock them where appropriate - either
automatically, or by allowing the user to unlock them manually.

hth
 
A

Albert D.Kallal

I guess the question is "which" set of controls do you want to operate on.

There is a property in each control that is "free to" use for whatever you
want. It is called the tag property.

So, for controls that you want to operator on, you can make up a value for
the "tag" property, and use that
(you can find the tag property in the "other" tab)

So, you could while in design mode set the tag value. Lets assume you set
the "tag" value to

Set1

So, now, you code could look like:


Dim c As Control

For Each c In Controls
If c.tab = "set1" Then
c.Enabled = False
c.Locked = True
End If
Next c

There is many many ways to skin a cat here.

You could use:

Dim strFields As String
Dim v As Variant
Dim i As Integer

v = Split("BusinessName,FName,LName,Address,Apt,City,State,ZipCode", ",")

For i = 0 To UBound(v)
Me(v(i)).Enabled = False
Me(v(i)).Locked = True
Next i


And, of course, you likely could setup the routine to pass a var

So, you go:

Call SetControls(True) ' to enable contorls
Call SetContorls(False) ' to disable contorls

You routine then looks like

Sub SetControls(bolOnOff as boolean)

For Each c In Controls
If c.tab = "set1" Then
c.Enabled = bolOnOff
c.Locked = not(bolOnOff)
End If
Next c


There is even more approaches, but the above has some good ideas to get you
going....
 
D

DS

tina said:
well, without knowing the context of what you're doing, it's hard to suggest
a specific "better" way. for instance, if the controls that you enumerated
constitute *all* the editable controls on the form (presumably you're
working with a form), it might work equally well to simply set the form's
AllowEdits property to False - and it's certainly much shorter and easier.
or, if all the enumerated controls are textboxes, you could write code to
loop through all the controls on the form, and set the Enabled and Locked
properties of all controls that are textboxes. or, if you need to pick and
choose certain controls to be disabled and locked, you might set the Tag
property of those specific controls to....anything, actually, such as "lock"
or just an "x", and run a loop through all form controls, setting the
Enabled and Locked properties of those controls whose Tag property equals
"lock" or "x" or "whatever".

keep in mind that, whatever method you choose to lock certain controls, you
will probably need to also unlock them where appropriate - either
automatically, or by allowing the user to unlock them manually.

hth
Basiclly I want all of the forms textboxs that are used for data entry
on the form locked until the user clicks on a command button to either
add a new record or another command button to edit the record. I also
have a listbox on the form that is used to find records that the user
clicks on if this matters.
Thanks
DS
 
K

Ken Snell [MVP]

This is a time when the Tag property of the controls is useful. If you put a
value in the Tag property of the controls that you want to enable/disable
and lock/unlock, the code becomes much simpler.

For example, let's say you put
1
in the Tag property of all these controls. Then the code becomes this:

Private Sub Command43_Click()
Dim ctl As Control
On Error GoTo Err_Command43_Click
DoCmd.RunCommand acCmdSaveRecord
For Each ctl in Me.Controls
If ctl.Tag = "1" Then
ctl.Enabled = False
ctl.Locked = True
End If
Next ctl
Me.List40.Requery
Exit_Command43_Click:
Exit Sub

Err_Command43_Click:
MsgBox Err.Description
Resume Exit_Command43_Click

End Sub
 
D

DS

Ken said:
This is a time when the Tag property of the controls is useful. If you put a
value in the Tag property of the controls that you want to enable/disable
and lock/unlock, the code becomes much simpler.

For example, let's say you put
1
in the Tag property of all these controls. Then the code becomes this:

Private Sub Command43_Click()
Dim ctl As Control
On Error GoTo Err_Command43_Click
DoCmd.RunCommand acCmdSaveRecord
For Each ctl in Me.Controls
If ctl.Tag = "1" Then
ctl.Enabled = False
ctl.Locked = True
End If
Next ctl
Me.List40.Requery
Exit_Command43_Click:
Exit Sub

Err_Command43_Click:.
MsgBox Err.Description
Resume Exit_Command43_Click

End Sub
Wow thats cool! I'll give it a try. Beats setting all of those fields!
Thanks
DS
 
D

DS

DS said:
Wow thats cool! I'll give it a try. Beats setting all of those fields!
Thanks
DS
Ken This is the greatest thing since.....! It saves a ton of work.
Thank you so very much!
Sincerely
DS
 
D

DS

Albert said:
I guess the question is "which" set of controls do you want to operate on.

There is a property in each control that is "free to" use for whatever you
want. It is called the tag property.

So, for controls that you want to operator on, you can make up a value for
the "tag" property, and use that
(you can find the tag property in the "other" tab)

So, you could while in design mode set the tag value. Lets assume you set
the "tag" value to

Set1

So, now, you code could look like:


Dim c As Control

For Each c In Controls
If c.tab = "set1" Then
c.Enabled = False
c.Locked = True
End If
Next c

There is many many ways to skin a cat here.

You could use:

Dim strFields As String
Dim v As Variant
Dim i As Integer

v = Split("BusinessName,FName,LName,Address,Apt,City,State,ZipCode", ",")

For i = 0 To UBound(v)
Me(v(i)).Enabled = False
Me(v(i)).Locked = True
Next i


And, of course, you likely could setup the routine to pass a var

So, you go:

Call SetControls(True) ' to enable contorls
Call SetContorls(False) ' to disable contorls

You routine then looks like

Sub SetControls(bolOnOff as boolean)

For Each c In Controls
If c.tab = "set1" Then
c.Enabled = bolOnOff
c.Locked = not(bolOnOff)
End If
Next c


There is even more approaches, but the above has some good ideas to get you
going....
Thank you Albert a whole lot of choices! This certanly makes life
easier and the cats safe !!!!
Once again Thank you Albert
Sincerely
DS
 

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

Similar Threads


Top