Can use SET for one control object but not the other--HELP!

J

jgrout

I'm just learning how to use VB to write code for Access 97 and am in the
middle of writing a simple event procedure to update a couple fields. I
decided to use the Set command to reference the control objects, as I have to
refer to the ones I am updating multiple times in my code. The problem is,
it works great for one of the control objects but doesn't work at all for the
other one! If I refer directly to the control object, the value is updated
no problem. Here's my code that doesn't work vs. code that does work:

DOESN'T WORK
Dim VC, Sell As Control
Set VC = Forms![Order Log]![Vendor Cost]
Set Sell = Forms![Order Log]![Revenue]
If PriceChoice = 1 Then
' this line doesn't work if I use "VC" but works fine if I use "Forms![Order
Log]![Vendor Cost]" instead of VC:
VC = VendorCost1
' this line works fine!
Sell = Price1
 
R

RoyVidar

jgrout said:
I'm just learning how to use VB to write code for Access 97 and am in
the middle of writing a simple event procedure to update a couple
fields. I decided to use the Set command to reference the control
objects, as I have to refer to the ones I am updating multiple times
in my code. The problem is, it works great for one of the control
objects but doesn't work at all for the other one! If I refer
directly to the control object, the value is updated no problem.
Here's my code that doesn't work vs. code that does work:

DOESN'T WORK
Dim VC, Sell As Control
Set VC = Forms![Order Log]![Vendor Cost]
Set Sell = Forms![Order Log]![Revenue]
If PriceChoice = 1 Then
' this line doesn't work if I use "VC" but works fine if I use
"Forms![Order Log]![Vendor Cost]" instead of VC:
VC = VendorCost1
' this line works fine!
Sell = Price1

This declaration:

Dim VC, Sell As Control

declares Sell as Control, but VC as Variant

You can use

Dim VC As Control
Dim Sell As Control

or

Dim VC As Control, Sell As Control

This *might* be the problem.
 
D

Dirk Goldgar

jgrout said:
I'm just learning how to use VB to write code for Access 97 and am in the
middle of writing a simple event procedure to update a couple fields. I
decided to use the Set command to reference the control objects, as I have
to
refer to the ones I am updating multiple times in my code. The problem
is,
it works great for one of the control objects but doesn't work at all for
the
other one! If I refer directly to the control object, the value is
updated
no problem. Here's my code that doesn't work vs. code that does work:

DOESN'T WORK
Dim VC, Sell As Control
Set VC = Forms![Order Log]![Vendor Cost]
Set Sell = Forms![Order Log]![Revenue]
If PriceChoice = 1 Then
' this line doesn't work if I use "VC" but works fine if I use
"Forms![Order
Log]![Vendor Cost]" instead of VC:
VC = VendorCost1
' this line works fine!
Sell = Price1


I'm not entirely sure why it doesn't work as written, but this line:
Dim VC, Sell As Control

.... declares VC as a Variant, not a Control. Change it to this:

Dim VC As Control, Sell As Control

and see if the rest of the code works now.
 
A

amigos

jgrout said:
I'm just learning how to use VB to write code for Access 97 and am in the
middle of writing a simple event procedure to update a couple fields. I
decided to use the Set command to reference the control objects, as I have
to
refer to the ones I am updating multiple times in my code. The problem
is,
it works great for one of the control objects but doesn't work at all for
the
other one! If I refer directly to the control object, the value is
updated
no problem. Here's my code that doesn't work vs. code that does work:

DOESN'T WORK
Dim VC, Sell As Control
Set VC = Forms![Order Log]![Vendor Cost]
Set Sell = Forms![Order Log]![Revenue]
If PriceChoice = 1 Then
' this line doesn't work if I use "VC" but works fine if I use
"Forms![Order
Log]![Vendor Cost]" instead of VC:
VC = VendorCost1
' this line works fine!
Sell = Price1
 

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