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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • markaelkins@newsgroups.nospam

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

    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(B yVal sender As System.Object, ByVal e As
    System.EventArg s) Handles Button1.Click
    Dim Conn As New ADODB.Connectio n

    Conn.Open("Prov ider=SQLOLEDB.1 ;Integrated Security=SSPI;P ersist
    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.Tex t) & _
    "," & 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(st rSql)
    Conn.Close()
    Conn = Nothing

    End Sub

    Private Sub TxtAcc_TextChan ged(ByVal sender As System.Object, ByVal e As
    System.EventArg s) Handles TxtAcc.TextChan ged

    End Sub

    Private Sub DTSDate_TextCha nged(ByVal sender As System.Object, ByVal e
    As System.EventArg s) Handles DTSDate.TextCha nged

    End Sub

    Private Sub DTBDate_TextCha nged(ByVal sender As System.Object, ByVal e
    As System.EventArg s) Handles DTBDate.TextCha nged

    End Sub

    Private Sub TxtContract_Tex tChanged(ByVal sender As System.Object, ByVal
    e As System.EventArg s) Handles TxtContract.Tex tChanged

    End Sub

    Private Sub TxtStrike_TextC hanged(ByVal sender As System.Object, ByVal e
    As System.EventArg s) Handles TxtStrike.TextC hanged

    End Sub

    Private Sub TxtSellV_TextCh anged(ByVal sender As System.Object, ByVal e
    As System.EventArg s) Handles TxtSellV.TextCh anged

    End Sub

    Private Sub TxtbVal_TextCha nged(ByVal sender As System.Object, ByVal e
    As System.EventArg s) Handles TxtbVal.TextCha nged

    End Sub

    Private Sub TxtITotal_TextC hanged(ByVal sender As System.Object, ByVal e
    As System.EventArg s) Handles TxtITotal.TextC hanged

    End Sub

    Private Sub TxtPTotal_TextC hanged(ByVal sender As System.Object, ByVal e
    As System.EventArg s) Handles TxtPTotal.TextC hanged

    End Sub

    Private Sub TxtProLoss_Text Changed(ByVal sender As System.Object, ByVal
    e As System.EventArg s) Handles TxtProLoss.Text Changed

    End Sub

    Private Sub sSymbol_TextCha nged(ByVal sender As System.Object, ByVal e
    As System.EventArg s) Handles sSymbol.TextCha nged

    End Sub
    End Class

  • Chris

    #2
    Re: how to do math functions on data in a form before saving to SQL20

    markaelkins@new sgroups.nospam wrote:[color=blue]
    > 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(B yVal sender As System.Object, ByVal e As
    > System.EventArg s) Handles Button1.Click
    > Dim Conn As New ADODB.Connectio n
    >
    > Conn.Open("Prov ider=SQLOLEDB.1 ;Integrated Security=SSPI;P ersist
    > 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.Tex t) & _
    > "," & 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(st rSql)
    > Conn.Close()
    > Conn = Nothing
    >
    > End Sub
    >
    > Private Sub TxtAcc_TextChan ged(ByVal sender As System.Object, ByVal e As
    > System.EventArg s) Handles TxtAcc.TextChan ged
    >
    > End Sub
    >
    > Private Sub DTSDate_TextCha nged(ByVal sender As System.Object, ByVal e
    > As System.EventArg s) Handles DTSDate.TextCha nged
    >
    > End Sub
    >
    > Private Sub DTBDate_TextCha nged(ByVal sender As System.Object, ByVal e
    > As System.EventArg s) Handles DTBDate.TextCha nged
    >
    > End Sub
    >
    > Private Sub TxtContract_Tex tChanged(ByVal sender As System.Object, ByVal
    > e As System.EventArg s) Handles TxtContract.Tex tChanged
    >
    > End Sub
    >
    > Private Sub TxtStrike_TextC hanged(ByVal sender As System.Object, ByVal e
    > As System.EventArg s) Handles TxtStrike.TextC hanged
    >
    > End Sub
    >
    > Private Sub TxtSellV_TextCh anged(ByVal sender As System.Object, ByVal e
    > As System.EventArg s) Handles TxtSellV.TextCh anged
    >
    > End Sub
    >
    > Private Sub TxtbVal_TextCha nged(ByVal sender As System.Object, ByVal e
    > As System.EventArg s) Handles TxtbVal.TextCha nged
    >
    > End Sub
    >
    > Private Sub TxtITotal_TextC hanged(ByVal sender As System.Object, ByVal e
    > As System.EventArg s) Handles TxtITotal.TextC hanged
    >
    > End Sub
    >
    > Private Sub TxtPTotal_TextC hanged(ByVal sender As System.Object, ByVal e
    > As System.EventArg s) Handles TxtPTotal.TextC hanged
    >
    > End Sub
    >
    > Private Sub TxtProLoss_Text Changed(ByVal sender As System.Object, ByVal
    > e As System.EventArg s) Handles TxtProLoss.Text Changed
    >
    > End Sub
    >
    > Private Sub sSymbol_TextCha nged(ByVal sender As System.Object, ByVal e
    > As System.EventArg s) Handles sSymbol.TextCha nged
    >
    > End Sub
    > End Class
    >[/color]

    When do you want the form to update?


    TxtProLoss.Text = cstr(cint(TxtIT otal.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

    Comment

    • markaelkins@newsgroups.nospam

      #3
      Re: how to do math functions on data in a form before saving to SQ

      Awesome!

      I entered the code in the textfield change for TxtPTotal

      Private Sub TxtPTotal_TextC hanged(ByVal sender As System.Object, ByVal e
      As System.EventArg s) Handles TxtPTotal.TextC hanged
      TxtProLoss.Text = CStr(CInt(TxtIT otal.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


      "Chris" wrote:
      [color=blue]
      > markaelkins@new sgroups.nospam wrote:[color=green]
      > > 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(B yVal sender As System.Object, ByVal e As
      > > System.EventArg s) Handles Button1.Click
      > > Dim Conn As New ADODB.Connectio n
      > >
      > > Conn.Open("Prov ider=SQLOLEDB.1 ;Integrated Security=SSPI;P ersist
      > > 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.Tex t) & _
      > > "," & 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(st rSql)
      > > Conn.Close()
      > > Conn = Nothing
      > >
      > > End Sub
      > >
      > > Private Sub TxtAcc_TextChan ged(ByVal sender As System.Object, ByVal e As
      > > System.EventArg s) Handles TxtAcc.TextChan ged
      > >
      > > End Sub
      > >
      > > Private Sub DTSDate_TextCha nged(ByVal sender As System.Object, ByVal e
      > > As System.EventArg s) Handles DTSDate.TextCha nged
      > >
      > > End Sub
      > >
      > > Private Sub DTBDate_TextCha nged(ByVal sender As System.Object, ByVal e
      > > As System.EventArg s) Handles DTBDate.TextCha nged
      > >
      > > End Sub
      > >
      > > Private Sub TxtContract_Tex tChanged(ByVal sender As System.Object, ByVal
      > > e As System.EventArg s) Handles TxtContract.Tex tChanged
      > >
      > > End Sub
      > >
      > > Private Sub TxtStrike_TextC hanged(ByVal sender As System.Object, ByVal e
      > > As System.EventArg s) Handles TxtStrike.TextC hanged
      > >
      > > End Sub
      > >
      > > Private Sub TxtSellV_TextCh anged(ByVal sender As System.Object, ByVal e
      > > As System.EventArg s) Handles TxtSellV.TextCh anged
      > >
      > > End Sub
      > >
      > > Private Sub TxtbVal_TextCha nged(ByVal sender As System.Object, ByVal e
      > > As System.EventArg s) Handles TxtbVal.TextCha nged
      > >
      > > End Sub
      > >
      > > Private Sub TxtITotal_TextC hanged(ByVal sender As System.Object, ByVal e
      > > As System.EventArg s) Handles TxtITotal.TextC hanged
      > >
      > > End Sub
      > >
      > > Private Sub TxtPTotal_TextC hanged(ByVal sender As System.Object, ByVal e
      > > As System.EventArg s) Handles TxtPTotal.TextC hanged
      > >
      > > End Sub
      > >
      > > Private Sub TxtProLoss_Text Changed(ByVal sender As System.Object, ByVal
      > > e As System.EventArg s) Handles TxtProLoss.Text Changed
      > >
      > > End Sub
      > >
      > > Private Sub sSymbol_TextCha nged(ByVal sender As System.Object, ByVal e
      > > As System.EventArg s) Handles sSymbol.TextCha nged
      > >
      > > End Sub
      > > End Class
      > >[/color]
      >
      > When do you want the form to update?
      >
      >
      > TxtProLoss.Text = cstr(cint(TxtIT otal.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
      >[/color]

      Comment

      • Peter Huang [MSFT]

        #4
        Re: how to do math functions on data in a form before saving to SQ

        Hi

        I think you may try to handle the TxtITotal's TextChanged event too.
        Private Sub TxtITotal_TextC hanged(ByVal sender As System.Object, ByVal
        e
        As System.EventArg s) Handles TxtPTotal.TextC hanged
        TxtProLoss.Text = CStr(CInt(TxtIT otal.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.

        Comment

        • Peter Huang [MSFT]

          #5
          Re: how to do math functions on data in a form before saving to SQ

          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.

          Comment

          Working...