type mismatch

J

johnlute

This one has me stumped and I hope someone has a stump puller to help
me out! I have an AfterUpdate event in a combobox control of a subform
in datasheet view. The event returns a type mismatch error and the
debugger points to the line that I have inside the ><.
If (Forms![Marzetti Main Menu].Form![Class] = "MZ" Or "RD") Then<
Beep
If MsgBox("You can't assigned an allergen to this item! " & _
"Allergens can be assigned to RM and PK class items only.
", vbOKOnly + _
vbQuestion) = vbOK Then
Cancel = True
Me.cbAllergen.Undo
End If
End If

Is it because I'm not listing the subform? I tried throwing it in
there but it didn't resolve things.

As always any help is greatly appreciated!
 
J

Jeff Boyce

John

If I'm interpreting correctly, you are trying to compare the value of
[Class] to a boolean ("MZ" Or "RD"). I believe you need to test [Class] =
"MZ" Or [Class] = "RD"...

Or you could use the In() function...

Regards

Jeff Boyce
Microsoft Office/Access MVP

johnlute said:
This one has me stumped and I hope someone has a stump puller to help
me out! I have an AfterUpdate event in a combobox control of a subform
in datasheet view. The event returns a type mismatch error and the
debugger points to the line that I have inside the ><.
If (Forms![Marzetti Main Menu].Form![Class] = "MZ" Or "RD") Then<
Beep
If MsgBox("You can't assigned an allergen to this item! " & _
"Allergens can be assigned to RM and PK class items only.
", vbOKOnly + _
vbQuestion) = vbOK Then
Cancel = True
Me.cbAllergen.Undo
End If
End If

Is it because I'm not listing the subform? I tried throwing it in
there but it didn't resolve things.

As always any help is greatly appreciated!
 
D

Dirk Goldgar

johnlute said:
This one has me stumped and I hope someone has a stump puller to help
me out! I have an AfterUpdate event in a combobox control of a subform
in datasheet view. The event returns a type mismatch error and the
debugger points to the line that I have inside the ><.
If (Forms![Marzetti Main Menu].Form![Class] = "MZ" Or "RD") Then<
Beep
If MsgBox("You can't assigned an allergen to this item! " & _
"Allergens can be assigned to RM and PK class items only.
", vbOKOnly + _
vbQuestion) = vbOK Then
Cancel = True
Me.cbAllergen.Undo
End If
End If

Is it because I'm not listing the subform? I tried throwing it in
there but it didn't resolve things.

As always any help is greatly appreciated!


The problem is with the improper use of the conjunction "Or". If Class is
the name of a control on the form "Marzetti Main Menu", and that form is
opened as a main form, then this would be correct:

If Forms![Marzetti Main Menu]![Class] = "MZ" _
Or Forms![Marzetti Main Menu]![Class] = "RD" _
Then

However, you mention a subform. If Class is on the subform, then for an
absolute reference, the name of the subform control has to be included. I
don't know what that name is, but an example of the reference would be like
this:

If Forms![Marzetti Main Menu]!SubformControlName.Form![Class] = "MZ" _
Or Forms![Marzetti Main Menu]!SubformControlName.Form![Class] = "RD" _
Then

In the above, you would substitute the name of the subform control (the
control on the main form that displays the subform) for
"SubformControlName".

It may be possible to simplify this, though. If this code is executing on
the same form -- or subform -- that holds the Class control, then you can
use the "Me" keyword to refer to that form, and rewrite the statement like
this:

If Me![Class] = "MZ" Or Me![Class] = "RD" Then
 
J

johnlute

Thanks, Jeff and Dirk!
The problem is with the improper use of the conjunction "Or".

ARRRGGGHHHH! As soon as I read that I could feel the 40 Watt bulb in
my head flicker a bit!
 If Class is
the name of a control on the form "Marzetti Main Menu", and that form is
opened as a main form, then this would be correct:

    If Forms![Marzetti Main Menu]![Class] = "MZ" _
    Or Forms![Marzetti Main Menu]![Class] = "RD" _
    Then

That was it. I revised to:
If Forms![Marzetti Main Menu]![Class] = "MZ" _
Or Forms![Marzetti Main Menu]![Class] = "RD" Then
Beep
If MsgBox("You can't assigned an allergen to this item! " & _
"Allergens can be assigned to RM and PK class items only.
", vbOKOnly + _
vbQuestion) = vbOK Then
Cancel = True
Me.cbAllergen.Undo
End If
End If

Thanks again, guys!
 

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