Hi i'm developing an app where i'm reading a value from a device and putting it into a mysql db, i have setup a function where the vb will calculate whether the new value being read has changed by 3% from the last value and if it has run the mysql query. What i need to do it read the value into a variable, then a new value and compare the two with the % function, how do i read a value and maybe add a delay before reading the new value (and it being overwritten) and doing the calculation?
Here is my current code:
Imports System.Data.SqlClient
Imports System.Data
Imports MySql.Data.MySqlClient
Public Class MainForm
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
Label1.Text = EthernetIPforSLCMicro1.ReadAny("F16:0")
Label2.Text = EthernetIPforSLCMicro1.ReadAny("F16:2")
Label13.Text = EthernetIPforSLCMicro1.ReadAny("F15:0")
Label14.Text = EthernetIPforSLCMicro1.ReadAny("F15:1")
Label15.Text = EthernetIPforSLCMicro1.ReadAny("F15:2")
Label16.Text = EthernetIPforSLCMicro1.ReadAny("F15:3")
Label17.Text = EthernetIPforSLCMicro1.ReadAny("B13/16")
Label18.Text = EthernetIPforSLCMicro1.ReadAny("B13/17")
Label19.Text = EthernetIPforSLCMicro1.ReadAny("B13/18")
Label20.Text = EthernetIPforSLCMicro1.ReadAny("B13/19")
End Sub
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If PercentIncreaseF16_0() = True Then
Dim conn As New Odbc.OdbcConnection
Dim connStr As String
connStr = ("server=xxxx; user id=xxxx; password=xxxx; database=xxxx; pooling=false")
'Declare inside of class >
Dim SQLStr As String
'SQL Staments
'SQL query = myQuery = "SQL Statment"
SQLStr = "INSERT into xxxxx(id, f16_0, t_stamp) VALUES(' ','" & Label1.Text & "','" & Now() & "')"
'Write to SQL
Dim MySQLConn As New MySqlConnection() 'The SQL Connection
Dim MySQLCmd As New MySqlCommand() 'The SQL Command
MySQLConn.ConnectionString = connStr 'Set the Connection String
MySQLConn.Open() 'Open the connection
MySQLCmd.Connection = MySQLConn 'Sets the Connection to use with the SQL Command
MySQLCmd.CommandText = SQLStr 'Sets the SQL String
MySQLCmd.ExecuteNonQuery() 'Executes SQL Commands Non-Querys only
MySQLConn.Close() 'Close the connection
End If
If PercentIncreaseF16_2() = True Then
Dim conn As New Odbc.OdbcConnection
Dim connStr As String
connStr = ("server=xxxxx; user id=xxxxx; password=xxxxx; database=xxxxx; pooling=false")
'Declare inside of class >
Dim SQLStr As String
'SQL Staments
'SQL query = myQuery = "SQL Statment"
SQLStr = "INSERT into xxxxx(id, f16_2, t_stamp) VALUES(' ','" & Label2.Text & "','" & Now() & "')"
'Write to SQL
Dim MySQLConn As New MySqlConnection() 'The SQL Connection
Dim MySQLCmd As New MySqlCommand() 'The SQL Command
MySQLConn.ConnectionString = connStr 'Set the Connection String
MySQLConn.Open() 'Open the connection
MySQLCmd.Connection = MySQLConn 'Sets the Connection to use with the SQL Command
MySQLCmd.CommandText = SQLStr 'Sets the SQL String
MySQLCmd.ExecuteNonQuery() 'Executes SQL Commands Non-Querys only
MySQLConn.Close() 'Close the connection
End If
End Sub
Private Function PercentIncreaseF16_0() As Boolean
Dim calc As Integer
Dim top As Integer
Dim bottom As Integer
top = 3
bottom = -3
calc = ((Label1.Text - Label42.Text) / Label42.Text * 100)
If (calc > bottom) And (calc < top) Then
Return False
Else
Return True
End If
End Function
Private Function PercentIncreaseF16_2() As Boolean
Dim calc As Integer
Dim top As Integer
Dim bottom As Integer
top = 3
bottom = -3
calc = ((Label2.Text - Label41.Text) / Label42.Text * 100)
If (calc > bottom) And (calc < top) Then
Return False
Else
Return True
End If
End Function
Private Function PercentIncreaseF15_0() As Boolean
Dim calc As Integer
Dim top As Integer
Dim bottom As Integer
top = 3
bottom = -3
calc = ((Label13.Text - Label30.Text) / Label42.Text * 100)
If (calc > bottom) And (calc < top) Then
Return False
Else
Return True
End If
End Function
Private Function PercentIncreaseF15_1() As Boolean
Dim calc As Integer
Dim top As Integer
Dim bottom As Integer
top = 3
bottom = -3
calc = ((Label14.Text - Label29.Text) / Label42.Text * 100)
If (calc > bottom) And (calc < top) Then
Return False
Else
Return True
End If
End Function
Private Function PercentIncreaseF15_2() As Boolean
Dim calc As Integer
Dim top As Integer
Dim bottom As Integer
top = 3
bottom = -3
calc = ((Label15.Text - Label28.Text) / Label42.Text * 100)
If (calc > bottom) And (calc < top) Then
Return False
Else
Return True
End If
End Function
Private Function PercentIncreaseF15_3() As Boolean
Dim calc As Integer
Dim top As Integer
Dim bottom As Integer
top = 3
bottom = -3
calc = ((Label16.Text - Label27.Text) / Label42.Text * 100)
If (calc > bottom) And (calc < top) Then
Return False
Else
Return True
End If
End Function
End Class
So i'm reading the current values into Label objects, then (hopefully) want to read those values into another set of Label's and then run the calculation function you see and then if "function() is true" run sql query?
Does this sound possible or can you suggest a better way to do this?
Thanks