what is wrong with this code?

A

Al

I am getting an error message "Invalid qualifier" on the following code:

If !crfMenustatus = "Lock" Then
EnableControl Me![EditCmd1], Enable:=False
Else
EnableControl Me![EditCmd1], Enable:=True
End If
*****************************************
Private Sub EnableControl(ctlName As String, Enable As Boolean)
' Enable or disable the edit controls on the form.
ctlName.Enabled = Enable

End Sub
 
B

Bob Hairgrove

I am getting an error message "Invalid qualifier" on the following code:

If !crfMenustatus = "Lock" Then

This would (almost) be valid C or C++ syntax; however, in VBA you need
to write either this:

If crfMenustatus <> "Lock" Then

or this:

If Not (crfMenustatus = "Lock") Then
EnableControl Me![EditCmd1], Enable:=False
Else
EnableControl Me![EditCmd1], Enable:=True
End If
*****************************************
Private Sub EnableControl(ctlName As String, Enable As Boolean)
' Enable or disable the edit controls on the form.
ctlName.Enabled = Enable

End Sub

Isn't having a separate procedure to just set the Enabled property of
a control to True or False more than a little bit of overkill?
 
B

Brendan Reynolds

Are you sure that message doesn't say 'Invalid or unqualified reference'?

If it does, try ...

With Me
If !crfMenustatus = "Lock" Then
EnableControl Me![EditCmd1], Enable:=False
Else
EnableControl Me![EditCmd1], Enable:=True
End If
End With

.... or ...

If Me!crfMenustatus = "Lock" Then
EnableControl Me![EditCmd1], Enable:=False
Else
EnableControl Me![EditCmd1], Enable:=True
End If

.... or ...

If crfMenustatus = "Lock" Then
EnableControl Me![EditCmd1], Enable:=False
Else
EnableControl Me![EditCmd1], Enable:=True
End If

If the intention is to enable or disable the control named 'EditCmd1',
you'll also need to modify the code to pass the *name* of the control
instead of the value of the control ...

If crfMenustatus = "Lock" Then
EnableControl "EditCmd1", Enable:=False
Else
EnableControl "EditCmd1", Enable:=True
End If

If that is, infact, the intention, then you could replace all of that code
with ...

EditCmd1.Enabled = (crfMenuStatus <> "Lock")
 
B

BruceM

If I'm reading this correctly you seem to have attempted to build a
function, but if so:
1) It needs to be defined as a function rather than a sub
2) I would think you need Dim ctl as Control, then use syntax something like
ctl.ControlName.Enabled whatever. Maybe I'm missing something, but I don't
see what you hope to accomplish by the Boolean.
3) What's the point? Enabled is a control property. There is a simpler way
of addressing that.

I expect, though, that the code doesn't get that far. I think the
exclamation mark before crfMenustatus is going to give you problems. If
MenuStatus is a text field, why not just do:

If Me.Menustatus = "Lock" Then
Me.EditCmd1.Enabled = True
Else
Me.EditCmd1.Enabled = False
End If

Same if it's a text box, but just use the text box name.
 
A

Al

This works fine:
If !crfMenustatus = "Lock" Then
EnableControl Me![EditCmd1], Enable:=False
Else
EnableControl Me![EditCmd1], Enable:=True
End If
****************************************************
Private Sub EnableControl(ctlName, Enable As Boolean)
' Enable or disable the edit controls on the form.
ctlName.Enabled = Enable
End Sub
*************************************
the problem was with the ctlName variable. I left it as Variant. any idea if
I wanted to assign a specific data type, what would it be?
Al
Brendan Reynolds said:
Are you sure that message doesn't say 'Invalid or unqualified reference'?

If it does, try ...

With Me
If !crfMenustatus = "Lock" Then
EnableControl Me![EditCmd1], Enable:=False
Else
EnableControl Me![EditCmd1], Enable:=True
End If
End With

.... or ...

If Me!crfMenustatus = "Lock" Then
EnableControl Me![EditCmd1], Enable:=False
Else
EnableControl Me![EditCmd1], Enable:=True
End If

.... or ...

If crfMenustatus = "Lock" Then
EnableControl Me![EditCmd1], Enable:=False
Else
EnableControl Me![EditCmd1], Enable:=True
End If

If the intention is to enable or disable the control named 'EditCmd1',
you'll also need to modify the code to pass the *name* of the control
instead of the value of the control ...

If crfMenustatus = "Lock" Then
EnableControl "EditCmd1", Enable:=False
Else
EnableControl "EditCmd1", Enable:=True
End If

If that is, infact, the intention, then you could replace all of that code
with ...

EditCmd1.Enabled = (crfMenuStatus <> "Lock")

--
Brendan Reynolds
Access MVP


Al said:
I am getting an error message "Invalid qualifier" on the following code:

