Code to run macro from combo box result

Snufflz

Registered User.
Local time
Today, 04:15
Joined
Mar 12, 2010
Messages
23
Hello,

I would really appreciate some help with coding problem. As you can see I am not very well up on it!

I have a combo box on a form returning three values - Engineer, Medical, Both.

I also have three matching macros which will print a particular letter (form) for the relevant client record - M:letter_PALS_eng, M:letter_PALS_med, M:letter_PALS_both.

What I want to be able to do is for the result of the combo box to trigger the relevant macro, which will print the required letter out.

-------
Private Sub PALS_letter_combo_AfterUpdate()

iif(Forms![F:fault]![PALS letter combo].Column(1)="Engineer", DoCmd.runMacro "M:letter_PALS_eng",,"
iif(Forms![F:fault]![PALS letter combo].Column(1)="Medical", DoCmd.runMacro "M:letter_PALS_med",,"
iif(Forms![F:fault]![PALS letter combo].Column(1)="Both", DoCmd.runMacro "M:letter_PALS_both",,"

End Sub
-------

This is the code I have put in the after event on the combo box and, no surprise, it doesn't work. I keep getting compile errors saying 'expected'.

I would be very grateful if someone could point me in the right direction. (Also bearing in mind that my knowing of code is somewhat sketchy!)

Many thanks.
 
iif(Forms![F:fault]![PALS letter combo].Column(1)="Engineer", DoCmd.runMacro "M:letter_PALS_eng",,"
iif(Forms![F:fault]![PALS letter combo].Column(1)="Medical", DoCmd.runMacro "M:letter_PALS_med",,"
iif(Forms![F:fault]![PALS letter combo].Column(1)="Both", DoCmd.runMacro "M:letter_PALS_both",,"

This code is fraught with errors.

Firstly you cannot use Iff as an opening statement
Secondly remove spaces from names
Thirdly don't use anything other than characters as digits in the names (Colons)

The best method is as follows

Code:
Dim MyMacroName As String

Select Case Me.Combo.Column(1)

   Case "Medical" : MyMacroName = "MacroName1"

   Case "Engineer" : MyMacroName = "MacroName2"

   Case "Both" : MyMacroName = "MacroName3"

End Select

DoCmd.RunMacro MyMacroName

Remember to change the my names for the real names as code is for brevity only.
 
You do not surprise me. I did not have a clue how to go about it so I took my best shot and got it completely wrong.

Thank you for your reply.

I've changed the code but nothing happens, which is better than the errors I was getting before!

Should the macros be an individual file each or part of one file? I now have one macro file called m_letter_PALS and within that are three macro names (mmed, meng and mboth). Each are set to print the relevant form based on where conditions.

Regards.
 

Users who are viewing this thread

Back
Top Bottom