• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 320
  • Last Modified:

Select case with multiple criteria for each case?

I need to execute code based on the value of a control on one form in combination with the value on a second form. Is this doable in Select Case or do I need to be doing a series of nested If...Thens?

Here's a crack at it in Select Case

    Select Case Forms!frmSavedValue!txtUbSaved AND forms!frmMain!fsubGrtOrg.form!cboStatusK
           
        Case "cmdGrt" and "External":
                 instructions......
0
eliwil
Asked:
eliwil
1 Solution
 
dqmqCommented:
Well THAT won't work!  Do it like this:

 Select Case Forms!frmSavedValue!txtUbSaved & vbnullchar &  forms!frmMain!fsubGrtOrg.form!cboStatusK
      Case "cmdGrt" & vbnullchar &  "External":
0
 
Leigh PurvisDatabase DeveloperCommented:
Although when you're into multiple criteria then you are usually better off with an If statement.
Saves jumping through hoops
0
 
LambertHeenanCommented:
If you don't want an messy set of Ifs then I'd go for a set of nested Select Case statments

Select Case Forms!frmSavedValue!txtUbSaved
    Case "cmdGrt"
    Select Case forms!frmMain!fsubGrtOrg.form!cboStatusK
        Case "External"
            Some Code
        Case "Interal"
           More Code
        End Select
    Case "cmdSomthinElse"
    Select Case forms!frmMain!fsubGrtOrg.form!cboStatusK
        Case "External"
            Some Code
        Case "Interal"
           More Code
        End Select
End Select

Still a bit messy, but not as bat as the IF Them verions IMHO.

If all the nested case options are the same, then you could hide the inner Select Case statement by just calling a function in the outer Case, passing in the test value and have the function use a Select Case statement to figure out what to do. Otherwise just cut and paste the inner select case statement.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
hnasrCommented:
You may split it into 2 Case statements
Select Case Forms!frmSavedValue!txtUbSaved & vbnullchar &  forms!frmMain!fsubGrtOrg.form!cboStatusK
      Case "cmdGrt"
             Select Case forms!frmMain!fsubGrtOrg.form!cboStatusK  ' repeat this with every case of the outer case
                    Case   "External"
                      .......

            End Select
.....
End Select
, One for Forms!frmSavedValue!txtUbSaved, the other nested for forms!frmMain!fsubGrtOrg.form!cboStatusK
0
 
eliwilAuthor Commented:
If Forms!frmSavedValue!txtUbSaved = "cmdGrt" And frms!frmMain!fsubGrtOrg.form!cboStatusK = "External" or "Applied for" Then

would this statement work? not at all sure of the punctuation.
0
 
dqmqCommented:
Or you can nest selects:
Or you can nest If/then within the select:

Select Case Forms!frmSavedValue!txtUbSaved
 Case "cmdGrt":
       Select Case forms!frmMain!fsubGrtOrg.form!cboStatusK
        Case "External":
        ...
        End Select
 Case "xxxxxx":
      If  forms!frmMain!fsubGrtOrg.form!cboStatusK = "External" Then
         ...
      End If
 ...
End Select

I do agree with LPurvis:  readability/maintainability is more important that consolidating your tests into the fewest lines possible.

Also, "cmdGrt" looks suspiciously like the name of a control rather than the value of one????????????


0

Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now