Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with Function

Status
Not open for further replies.

bjarvis

Technical User
Jan 15, 2001
38
0
0
US
I have some code that will be used with many different combo boxes. The combo boxes will be named cboEquipment0, cboEquipment1, cboEquipment2, etc... I would like to create a function so that I didn't have to write the code for every combo box. Here's my code.

Private Sub cboEquipment0_Click()
'This code will figure the Acres you can cover per hour with the equipment
'selected.

Dim F As Double, S As Integer, W As Integer, EF As Integer

On Error Resume Next

W = InputBox("Enter the width of the equipment.", "Equipment Width")
S = InputBox("Enter the average speed in the field.", "Equipment Speed")
If cboEquipment0 = "Combine" Then
EF = 70
F = (((S * W) * (EF / 100)) / 8.25)
txtEquipment0.Text = F
ElseIf cboEquipment0 = "Disk" Then
EF = 85
F = (((S * W) * (EF / 100)) / 8.25)
txtEquipment0.Text = F
ElseIf cboEquipment0 = "Field Cultivator" Then
EF = 85
F = (((S * W) * (EF / 100)) / 8.25)
txtEquipment0.Text = F
ElseIf cboEquipment0 = "Chisel Plow" Then
EF = 85
F = (((S * W) * (EF / 100)) / 8.25)
txtEquipment0.Text = F
ElseIf cboEquipment0 = "Moldboard Plow" Then
EF = 85
F = (((S * W) * (EF / 100)) / 8.25)
txtEquipment0.Text = F
ElseIf cboEquipment0 = "Row Crop Cultivator" Then
EF = 80
F = (((S * W) * (EF / 100)) / 8.25)
txtEquipment0.Text = F
ElseIf cboEquipment0 = "Drill" Then
EF = 70
F = (((S * W) * (EF / 100)) / 8.25)
txtEquipment0.Text = F
ElseIf cboEquipment0 = "Planter" Then
EF = 65
F = (((S * W) * (EF / 100)) / 8.25)
txtEquipment0.Text = F
ElseIf cboEquipment0 = "Fertilizer Spreader" Then
EF = 70
F = (((S * W) * (EF / 100)) / 8.25)
txtEquipment0.Text = F
ElseIf cboEquipment0 = "Sprayer" Then
EF = 65
F = (((S * W) * (EF / 100)) / 8.25)
txtEquipment0.Text = F
ElseIf cboEquipment0 = " " Then
txtEquipment0.Text = ""
End If
End Sub
 
I would suggest using a select case

Select Case cboEquipment0.text
Case "Combine"
EF = 70
F = (((S * W) * (EF / 100)) / 8.25)
txtEquipment0.Text = F
Case "Disk"
* insert code here*
ETC.....
End Select


Then all you have to change is the Select Case statement at the top to cboEquipment1.text

Hope this helps

Adam
 
If all the code is the same, just the combo boxes are different, you could use a control array of combo boxes and write a separate routine to handle it, and call the routine from within cbo_Click(Index as Integer) routine:

Sub DoCombos(Index as Integer, S as Integer, W as Integer)
Dim F as Double
Dim EF as Integer

'Here's Adam95's suggestion integrated with this:
Select Case cboEquipment(Index).Text
Case "Combine"
'''
Case "Disk"
'''
End Select

End Sub

Now you just call this routine from the array's click() event:

Private Sub cboEquipment_Click(Index as Integer)
Dim W as Integer
Dim S as Integer

W = InputBox("Enter the width of the equipment.", "Equipment Width")
S = InputBox("Enter the average speed in the field.", "Equipment Speed")

'once input has been validated:
DoCombos Index, S, W

End Sub


Hope that helps!

-Mike

Difference between a madman and a genius:
A madman uses his genius destructively,
A genius uses his madness constructively.
 
Control arrays are a must to reduce your solution. Anytime you see a sequential progression of object names associated with identical processing you should immediately think array. In your case, two arrays, one for the combo box the other for the matching text box. The only hard part is making sure teh indexes match up properly. That's not too hard.

The select case statement is a nicer way to code your if then elseif statement. It is still not much better in your case. To reduce this part of your solution you need to really look at it again. It shares a similar trait with your controls. It repeats nearly identical code. A couple variables exist in it that can easly be identified, the basic processing is identical. Maybe an array can help you in that area too.

[tt]
' Initialize the collection of item objects.
MyTable = Array (
Array("SearchVal", ValEF1),
...
Array("")
)
[/tt]
Next, DoCombos serves better as a function returning the value you want transfered. The first parameter should be the actual search value to limit the input to only bare necessities. I mean, what does the a control index have to do with the resulting value. For that matter what does coming from a control have to do with it. Having it present clutters the algorithm with unnecessary labor. With this in mind, the name, it no longer deals with the combo control so the name is bad. We'll keep it just for continuity with the excellent information you got earlier.
[tt]
Function DoCombos(FindThis as String, S, W)
' This function returns F property for the name given
DIM EF

EF = 0 ' default EF
For i = lbound(MyTable) to uBound(MyTable)
If (Mytable(i)(0) = FindThis) Then
EF= MyTable(i)(1)
Exit For
End If
Next i

DoCombos = INSERT WHAT YOU NEED HERE

End Function
[/tt]

A snippit using this might be
[tt]
F = DoCombos(cboEquipment(Index).Text, S, W)
txtEquipment(Index).Text = F
[/tt]

In a for each loop the Combobox looses its index... sort of:
[tt] F = DoCombos(cboEach.Text, S, W) [/tt]
and to get the right Text changed
[tt] txtEquipment(cboEach.Index).Text = F[/tt]

I have to admit: if a different computation is applied to each item, this solution becomes broken and the Select/Case statement is the better choice.


Wil Mead
wmead@optonline.net

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top