Search code examples
databaseformsvbams-accessopenform

Database Design/Open Form CLICK EVENT in VBA using Access: Specific Inquiry


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.


Solution

  • 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