Sub CmdCalculate_Click ()
Dim ToDos As ToDosBuffer
Dim Length As Long
Dim ReturnValue As Double

' make sure exactly three of the fields have values
' set Code to indicate the empty field

    FieldCount = 0
    If TxtPrinciple.Text <> "" Then
        FieldCount = FieldCount + 1
    Else
        ToDos.Code = 1
    End If
    If TxtRate.Text <> "" Then
        FieldCount = FieldCount + 1
    Else
        ToDos.Code = 2
    End If
    If TxtTime.Text <> "" Then
        FieldCount = FieldCount + 1
    Else
        ToDos.Code = 3
    End If
    If TxtInterest.Text <> "" Then
        FieldCount = FieldCount + 1
    Else
        ToDos.Code = 4
    End If

    If (FieldCount <> 3) Then
        MsgBox "You Must Specify Exactly Three of the Parameters", MB_OK, "Incorrect Data Entry"
        Exit Sub
    End If

    Select Case ToDos.Code
        Case 1 'calculate principle
            ToDos.Rate = Val(TxtRate.Text)
            ToDos.Time = Val(TxtTime.Text)
            ToDos.Interest = Val(TxtInterest.Text)
        Case 2 'calculate rate
            ToDos.Principle = Val(TxtPrinciple.Text)
            ToDos.Time = Val(TxtTime.Text)
            ToDos.Interest = Val(TxtInterest.Text)
        Case 3 'calculate time
            ToDos.Principle = Val(TxtPrinciple.Text)
            ToDos.Rate = Val(TxtRate.Text)
            ToDos.Interest = Val(TxtInterest.Text)
        Case 4 'calculate interest
            ToDos.Principle = Val(TxtPrinciple.Text)
            ToDos.Time = Val(TxtTime.Text)
            ToDos.Rate = Val(TxtRate.Text)
    End Select

    retcode = SendToDos(DosAp, ToDos, Len(ToDos))
    If (retcode <> 0) Then
        ErrorMessage (retcode)
    Else
        retcode = GetFromDos(DosAp, ReturnValue, Len(ReturnValue), Length)
        If (retcode <> 0) Or (Length > Len(ReturnValue)) Then
            ErrorMessage (retcode)
        Else
            Select Case ToDos.Code
                Case 1 'calculate principle
                    TxtPrinciple.Text = ReturnValue
                Case 2 'calculate rate
                    TxtRate.Text = ReturnValue
                Case 3 'calculate time
                    TxtTime.Text = ReturnValue
                Case 4 'calculate interest
                    TxtInterest.Text = ReturnValue
            End Select
        End If
    End If
End Sub

Sub CmdExit_Click ()
Dim ToDos As ToDosBuffer
    ToDos.Code = -1
    retode = UnloadDosAp(DosAp, ToDos, Len(ToDos))
    If (retcode <> 0) Then
        ErrorMessage (retcode)
    End If
    Unload Form1
End Sub

Sub Form_Load ()
    retcode = LoadDosAp("interest.exe", DosAp, SW_SHOWMINIMIZED, 1024)
    If (retcode <> 0) Then
        ErrorMessage (retcode)
        End
    End If
End Sub

Sub TxtRate_KeyPress (KeyAscii As Integer)
If (KeyAscii = 13) Then
    If (Val(TxtRate.Text) <= 0#) Or (Val(TxtRate.Text) >= 1#) Then
        MsgBox "Rate Must Be a Number Between 0.0 and 1.0", MB_OK, "Bad Rate"
    End If
End If
End Sub

