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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to make command buttons multiply, divide...

Status
Not open for further replies.

Tyskie19

Technical User
Feb 13, 2005
14
CA
I want to make a command button, but upon clicking it, it should perform an action (X, /, -, +, etc.) among two text boxes. How can I do this?
 
in the OnClick for your command
(If your text boxes are txtInp and txtOut)
Code:
If IsNumeric(Me.txtInp) Then
    Me.txtOut = Me.txtInp) / 4
Else
    Me.txtOut = "huh"
End If

traingamer
 
How are ya Tyskie19 . . . . .

This is from my notes on a secaniro which has:
[ol][li][blue]TextBox[/blue] on the left named [blue]Dat1[/blue].[/li]
[li]Followed by a [blue]Label[/blue] named [blue]Op[/blue] (holds the operator).[/li]
[li]Followed by a [blue]TextBox[/blue] named [blue]Dat2[/blue].[/li]
[li]Followed by a [blue]Label[/blue] with [blue]Caption[/blue] property set to [blue]"="[/blue].[/li]
[li]Followed by a [blue]TextBox[/blue] named [blue]Ans[/blue].[/li][/ol]
The following code (in the [purple]Click[/purple] event of the button) cycles thru [purple]+ - x /[/purple] on each click. [purple]Validation[/purple] of the two textboxes and [purple]divide by zero[/purple] are taken care of:
Code:
[blue]   Dim prp As Property, errName As String
   Dim C1 As Double, C2 As Double
   Dim Msg As String, Style As Integer, Title As String, DL As String
   
   Set prp = Me!Op.Properties("Caption")
   Style = vbInformation + vbOKOnly
   Title = "Data Entry Error! . . ."
   DL = vbNewLine & vbNewLine
      
   [green]'Validation[/green]
   If Not IsNumeric(Me!Dat1) Then
      errName = "Dat1"
   ElseIf Not IsNumeric(Me!Dat2) Then
      errName = "Dat2"
   ElseIf Me!Dat2 = 0 Then
      errName = "Zero"
   End If
   
   If errName = "" Then
      C1 = CDbl(Me!Dat1) [green]'convert to double percision[/green]
      C2 = CDbl(Me!Dat2) [green]'convert to double percision[/green]
      
      If prp = "+" Then
         prp = "-"
         Me!Ans = C1 - C2
      ElseIf prp = "-" Then
         prp = "x"
         Me!Ans = C1 * C2
      ElseIf prp = "x" Then
         prp = "/"
         Me!Ans = C1 / C2
      Else
         prp = "+"
         Me!Ans = C1 + C2
      End If
   Else
      If errName = "Zero" Then
         Msg = "Can't divide by zero!"
      Else
         Msg = "Data Entry missing or non-numeric!"
      End If
      
      MsgBox Msg, Style, Title
      
      If errName = "zero" Or errName = "Dat2" Then
         Me!Dat2.SetFocus
      Else
         Me!Dat1.SetFocus
      End If
   End If
   
   Set prp = Nothing[/blue]

Calvin.gif
See Ya! . . . . . .
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top