how to do math functions on data in a form before saving to SQL 20

G

Guest

In the code below I’m trying to figure out how to dynamically perform math
functions in a form. To start, I would like to subtract
TxtITotal.Text from TxtPTotal.Text and display the results in
TxtProLoss.Text before sending the data to the database. I have no
idea where to begin.

Any help would be greatly appreciated,

Thank you,

Mark


Public Class Form1
Dim StrServer As String = "servername"
Dim strUserName As String = "username"
Dim strPassword As String = "password"
Dim strDB As String = "ABC"

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim Conn As New ADODB.Connection

Conn.Open("Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist
Security Info=False;User ID=" & strUserName & _
";password=" & strPassword & ";Initial Catalog=" & strDB &
";Data Source=" & StrServer)

Dim strSql As String

strSql = "INSERT INTO SP_Trading_1 (acct, sSymbol, tDateS, tDateB,
contracts, strike, sValue, bValue, iTotal, pTotal, Pl) VALUES (" & _
CDbl("0" & TxtAcc.Text) & ",'" & sSymbol.Text & "','" &
DTSDate.Text & "','" & DTBDate.Text & "'," & CDbl("0" & TxtContract.Text) & _
"," & CDbl("0" & TxtStrike.Text) & "," & CDbl("0" &
TxtSellV.Text) & "," & CDbl("0" & TxtbVal.Text) & "," & _
CDbl("0" & TxtITotal.Text) & "," & CDbl("0" & TxtPTotal.Text) &
"," & CDbl("0" & TxtProLoss.Text) & ")"

Conn.Execute(strSql)
Conn.Close()
Conn = Nothing

End Sub

Private Sub TxtAcc_TextChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles TxtAcc.TextChanged

End Sub

Private Sub DTSDate_TextChanged(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles DTSDate.TextChanged

End Sub

Private Sub DTBDate_TextChanged(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles DTBDate.TextChanged

End Sub

Private Sub TxtContract_TextChanged(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles TxtContract.TextChanged

End Sub

Private Sub TxtStrike_TextChanged(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles TxtStrike.TextChanged

End Sub

Private Sub TxtSellV_TextChanged(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles TxtSellV.TextChanged

End Sub

Private Sub TxtbVal_TextChanged(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles TxtbVal.TextChanged

End Sub

Private Sub TxtITotal_TextChanged(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles TxtITotal.TextChanged

End Sub

Private Sub TxtPTotal_TextChanged(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles TxtPTotal.TextChanged

End Sub

Private Sub TxtProLoss_TextChanged(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles TxtProLoss.TextChanged

End Sub

Private Sub sSymbol_TextChanged(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles sSymbol.TextChanged

End Sub
End Class
 
C

Chris

In the code below I’m trying to figure out how to dynamically perform math
functions in a form. To start, I would like to subtract
TxtITotal.Text from TxtPTotal.Text and display the results in
TxtProLoss.Text before sending the data to the database. I have no
idea where to begin.

Any help would be greatly appreciated,

Thank you,

Mark


Public Class Form1
Dim StrServer As String = "servername"
Dim strUserName As String = "username"
Dim strPassword As String = "password"
Dim strDB As String = "ABC"

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim Conn As New ADODB.Connection

Conn.Open("Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist
Security Info=False;User ID=" & strUserName & _
";password=" & strPassword & ";Initial Catalog=" & strDB &
";Data Source=" & StrServer)

Dim strSql As String

strSql = "INSERT INTO SP_Trading_1 (acct, sSymbol, tDateS, tDateB,
contracts, strike, sValue, bValue, iTotal, pTotal, Pl) VALUES (" & _
CDbl("0" & TxtAcc.Text) & ",'" & sSymbol.Text & "','" &
DTSDate.Text & "','" & DTBDate.Text & "'," & CDbl("0" & TxtContract.Text) & _
"," & CDbl("0" & TxtStrike.Text) & "," & CDbl("0" &
TxtSellV.Text) & "," & CDbl("0" & TxtbVal.Text) & "," & _
CDbl("0" & TxtITotal.Text) & "," & CDbl("0" & TxtPTotal.Text) &
"," & CDbl("0" & TxtProLoss.Text) & ")"

Conn.Execute(strSql)
Conn.Close()
Conn = Nothing

End Sub

Private Sub TxtAcc_TextChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles TxtAcc.TextChanged

End Sub

Private Sub DTSDate_TextChanged(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles DTSDate.TextChanged

End Sub

Private Sub DTBDate_TextChanged(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles DTBDate.TextChanged

End Sub

Private Sub TxtContract_TextChanged(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles TxtContract.TextChanged

End Sub

Private Sub TxtStrike_TextChanged(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles TxtStrike.TextChanged

End Sub

Private Sub TxtSellV_TextChanged(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles TxtSellV.TextChanged

End Sub

Private Sub TxtbVal_TextChanged(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles TxtbVal.TextChanged

End Sub

Private Sub TxtITotal_TextChanged(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles TxtITotal.TextChanged

End Sub

Private Sub TxtPTotal_TextChanged(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles TxtPTotal.TextChanged

End Sub

Private Sub TxtProLoss_TextChanged(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles TxtProLoss.TextChanged

End Sub

Private Sub sSymbol_TextChanged(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles sSymbol.TextChanged

End Sub
End Class

When do you want the form to update?


TxtProLoss.Text = cstr(cint(TxtITotal.Text ) - cint(TxtPTotal.Text))

That will do that calc. if you want to do it after one of those two
textfield changes do it in the textchanged functions you have listed in
your code.

Chris
 
G

Guest

Awesome!

I entered the code in the textfield change for TxtPTotal

Private Sub TxtPTotal_TextChanged(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles TxtPTotal.TextChanged
TxtProLoss.Text = CStr(CInt(TxtITotal.Text) - CInt(TxtPTotal.Text))
End Sub

The code does calculate after entering the value in TxtPTotal text box, but
what if I need to change the value in the TxtITotal text box?
How do I code this section to allow changes to either box?

Thanks,

Mark
 
P

Peter Huang [MSFT]

Hi

I think you may try to handle the TxtITotal's TextChanged event too.
Private Sub TxtITotal_TextChanged(ByVal sender As System.Object, ByVal
e
As System.EventArgs) Handles TxtPTotal.TextChanged
TxtProLoss.Text = CStr(CInt(TxtITotal.Text) - CInt(TxtPTotal.Text))
End Sub

so that either TxtITotal.Text or TxtPTotal.Text changed the TxtProLoss.Text
will be recalculated.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
P

Peter Huang [MSFT]

Hi Mark,

Did my suggestion help you?
If you still have any concern, please feel free to post here.
Thanks!

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top