Insert/Update data into tables in SQL Server Express via FTView SE

A few quick comments:

  • The [Dim Rs1 ...] statement can be removed
  • The [Set Conn1 = Nothing] statement inside IF-ENDIF clauses can all be removed, and add one just before the final END statement. This is currently an error:
    • If more than one of the Tag_Save_... or Tag_Updt_... .Values are 1, then when the second one runs, Conn1 will be Nothing and the [Conn1.ConnectionString = "..."] statement will throw an error.
  • I did some more research, and VB/SQL has methods to do parameterized queries, so you don't have to fiddle around with ticks and ampersands. See the example below.
Private Sub Display_Activate()
...
Dim cmd as ADODB.Command
...
IF Tag_....Value THEN
' Open connection
Conn1.ConnectionString = "dsn=..."
Conn1.Open

' Create command object
Set cmd = New ADODB.Command
cmd.ActiveConnection = Conn1

' Assign SQL statement to command object, with 8 parameter tokens (?s), which tokens will be replaced by parameter values
cmd.CommandText = "Insert into TBL_RCP_SETPOINT (DT_INICIO_RECEBIMENTO, HR_INICIO_RECEBIMENTO, PLACA, COD_COLETA, SILO_DESTINO, NR_LOTE_CAMINHAO, NR_LOTE_SILO, NR_RECEPCAO) values (?,?,?,?,?,?,?,?);"
cmd.CommandType = adCmdText

' Add parameter values; N.B. adDate and adTime may need to be adDbDate and adDbTime
cmd.Parameters.Append cmd.CreateParameter( , adDate, adParamInput, , Date)
cmd.Parameters.Append cmd.CreateParameter( , adTime, adParamInput, , Time)
cmd.Parameters.Append cmd.CreateParameter( , adVarChar, adParamInput, 12, Tag_Placa.Value)
cmd.Parameters.Append cmd.CreateParameter( , adInteger, adParamInput, , Tag_Cod_Coleta.Value)
cmd.Parameters.Append cmd.CreateParameter( , adInteger, adParamInput, , Tag_Silo_Destino.Value)
cmd.Parameters.Append cmd.CreateParameter( , adVarChar, adParamInput, 50, Tag_Nr_Lote_C.Value)
cmd.Parameters.Append cmd.CreateParameter( , adVarChar, adParamInput, 50, Tag_Nr_Lote_S.Value)
cmd.Parameters.Append cmd.CreateParameter( , adInteger, adParamInput, , Tag_Nr_RCP.Value)

' Execute command with parameters replacing ?s in statement
cmd.execute

' Cleanup: delete command; close, but do not delete, connection, so it can be used again
Set cmd = Nothing
Conn1.Close
END IF

' Delete connection variable after all the IF-ENDIFs
Set Conn1 = Nothing
END SUB

I only show the INSERT above; for the UPDATES, the command text and parameter assignments would look something like this:
IF ... THEN
...
cmd.CommandText = "UPDATE TBL_RCP_SETPOINT SET TOTAL_LITROS_RECEBIDO=?, DT_FIM_RECEBIMENTO=? WHERE DT_FIM_RECEBIMENTO IS NULL AND NR_RECEPCAO=1;"

cmd.Parameters.Append cmd.CreateParameter(,adDate, adParamInput, , Date)
cmd.Parameters.Append cmd.CreateParameter(, adTime, adParamInput, , Time)
...
END IF
This approach also has the advantages of not being open to SQL injection security attacks, and of being more clear and easier to read, even if it is more verbose


References:

 
Last edited:

Similar Topics

I'm new to ST so go easy. Studio 5000 L33ER PLC I'm trying to take 2 strings, merge them into 1 string. with a space between the 2 strings...
Replies
3
Views
2,247
Hello everyone. Currently I am using Eplan p8 and faced (while creating a panel) with a challenge in finding a device called DIN mounting rail...
Replies
10
Views
3,397
Hello guys, I am working with a CompactLogix, which communicates over Ethernet/IP with a pin marker. Basically, The PLC sends the texts to be...
Replies
6
Views
1,385
Hi, I have some remote IO connected to my S7-317-2 PN/DP system. I have 1 particular profinet remote IO which is not important to the system, and...
Replies
9
Views
2,980
I am using VbScript in Indusoft to insert data into a MySql database. Immediately after inserting a row, I need to know what that row number is...
Replies
24
Views
5,285
Back
Top Bottom