0

I wish to program a click event in a "Main Information" form that will open up to 17 other forms based on values selected in a multiple-answer look-up field combo control (corresponding to a numeric ID field/text string categories related via an independent, separate table) housed within that "Main Information" form.

I have learned this is the general code:

 Private Sub DiagCat_AfterUpdate()
   Select Case DiagCat
     Case "Cancer [140-208]"
       DoCmd.OpenForm (Cancer_Form)
     Case "Heart Disease [393-398, 402, 410-429]"
       DoCmd.OpenForm (Heart_Disease_Form)
     Case "Stroke [430-438]"
      DoCmd.OpenForm (Stroke_Form)
     Case "Diabetes [250]"
      DoCmd.OpenForm (Diabetes_Form)
     Case "Hypertension [401]"
      DoCmd.OpenForm (Hypertension_Form)
    Case "Liver Disease [070, 571-573]"
      DoCmd.OpenForm (Elevated_Cholesterol_Form)
   End Select
 End Sub

VBA issues a "Type Mismatch" error 13 now; the error executes on the 'Case "Cancer [140-208]"' line.

Thank you. Any assistance greatly appreciated.

13
  • You would be wise to tell us which line of the code triggers the error. This could be the culprit: DoCmd.OpenForm (Cancer_Form) What is Cancer_Form? OpenForm needs the form name as a string value, so I'll guess Cancer_Form is the name of a form and you should use: DoCmd.OpenForm "Cancer_Form" Parentheses not required.
    – HansUp
    Oct 14, 2015 at 17:13
  • Add Option Explicit to your code module's Declarations section. Then run Debug->Compile from the VB Editor's main menu. Fix anything the compiler complains about, and repeat compile/fix until no more compile errors.
    – HansUp
    Oct 14, 2015 at 17:18
  • Thank you so much HansUp! I will try this. If there is no underscore in the Form title, should it be "Cancer Form"?
    – JRNewm
    Oct 14, 2015 at 17:22
  • Yes, I think so. Double-check the form name in the Navigation pane. However it's displayed there, put the matching text between the quotes here: DoCmd.OpenForm "<form Name>"
    – HansUp
    Oct 14, 2015 at 17:25
  • Unfortunately, after I add "Option Explicit", and correct all the form names after the DoCmd's, I still receive a type 13 mismatch error on the line 'Case "Cancer [140-208]"'- unsure what I am doing incorrectly. Thanks for all your help anyway.
    – JRNewm
    Oct 14, 2015 at 17:29

1 Answer 1

0

The Select Case keyword is what you need to select an action based on multiple values of a field.

Private Sub cmdOpen_Click() 'where cmdOpen is the button control clicked to open the forms
      Select Case cmbDxCatsID 'assuming cmbDxCatsID is the name of the combobox control on the form
      Case 1
          DoCmd.OpenForm(Form1)
      Case 2
          DoCmd.OpenForm(Form2)
      'etc.........
      End Select
End Sub
4
  • Thank you so much! Very grateful!
    – JRNewm
    Oct 14, 2015 at 14:11
  • Woops- new to StackOverflow- I clicked the check- I think that accepts it. Thanks again!
    – JRNewm
    Oct 14, 2015 at 14:53
  • So unfortunately, even though I think this is the right way to go about this, I am still getting errors. The specific combo box that is used to generate the "select case" DoCmds is actually a drop-down box that uses SQL to generate LookUp values from another independent table. I was trying to have the cases be specific text strings of the categories able to be chosen, but Access/VBA keeps telling me there is a "Type Mismatch"; is there anyway to referenced the table/field/values of the table each look-up value refers to? Am I doing this completely wrong?
    – JRNewm
    Oct 14, 2015 at 16:52
  • I also tried moving the "click event" from an independent button to the very drop down combo box control itself, to Open Forms on the After Update event:
    – JRNewm
    Oct 14, 2015 at 16:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.