PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, November 22, 2022

[FIXED] How can I show a value based on multiple conditions?

 November 22, 2022     excel, multiple-conditions, vba     No comments   

Issue

In my form I want users to be able to see a Colour or an Animal based on an optionbox AND based on their input. The code I have attempted so far hasnt worked for me, and I think its because I dont understand how to properly format it.

I have tried doing it per number for every colour, I've tried just adding the numbers as possible values and I tried creating an array. No succes however.

For example.

If Optionbutton A is selected, and the textbox A value is 1, 4, or 10, then textbox B will show Blue. But if textbox A value is 2, 3, 5, or 9 then textbox B will show Green.

Example 2.

Optionbutton B, and textbox A value is 1, 3, 4, 10 or 12, then textbox B will show Butterfly. But if textbox A value is 2, 5, 7 then textbox B will show Caterpie.

Added note, I dont know if this possible with the form, but if the user were to toggle between optionboxes that textbox B would change accordingly.

Private Sub txtboxA_Change()

Dim txtboxA As String
Dim Colourarray As Variant

Blue = Array("1", "4", "10")

Me.txtboxA.MaxLength = 2

'If OptA is selected then pick colour based on textbox value

If Me.OptA.Value = True And Me.txtboxA.Value = "1" Then 'This bit seemed to work, but once I copied this for every number which needs to show green, vba got unhappy at me. 
'If Me.OptA.Value = True And Me.txtboxA.Value = "1", "4", "10" Then
'If Me.optA.Value = True And (Poort = "1", "4", "10") Then
'If Me.txtboxA.Value = Colourarray And Me.optA.Value = True Then
Me.txtboxB.Value = "Blue"
End If
End Sub

Solution

This uses a select case instead of an if.

Private Sub txtboxA_Change()

    Dim txtboxA As String
    Dim optionchoice As Long

    Me.txtboxA.MaxLength = 2

    'option A = 1; option B = 2
    If Me.optA.Value = True Then 'Easier to do a select case on optionchoice over option button
        optionchoice = 1
    ElseIf Me.optB.Value = True Then
        optionchoice = 2
    End If


    Select Case optionchoice 'Based on Option Button selection
        Case 1 'Option A
            Select Case Me.txtboxA.Value 'Based on Text Box value
                Case 1, 4, 10
                    Me.txtBoxB.Value = "Blue"
                Case 2, 3, 5, 9
                    Me.txtBoxB.Value = "Green"
            End Select
        Case 2 'Option B
            Select Case Me.txtboxA.Value
                Case 1, 3, 4, 10, 12
                    Me.txtBoxB.Value = "Butterfly"
                Case 2, 5, 7
                    Me.txtBoxB.Value = "Caterpie"
            End Select
        End Select

End Sub


Answered By - Warcupine
Answer Checked By - Pedro (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing