Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Option Buttons, a better way?

Posted on 2002-07-11
10
Medium Priority
?
162 Views
Last Modified: 2012-05-04
Say I have these options;

[Frame1]
Private Sub optA_Click()
Private Sub optB_Click()
[Frame2]
Private Sub optC_Click()
Private Sub optD_Click()

Is there a better way to do this?

Private Sub cmdCalc_Click()

If optA Then
    If optC Then
        intM1 = 2.5
        intM2 = 3.5
    Else
        intM1 = 3.5
        intM2 = 4.5
    End If
ElseIf optB Then
    If optC Then
        intM1 = 3
        intM2 = 4
    Else
        intM1 = 4
        intM2 = 5
    End If
End If

This works, but I think theres a better way...
0
Comment
Question by:mgfranz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 7148484
Try this:

[Form code]
Private mintBase as integer
Private mintOffset as integer

Private Sub Form_Load()
  'to force the good values
  optB.Value = true
  optD.Value = true

  optA.Value = true
  optC.Value = true
End Sub

Private Sub optA_Click()
  mintBase = 2,5
End Sub

Private Sub optB_Click()
  mintBase = 3
End Sub

Private Sub optC_Click()
  mintOffset = 0
End Sub

Private Sub optD_Click()
  mintOffset = 1
End Sub

Private Sub cmdCalc_Click()
  intM1 = mintBase + mintOffset
  intM2 = intM1 + 1
End Sub

CHeers
0
 
LVL 4

Accepted Solution

by:
TigerZhao earned 200 total points
ID: 7148642
Private mvValue1 As Variant
Private mvValue2 As Variant

Private Sub Form_Load()
    mvValue1 = Array(2.5, 3.5, 3, 4)
    mvValue2 = Array(3.5, 4.5, 4, 5)
End Sub

Private Sub cmdCalc_Click()
    Dim I As Long
    I = IIf(optA.Value, 0, 2) + IIf(optC.Value, 0, 1)

    intM1 = mvValue1(I)
    intM2 = mvVAlue2(I)
End Sub
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 7149579
If you'll be expanding this list, throw everything into a database.

The table will be a two-part key like this:

tblMValue
=========
KeyAB (integer)
KeyCD (integer)
MValue1 (double)
MValue2 (double)

Records:
KeyAB  KeyCD  MValue1  MValue2
0 (A)  0 (C)  2.5      3.5
0 (A)  1 (D)  3.5      4.5
1 (B)  0 (C)  3        4
1 (B)  1 (D)  4        5

Query:
SELECT MValue1, MValue2 FROM tblMValue WHERE KeyAB = (#1) AND KeyCD = (#2)

#1 and #2 are values extracted from your option buttons, which could be control arrays that have their index=the table key.

Then:
rstMValue.Open strSQL
if rstMValue.EOF then
   msgbox "couldn't find combination"
else
   intM1 = rstMValue.Fields("MValue1").Value
   intM2 = rstMValue.Fields("MValue2").Value
endif
rstMValue.close
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 18

Author Comment

by:mgfranz
ID: 7149605
Not really a dB issue... but a good suggestion.

Angelll you pretty much had the idea I was trying to work with, but Tiger has what would probably be a more efficient solution.

Let me try them both and do an analysis.
0
 
LVL 18

Author Comment

by:mgfranz
ID: 7150233
OK... Same scenario, different values for M2 if optA or optB selected;

If optA Then
   If optC Then
       intM1 = 2.5
       intM2 = 4
   Else
       intM1 = 3.5
       intM2 = 5
   End If
ElseIf optB Then
   If optC Then
       intM1 = 3
       intM2 = 4
   Else
       intM1 = 4
       intM2 = 5
   End If
End If

So in other words, M2 is going to be either 4 or 5.

This works, but it looks redundant on mvValue2;

Private Sub Form_Load()
   mvValue1 = Array(2.5, 3.5, 3, 4)
   mvValue2 = Array(4, 5, 4, 5)
End Sub

Private Sub cmdCalc_Click()
   Dim I As Long
   I = IIf(optA.Value, 0, 2) + IIf(optC.Value, 0, 1)

   intM1 = mvValue1(I)
   intM2 = mvVAlue2(I)
End Sub

0
 
LVL 22

Expert Comment

by:rspahitz
ID: 7150243
It may seem redundant, but unless you can guarantee that those values will always be the same, it's worth keeping it this way.  The processing time is so minimal that it doesn't hurt to keep it.
0
 
LVL 18

Author Comment

by:mgfranz
ID: 7150252
I'm told they will, and I agree... the footprint for the array is so small anyway.  But just the same, is there a way around it?
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 7150258
One simple way is to modify the two lines to appear as follows:

 mvValue2 = Array(4, 5)


  intM2 = mvVAlue2(I mod 2)
0
 
LVL 18

Author Comment

by:mgfranz
ID: 7150267
This worked excellent.

rspahitz, I'll throw you a bone too.  ;-)
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 7150275
That wasn't necessary, but thanks.

I see it at http://www.experts-exchange.com/visualbasic/Q_20322798.html
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question