Disabling Controls

G

Guest

Hi all, I would appreciate any thoughts on this small problem.

First, a specific coding question:

dim frm as Form
dim ctl as Control

sub Disable()
Set frm = New Form_Form2
for each ctl in frm
If ctl.Name <> Screen.ActiveControl.Name Then
'you can't disable the button with the focus
If ctl.ControlType = acCommandButton Then
frm.ctl.enabled = false
'this is where the problem is
'if you substitute the actual names of a form and a
control, it works
'but it doesn't work as variables. So I've got some
'syntax or conceptual problem here.
end if
end if
next ctl
end sub

Second, a more general question as to what I'm trying to do. I'm opening a
document in Word with Automation from an Access form, and I'm using
WithEvents to monitor from Access what's happening in the Word document. But
I don't want the user to be able to exit from the Access form until the Word
document is closed. It seems the easiest way is simply to disable all the
Command Buttons while Word is open. I'm sure this would work if I enumerate
each button specifically, but I'd like to generalize it so that I can call it
from any form I want to "freeze."

Thank you.
 
M

Marshall Barton

Bruce said:
Hi all, I would appreciate any thoughts on this small problem.

First, a specific coding question:

dim frm as Form
dim ctl as Control

sub Disable()
Set frm = New Form_Form2
for each ctl in frm
If ctl.Name <> Screen.ActiveControl.Name Then
'you can't disable the button with the focus
If ctl.ControlType = acCommandButton Then
frm.ctl.enabled = false
'this is where the problem is
'if you substitute the actual names of a form and a
control, it works
'but it doesn't work as variables. So I've got some
'syntax or conceptual problem here.
end if
end if
next ctl
end sub

Second, a more general question as to what I'm trying to do. I'm opening a
document in Word with Automation from an Access form, and I'm using
WithEvents to monitor from Access what's happening in the Word document. But
I don't want the user to be able to exit from the Access form until the Word
document is closed. It seems the easiest way is simply to disable all the
Command Buttons while Word is open. I'm sure this would work if I enumerate
each button specifically, but I'd like to generalize it so that I can call it
from any form I want to "freeze."


You lost me on the second part, but I think the code is
thrown completely out of whack by your use of
Set frm = New Form_Form2
which opens another instance of Form2. If you really want
to have more copies of Form2 open, then your code is
probably trying to determine an active control before the
form has finished opening. Furthermore, the new instance of
Form2 will close as soon as the procedure exits and the frm
variable goes out of scope.

I think it far more likely that you just want to disable the
controls on the currently active form. In this case, try
using code more like this:

sub Disable()
dim frm as Form
dim ctl as Control
Set frm = Screen.ActiveForm
for each ctl in frm
If ctl.Name <> frm.ActiveControl.Name Then
'you can't disable the button with the focus
If ctl.ControlType = acCommandButton Then
ctl.enabled = false
end if
end if
next ctl
end sub
 
D

Douglas J. Steele

I think Marsh has given you a good answer.

I just wanted to point out that the error in your code was the inclusion of
the word frm in the line below:

frm.ctl.enabled = false

ctl.enabled = false is what's required.
 
G

Guest

Thank you very much. That was my problem. There may be a simpler way to
keep someone from leaving a form (without freezing Access with something like
a While/Wend), but this works nicely.

Once you know the syntax for something, it is obvious. Unfortunately, there
are lots of situations where I have a general idea of what I want to do, but
I play with the syntax combinations for hours without success (in this case,
all lasat weekend). Again, thank you!

Bruce Maston
 
M

Marshall Barton

You're welcome.

After you work with the object model for (more than?) a
little while, some sort of pattern emerges and the brain
just "gets it", so keep on truck'n ;-)
 

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