Results 1 to 13 of 13
  1. #1
    Vita's Avatar
    Vita is offline Competent Performer
    Windows 10 Access 2002
    Join Date
    May 2022
    Location
    Massachusetts, USA
    Posts
    296

    Is there a way to do an if statement based on number Case condition?

    Not sure how to phrase this properly so I am posting an example and hopefully someone understands what I mean.



    Code:
     Case "cboProcess", "lblProcess"
                    .Width = ProcWidth
                    .Left = ProcLeft
                    .Top = IIf(.Name = "cboProcess", top3Ctl, top3Lbl)
    I have a Select statement with lots of controls and I want to adjust the tops accordingly (The above code works as intended).
    Instead of having to change the string inside the IIf every time is there a way I can do something like the following pseudocode?

    Code:
     Case "cboProcess", "lblProcess"
                    .Width = TempNProcWidth
                    .Left = TempProcLeft
                    .Top = IIf(Case.1, top3Ctl, top3Lbl) 'or Case1 or Case(1)
    Thanks!

  2. #2
    davegri's Avatar
    davegri is offline Excess Access
    Windows 11 Access 2019
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,413
    Instead of the iif, you can nest another Select Case to handle which case is true from the outside Select Case.
    Last edited by davegri; 06-29-2023 at 04:35 PM.

  3. #3
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,939
    Perhaps use the Switch() function?
    Might be better having them all in a table and use that?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  4. #4
    Vita's Avatar
    Vita is offline Competent Performer
    Windows 10 Access 2002
    Join Date
    May 2022
    Location
    Massachusetts, USA
    Posts
    296
    Quote Originally Posted by davegri View Post
    Instead of the iif, you can nest another Select Case to handle which case is true from the outside Select Case.
    This sounds kind of what I want? How could I select which case?

    Quote Originally Posted by Welshgasman View Post
    Perhaps use the Switch() function?
    Might be better having them all in a table and use that?
    I don't want to do this because this is just so the form controls will resize as the form gets resized. I want to keep in all contained in the form/vba.
    Switch would provide the same functionality as the IIf it seems but I would still have to type "cboProcess" or whatever control it is in.
    I specifically want to say
    Code:
    Select Case testexpression
    [ Case expressionlist-n [ statements-n ]]
    [ Case Else [ elsestatements ]]
    End Select
    (above copied from select case documentation)
    IIf(Case expressionlist-1, true, false)

    Edit: I don't think its possible to do what I want because it is just an expression and isn't stored in any sort of (system side) array for me to reference.
    More so looking to know if it is or isn't a thing I can do. I did end up using that switch statement for cases that had more than 2 expressions.

  5. #5
    davegri's Avatar
    davegri is offline Excess Access
    Windows 11 Access 2019
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,413
    For the nested Select Case approach I was envisioning something like this: (thing is your existing object name)

    Code:
     Select case thing
    	Case "cboProcess", "lblProcess"
    		.Width = TempNProcWidth
    		.Left = TempProcLeft
    		Select case thing
    			case "cboProcess"
    				.Top =  top3Ctl 'or Case1 or Case(1)
    			case else	
    				.Top =  top3Lbl 
    		end select
    end select

  6. #6
    Vita's Avatar
    Vita is offline Competent Performer
    Windows 10 Access 2002
    Join Date
    May 2022
    Location
    Massachusetts, USA
    Posts
    296
    Quote Originally Posted by davegri View Post
    For the nested Select Case approach I was envisioning something like this: (thing is your existing object name)

    Code:
     Select case thing
        Case "cboProcess", "lblProcess"
            .Width = TempNProcWidth
            .Left = TempProcLeft
            Select case thing
                case "cboProcess"
                    .Top =  top3Ctl 'or Case1 or Case(1)
                case else    
                    .Top =  top3Lbl 
            end select
    end select
    Isn't this basically the same as the IIf I already have?
    Code:
    .Top = IIf(.Name = "cboProcess", top3Ctl, top3Lbl)
                                        Case cbo^   Case else^
    
    
    I understand the Select Case would allow me to do more than 1 case but I am just using the Switch function in that case.
    This wasn't my question though.

    The select case is in a for loop that loops through the controls.
    My case statements have 2 expressions.
    Expression 1 "cboProcess"
    Expression 2 "lblProcess"
    I want to know if inside the select statement there is a way to check if Expression 1 is true regardless of the string name.

    aka a string of code that I could copy to all the cases.

    Here is a larger segment of the code so people can understand what I mean better.
    Code:
                Case "cboProcess", "lblProcess"
                    .Width = TempNProcWidth
                    .Left = TempProcLeft
                    .Top = IIf(.Name = "cboProcess", top3Ctl, top3Lbl)
    
                Case "txtIdMax", "lblIdMax"
                    .Width = MinMaxWidth
                    .Left = LeftEdge
                    .Top = IIf(.Name = "txtIdMax", top3Ctl, top3Lbl)
                    
                Case "txtIdMin", "lblIdMin"
                    .Width = MinMaxWidth
                    .Left = MinLeft
                    .Top = IIf(.Name = "txtIdMin", top3Ctl, top3Lbl)
    See how each IIf I have to type out the 1st expression (cboProcess, txtIdMax, txtIdMin).
    I am looking to see if there is a way that I can just say "If .Name = expression1" as if the case expressions were an array.

    So instead my code could look like this:
    Code:
                Case "cboProcess", "lblProcess"
                    .Width = TempNProcWidth
                    .Left = TempProcLeft
                    .Top = IIf(.Name = Me.Expression1, top3Ctl, top3Lbl)
    
                Case "txtIdMax", "lblIdMax"
                    .Width = MinMaxWidth
                    .Left = LeftEdge
                    .Top = IIf(.Name = Me.Expression1, top3Ctl, top3Lbl)
                    
                Case "txtIdMin", "lblIdMin"
                    .Width = MinMaxWidth
                    .Left = MinLeft
                    .Top = IIf(.Name = Me.Expression1, top3Ctl, top3Lbl)
    P.S. Apologies if any of that came off as rude its not my intention.

  7. #7
    davegri's Avatar
    davegri is offline Excess Access
    Windows 11 Access 2019
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,413
    Need to see more context.
    Need to see code from the "With" and include code that shows the variable that is the object of the Select Case.

    Without those, as a guess, this might work:
    Create a new variable and before the select case, assign it the value of .Name.
    Try using that variable in the IIF

  8. #8
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2013 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    Wouldn't something like this be easier?
    Code:
    Dim ctl as Control,sCtl as string,sLbl as string
    
    
    For each ctl in Me.Controls
        sCtl=ctl.Name
        sLbl=ctl.Properties(3) ' 3=LabelName or you can use ctl.Controls.Item(0).Name to get the associated label's name
        .Width = MinMaxWidth
        .Left = LeftEdge
        .Top = IIf(ctl.ControlType = acLabel,top3Lbl, top3Ctl)
    Next ctl
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  9. #9
    Vita's Avatar
    Vita is offline Competent Performer
    Windows 10 Access 2002
    Join Date
    May 2022
    Location
    Massachusetts, USA
    Posts
    296
    Quote Originally Posted by davegri View Post
    Need to see more context.
    Need to see code from the "With" and include code that shows the variable that is the object of the Select Case.

    Without those, as a guess, this might work:
    Create a new variable and before the select case, assign it the value of .Name.
    Try using that variable in the IIF
    I just used my specific code as an example. I am more trying to ask about a general feature.

    Like if I create a select case with a ton of cases that have multiple expressions, can I reference x expression.

    Here is another example that I am making up on the spot.

    Code:
    'Inventing pseudocode here
    'Me referring to the Select statement
    'Case referring to the Case
    'Expression acting as a property of that case (as if the case was an array since it has multiple expressions)
    'Value would return the value of that expression
    
    
    for i = 0 to 12
    Select Case Number
         Case 1, 2
              If Me.Case.Expression(1) then 'In this example it returns true or false. True if you are currently on the first expression.
                   Debug.print "1"
              end if
         Case 3, 4, 5, 6
              If Me.Case.Expression = 1 then 'This example returns the numerical position of the case expression. 3 = 1, 4 = 2, 5 = 3, 6 = 4
                   debug.print Me.Case.Expression.Value 'Would print 3
              end if
         Case 7, 8
              If Me.Case.Expression = 1 then
                   debug.print Me.Case.Expression.Value 'Would print 7
              end if
         Case 9, 10, 11, 12
              If Me.Case.Expression = 3 then
                   debug.print Me.Case.Expression.Value 'Would print 11
              end if
    end select
    i = i + 1
    loop
    Hopefully this gives a better example. I am trying to ask if there is a way to refer to the cases numerical expression position like in the above.
    I am not looking for an alternative since the way I have it now works just looking to see if it is possible to reference that.

    Quote Originally Posted by Gicu View Post
    Wouldn't something like this be easier?
    Code:
    Dim ctl as Control,sCtl as string,sLbl as string
    
    
    For each ctl in Me.Controls
        sCtl=ctl.Name
        sLbl=ctl.Properties(3) ' 3=LabelName or you can use ctl.Controls.Item(0).Name to get the associated label's name
        .Width = MinMaxWidth
        .Left = LeftEdge
        .Top = IIf(ctl.ControlType = acLabel,top3Lbl, top3Ctl)
    Next ctl
    Cheers,
    I wish it were that simple but the overall goal of this code is to automatically lay out all controls and adjust with resizing.
    Since they have different sizes and different positions I can't just set them all to the same width/left value. I need to individually set their positions and width using percentages of the form which requires a lot of variables as far as I can figure out.
    Some controls use the width and left of another as its starting point so I have lots of variables that are "control2nameLeft = control1nameleft + control1namewidth + 300" so that control 2 appears to the right of control 1 while also scaling with the form.

  10. #10
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    I want to know if inside the select statement there is a way to check if Expression 1 is true regardless of the string name.
    Not sure if this would help but you can use Select Case True and then within, create all sorts of tests that you otherwise could not account for in a single Select Case test. Example,
    Code:
    Select Case True
       Case .Name = "cboProcess
         do something
       Case .Name = "lblProcess"
         do something else
    End Select
    Probably not attractive since I guess you'd have a major re-write on your hands, but I thought the concept worth mentioning. I'm also wondering why not a loop, or if not too many controls, dispense with the Select and make use of the control tag somehow (maybe as a seed value that gets modified). Without seeing more of the pertinent code it's hard to make any focused suggestions. I can't recall if Colin's form resizing would help you as I've never used it, but it frequently gets referred to in here.

    EDIT - didn't find it on his site, except if you enter 'form resize' here, there are document links that pop up
    https://isladogs.co.uk/search/index.html
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  11. #11
    Vita's Avatar
    Vita is offline Competent Performer
    Windows 10 Access 2002
    Join Date
    May 2022
    Location
    Massachusetts, USA
    Posts
    296
    Quote Originally Posted by Micron View Post
    Not sure if this would help but you can use Select Case True and then within, create all sorts of tests that you otherwise could not account for in a single Select Case test. Example,
    Code:
    Select Case True
       Case .Name = "cboProcess
         do something
       Case .Name = "lblProcess"
         do something else
    End Select
    Probably not attractive since I guess you'd have a major re-write on your hands, but I thought the concept worth mentioning. I'm also wondering why not a loop, or if not too many controls, dispense with the Select and make use of the control tag somehow (maybe as a seed value that gets modified). Without seeing more of the pertinent code it's hard to make any focused suggestions. I can't recall if Colin's form resizing would help you as I've never used it, but it frequently gets referred to in here.

    EDIT - didn't find it on his site, except if you enter 'form resize' here, there are document links that pop up
    https://isladogs.co.uk/search/index.html
    Hmmm that is definitely something I will keep on my toolbelt thanks!
    That trick reminds me of the Switch function trick in excel where you do basically the same thing (Switch(true, i =4, do thing, i >4, do thing, i< 4, do thing) etc etc)


    It is all nested in a loop that goes through controls but I have a specific layout I want to achieve and things are different sizes etc. I don't know enough about seed values or how I would incorporate that but it sounds like an interesting idea.
    I found a few form resize tools but none that are open source and I really want to learn how to do it myself which is why I am doing this project.

    I just really want to know if you can reference and expression position.
    I think it would be useful in some code such as this to be able to for instance always refer to the 1st expression or always refer to the 3rd expression.
    It certainly seems like it should be possible.

    I reeaallly want an answer of whether it is possible or not at this point so I can sleep later hahaha sometimes my brain keeps me awake wondering if certain things are possible such as this.

  12. #12
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    By seed value I meant that if there was a starting position of a control, that would be it. The code would calculate the new position based on some factor, but without a complete understanding of what you're working with, it's not possible to be less vague. That is, if the resulting form was going to be 20% larger, then use that in the resizing.

    The only way I can imagine (at present) co-relating the position of an expression would be an array. If your case tests included the position then you could get that position value from the array you've already created. However this would require something along the lines of "cboProcess1", "lblProcess2" which you're not going to do. Perhaps the array association idea would work if you used the tag property:
    Code:
    Case "cboProcess", "lblProcess"
       .Top = myArray(.Tag)
    That would require a static array of control names in the same position as the unique tag value for each control.

    Sorry if I'm distracting from the goal with any of this.

    EDIT -forgot to say that I've tried to discover which value (n) in the expression list causes the statements to be executed but have not succeeded. By that I mean that if one wants to know if it was A or B in "Case A,B" that was the trigger, it doesn't seem possible.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  13. #13
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    have you considered using the tag property

    not seeing the whole code so this is a kind of guess based on your first post, no idea where the variables such as ProcWidth are coming from

    for control cboProcess put in the tag ProcWidth, ProcLeft, top3ctl
    for control lblProcess put in the tag ProcWidth, ProcLeft, top3lbl

    then your code would be something like

    Code:
    for each ctl in me.controls
        with ctl 
            if .tag<>"" then
                 .width=eval(split(.tag,",")(0))
                 .left=eval(split(.tag,",")(1))
                 .top=eval(split(.tag,",")(2))
            end if
        end with
    next ctl
    may need to play around with the eval function to return a value from the string e.g. might be


    Eval(""" & split(.tag,",")(0) & """")

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 17
    Last Post: 07-29-2019, 04:24 AM
  2. CASE STATEMENT or IF-ELSE-THEN? HELP
    By Shakenaw in forum Access
    Replies: 9
    Last Post: 06-08-2015, 11:24 AM
  3. Should I /can I use a Select Case statement?
    By Gina Maylone in forum Access
    Replies: 1
    Last Post: 12-13-2014, 12:08 PM
  4. Case Statement
    By RussH in forum Programming
    Replies: 1
    Last Post: 08-12-2013, 02:50 PM
  5. if or case statement and how
    By Dannat in forum Queries
    Replies: 1
    Last Post: 01-31-2012, 10:35 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums