Results 1 to 3 of 3
  1. #1
    karmacable is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2011
    Posts
    24

    Better way of coding large 'If / Then' statements

    I have this code which works great. However, it's getting very large to write and was hoping there's a better way to write it. Here's the basic scenario (not the actual code, just the structure for ease of readability);



    Dim W1 As Integer
    Dim W2 As Integer
    W1 = Me.txtFirst
    W2 = Me.txtSecond

    Code:
     
    If      W1 = 0    And   W2 = 0
        
        Me.txtStart  =  ""
        Me.txtEnd    =  ""
     
    ElseIf  W1  <> 0  And  W2  =   0
        
        Me.txtStart  =  Me.txtHi
        Me.txtEnd    =  ""
     
    ElseIf  W1  <> 0  And  W2  <>  0
        
        Me.txtStart  =  Me.txtRed
        Me.txtEnd    =  Me.txtYellow
     
    ElseIf  W1  =   0  And  W2  <>  0
        
        Me.txtStart  =  ""
        Me.txtEnd    =  Me.txtLow
     
    EndIf
    As you can see in this structure, I have two fields (W1,W2) who's every combination (which in this case is four) has a specific output. I am currently using this structure on three fields, which caused me to create 8 possible combinations. I have another piece of code which using this structure will require me to create nine fields! I'm truly hoping there's a better way I currently don't know of (can a 'Select Case' statement work here)?

    Any suggestions are appreciated!

  2. #2
    pdebaets is offline Competent Performer
    Windows Vista Access 2010 (version 14.0)
    Join Date
    Jul 2010
    Location
    Los Angeles
    Posts
    235
    What you have there is readable. I'm not sure there's a problem. You may want to try something like:

    Code:
    If W1 = 0 And W2 = 0 Then Me.txtStart = "":    Me.txtEnd = ""
    ElseIf W1 <> 0 And W2 = 0 Then Me.txtStart = Me.txtHi: Me.txtEnd = ""
    ElseIf W1 <> 0 And W2 <> 0 Then Me.txtStart = Me.txtRed: Me.txtEnd = Me.txtYellow
    ElseIf W1 = 0 And W2 <> 0 Then Me.txtStart = "": Me.txtEnd = Me.txtLow
    End If
    ... or:

    Code:
    Select _
    Case W1 = 0 And W2 = 0: Me.txtStart = "": Me.txtEnd = ""
    Case W1 <> 0 And W2 = 0: Me.txtStart = Me.txtHi: Me.txtEnd = ""
    Case W1 <> 0 And W2 <> 0: Me.txtStart = Me.txtRed: Me.txtEnd = Me.txtYellow
    Case W1 = 0 And W2 <> 0: Me.txtStart = "": Me.txtEnd = Me.txtLow
    End Select

  3. #3
    ssanfu is offline Master of Nothing
    Windows 2K Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    I don't know if this is better, but at least it is not hard coded....

    My idea is to use a table, open a record set and get the values from the controls on the form.

    The attached A2K MDB example has 2 fields V1 & V2 (v= values). This will allow 4 combinations:

    0,0
    0,1
    1,0
    1,1

    If you used 3 fields to do the tests (V1, V2 & V3), you would have 8 records in the table.:
    0,0,0
    0,0,1
    0,1,0
    0,1,1
    1,0,0
    1,0,1
    1,1,0
    1,1,1


    There are 2 other fields in the table, F1 & F2. These are the control names that the final values will be looked up. You could have as many fields as you want.

    I used your names from the code example, so if it is hard to understand....


    I know I'm not explaining this very well, so if you have questions, post back...
    Last edited by ssanfu; 09-15-2011 at 08:20 PM. Reason: edited

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

Similar Threads

  1. Error Coding Problems
    By desk4tbc in forum Programming
    Replies: 7
    Last Post: 07-11-2011, 04:10 PM
  2. Coding question
    By kzoli62 in forum Access
    Replies: 1
    Last Post: 07-04-2011, 05:09 PM
  3. Combo Box Coding
    By Hale in forum Forms
    Replies: 2
    Last Post: 05-04-2010, 10:56 AM
  4. Button Coding
    By yamie in forum Programming
    Replies: 3
    Last Post: 02-22-2010, 10:45 AM
  5. Bar-Coding
    By texasprincess7 in forum Access
    Replies: 1
    Last Post: 02-12-2009, 10:29 AM

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