RE:VBS EN WINCC
Publicado por
fido dido (1 intervención) el 12/11/2010 18:16:49
a ver si te sirve hermano!
Option Explicit
Function action
Dim objConnection
Dim objCommand
Dim objRecordset
Dim strConnectionString
Dim lngValue
Dim lngCount
Dim iSilo, iPro, iSeg, iMin, iHor, S, M, H
Dim iTipo
Dim lRem
Dim rPeso
Dim sHora
Dim sFecha
Dim rHora
Dim rFecha
Dim sSQLCampos, sSQLValores, sSQLText
On Error Resume Next
rPeso= Round(HMIRuntime.Tags("Peso_Har").Read)
iSilo = HMIRuntime.Tags("Silo_Har").Read
If (HMIRuntime.Tags("Ini_Harina").Read = 1) Then
HMIRuntime.Trace "///////////////////////////////////////////////////////////////////////" & vbNewLine
HMIRuntime.Trace "Actualización de Carga de Silo" & vbNewLine
'Carga las variables del HMI
iTipo = HMIRuntime.Tags("Tipo_Har").Read
iPro = HMIRuntime.Tags("Pro_Har").Read
lRem = HMIRuntime.Tags("Rem_Har").Read
rFecha = HMIRuntime.Tags("Fecha_Har").Read
If rFecha <> 0 Then
rFecha = FormatDateTime(rfecha,2)
Else
rFecha = FormatDateTime("01/01/2000",2)
End If
rHora = HMIRuntime.Tags("Hora_Har").Read
rHora = FormatDateTime(rHora,4)
iSeg = (Timer-HMIRuntime.Tags("Tiempo_Har").Read) \ 1
iMin = iSeg \ 60
iHor = iMin \ 60
iSeg = iSeg-iMin * 60
iMin = iMin-iHor * 60
S = "0" & iSeg
M = "0" & iMin
H = "0" & iHor
H=Right(H, Len(H) - Len(H) \ 3)
M=Right(M, Len(M) - Len(M) \ 3)
S=Right(S, Len(S) - Len(S) \ 3)
'Genera la Fecha y Hora en formato datetime de SQL
'sFecha = Date
'sHora = Hour(Time) & ":" & Minute(Time) & ":" & Second(Time)
'Dejo un rastro de los datos utilizados
HMIRuntime.Trace "Tiempo: " & H & ":" & M & ":" & S & vbNewLine
HMIRuntime.Trace "Fecha: " & rFecha & " " & rHora & vbNewLine
HMIRuntime.Trace "Silo: " & iSilo & vbNewLine
HMIRuntime.Trace "Peso: " & rPeso & vbNewLine
HMIRuntime.Trace "Proveedor: " & iPro & vbNewLine
HMIRuntime.Trace "Remito: " & lRem & vbNewLine
Set objConnection = CreateObject("ADODB.Connection")
objConnection.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=fido;Data Source=.\WINCC"
objConnection.CursorLocation = 3
objConnection.Open
Set objRecordset = CreateObject("ADODB.Recordset")
Set objCommand = CreateObject("ADODB.Command")
sSQLCampos = "Silo, Peso, Tipo, Proveedor, Remito, Fecha, Tiempo"
sSQLValores = iSilo & ", " & rPeso & ", " & iTipo & ", " & iPro & ", " & lRem & ", " &_
"CONVERT(datetime, '" & rFecha & " " & rHora & "', 103), CONVERT(nchar, '" & H & ":" & M & ":" & S & "', 103)"
sSQLText = "INSERT INTO Carga_H( " & sSQLCampos & " ) VALUES ( " & sSQLValores & ");"
With objCommand
.ActiveConnection = objConnection
.CommandText = sSQLText
.Execute
End With
If (err.number = 0) Then
HMIRuntime.Trace "El informe de Carga de Materia Prima se ha generado exitosamente" & vbNewLine
Else
HMIRuntime.Trace "Ha ocurrido un error al generar el informe de Carga de Materia Prima" & vbNewLine
HMIRuntime.Trace "El numero del error es: " & err.number & vbNewLine
err.clear
End If
Set objCommand = Nothing
objConnection.Close
Set objRecordset = Nothing
Set objConnection = Nothing
HMIRuntime.Trace "///////////////////////////////////////////////////////////////////////" & vbNewLine
HMIRuntime.Tags("Ini_Harina").Write 0
End If
End Function