By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,480 Members | 1,229 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,480 IT Pros & Developers. It's quick & easy.

Using a Macro to Set the Value of a Combo Box

P: n/a
Hi Everyone-

I am trying to use a simple macro to set the value of a combo box on a
form, and I can't get it to work.

I have a macro with 2 actions: OpenForm and SetValue. I can open my
form, but I can't get the macro to set the value of a combo box on the
form that opens. I don't need the macro to look up any values, I just
want it to set the value of a combo box.

I have a very simple table called tblDivision. It has 2 fields:

DivisionID (Primark Key, text)
DivisionDescription (text)

DivisionID DivisionDescription
NFC National Football League
AFC American Football League

My form that opens has information about the teams that fall into one
of these 2 leagues, i.e. team city, team name, etc...

At the top of my form, is a combo box. In this combo box, you can
select "NFC" or "AFC" to describe which league the team is in. But I'm
trying to use a command button (on another form) that runs a macro that
will automatically set the value of the combo box on the form to "NFC"
or "AFC." I have one command button that says "Create NFC Team" and
another command button that says "Create AFC Team." Both command
buttons run a macro that opens the same form. Based on which command
button I hit, I want the value of the combo box set to that value. For
instance if I hit the "Create NFC Team" command button, I want my macro
to set the value of my combo box on my team form set to "NFC." And if I
hit the "Create AFC Team" command button, I want the value of the combo
box on my team form set to "AFC."

Basically, I just want the macro to set the value of my combo box to
either "NFC" or "AFC."

In the "Item" dialog box of the "Set Value" action, I have the name of
my control, a combo box named cboDivision.

I'm having problems with the "Expression" part of my "Set Value"
action. I'm not sure what to enter here. I just want to set the value
of the combo box to "NFC" or "AFC" depending on which command button I
hit. I've tried a few things but my macro always halts. I would just
set the "Expression" = "NFC" or = "AFC" but it says that you shouldn't
use the equals sign. Is there any way to do this? I thought it would be
easy, but it has been a pain in my butt!

Thanks!

Megan

Nov 13 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Yeah, macros basically suck. Once you learn to code a little, you'll
rarely use them again. But, that said, here's what you do...

create a new macro (or modify yours)
steps...
OpenForm: frmTeam
DataMode: Add
WindowMode: Normal
SetValue:
Item: [Forms]![frmTeam]![Division]
Expression: "NFC"

You could convert all this to code and it would look like this:

Option Compare Database

'------------------------------------------------------------
' mcrAddAFC
'
'------------------------------------------------------------
Function mcrAddAFC()
On Error GoTo mcrAddAFC_Err

DoCmd.OpenForm "frmTeam", acNormal, "", "", acAdd, acNormal
Forms!frmTeam!Division = "AFC"
mcrAddAFC_Exit:
Exit Function

mcrAddAFC_Err:
MsgBox Error$
Resume mcrAddAFC_Exit

End Function

Nov 13 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.