If !crfMenustatus = "Lock" Then
EnableControl Me![EditCmd1], Enable:=False
Else
EnableControl Me![EditCmd1], Enable:=True
End If
*****************************************
Private Sub EnableControl(ctlName As String, Enable As Boolean)
' Enable or disable the edit controls on the form.
ctlName.Enabled = Enable

End Sub
 
B

Brendan Reynolds

Change it to Control.

Alternatively you could change it back to String as you had it before, but
change the code that calls it to pass the name of the control in quotes as
in my previous example ...

EnableControl "EditCmd1", Enable:=False

.... and also change the code in the sub ...

Controls(ctlName).Enabled = Enable

That is to say, you can use As Control to pass a reference to the control as
an object, or you can use As String to pass the name of the control. The
trick is to be consistent, make sure if you use a reference to the control
as an object in the calling code, you don't then try to use the name of the
control as a string in the called code, or vice versa.

But as I said before, you can replace all of this code with a single line
....

EditCmd1.Enabled = (crfMenuStatus <> "Lock")

--
Brendan Reynolds
Access MVP

Al said:
This works fine:
If !crfMenustatus = "Lock" Then
EnableControl Me![EditCmd1], Enable:=False
Else
EnableControl Me![EditCmd1], Enable:=True
End If
****************************************************
Private Sub EnableControl(ctlName, Enable As Boolean)
' Enable or disable the edit controls on the form.
ctlName.Enabled = Enable
End Sub
*************************************
the problem was with the ctlName variable. I left it as Variant. any idea
if
I wanted to assign a specific data type, what would it be?
Al
Brendan Reynolds said:
Are you sure that message doesn't say 'Invalid or unqualified reference'?

If it does, try ...

With Me
If !crfMenustatus = "Lock" Then
EnableControl Me![EditCmd1], Enable:=False
Else
EnableControl Me![EditCmd1], Enable:=True
End If
End With

.... or ...

If Me!crfMenustatus = "Lock" Then
EnableControl Me![EditCmd1], Enable:=False
Else
EnableControl Me![EditCmd1], Enable:=True
End If

.... or ...

If crfMenustatus = "Lock" Then
EnableControl Me![EditCmd1], Enable:=False
Else
EnableControl Me![EditCmd1], Enable:=True
End If

If the intention is to enable or disable the control named 'EditCmd1',
you'll also need to modify the code to pass the *name* of the control
instead of the value of the control ...

If crfMenustatus = "Lock" Then
EnableControl "EditCmd1", Enable:=False
Else
EnableControl "EditCmd1", Enable:=True
End If

If that is, infact, the intention, then you could replace all of that
code
with ...

EditCmd1.Enabled = (crfMenuStatus <> "Lock")

--
Brendan Reynolds
Access MVP


Al said:
I am getting an error message "Invalid qualifier" on the following
code:

If !crfMenustatus = "Lock" Then
EnableControl Me![EditCmd1], Enable:=False
Else
EnableControl Me![EditCmd1], Enable:=True
End If
*****************************************
Private Sub EnableControl(ctlName As String, Enable As Boolean)
' Enable or disable the edit controls on the form.
ctlName.Enabled = Enable

End Sub
 
A

Al

First of all, I would like to thank every one for their very well thought
response. What I am trying to accomplish here is that I wanted to build a
generic procedures so that, I could use it through out the database with
other forms just by changing the argument to suite the situation. I have a
situation, through out the database, where I need to enable/disable buttons
on menus or forms based on different situations. I wanted to have a generic
and flexible way to do that without having to repeat a whole lots of code
every time. Later I would need to use this procedure into a loop that go
through several command buttons and disable/enable them. May be there are
better Ideas to address this. I am open to any idea and I certainley like
some of the ideas here.
thanks
Al

BruceM said:
If I'm reading this correctly you seem to have attempted to build a
function, but if so:
1) It needs to be defined as a function rather than a sub
2) I would think you need Dim ctl as Control, then use syntax something like
ctl.ControlName.Enabled whatever. Maybe I'm missing something, but I don't
see what you hope to accomplish by the Boolean.
3) What's the point? Enabled is a control property. There is a simpler way
of addressing that.

I expect, though, that the code doesn't get that far. I think the
exclamation mark before crfMenustatus is going to give you problems. If
MenuStatus is a text field, why not just do:

If Me.Menustatus = "Lock" Then
Me.EditCmd1.Enabled = True
Else
Me.EditCmd1.Enabled = False
End If

Same if it's a text box, but just use the text box name.

Al said:
I am getting an error message "Invalid qualifier" on the following code:

If !crfMenustatus = "Lock" Then
EnableControl Me![EditCmd1], Enable:=False
Else
EnableControl Me![EditCmd1], Enable:=True
End If
*****************************************
Private Sub EnableControl(ctlName As String, Enable As Boolean)
' Enable or disable the edit controls on the form.
ctlName.Enabled = Enable

End Sub
 

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