DB work orders to PLC

If it helps, The VBA code below is taken from Excel but it connects to a Database via ODBC and executes a stored procedure that returns a recordset.

Code:
Sub Fetch_TimeSeries_By_LocalID(myLocalID As Long)
Dim con As ADODB.Connection
Dim cmd As ADODB.Command
Dim rs As ADODB.Recordset
Dim par As String
Dim WSP1 As Worksheet
Set con = New ADODB.Connection
Set cmd = New ADODB.Command
Set rs = New ADODB.Recordset
Dim mySheet, myFileName, strTemp As String

On Error GoTo ErrHandler

If myConfig.DSN = "" Then
    ConfigOK = Read_Config
End If


myFileName = ActiveWorkbook.Name
mySheet = ActiveSheet.Name

Application.DisplayStatusBar = True
Application.StatusBar = "Contacting SQL Server..."

Set WSP1 = Worksheets("Time Series Data")
WSP1.Activate
' Remove any values in the cells where we want to put our Stored Procedure's results.
Dim rngRange As Range
Set rngRange = Range(Cells(7, 2), Cells(Rows.Count, 1)).EntireRow
rngRange.ClearContents

' Log into our SQL Server, and run the Stored Procedure

con.Open myConfig.DSN, myConfig.UName, myConfig.PWord
cmd.ActiveConnection = con

Dim prmLocalID As ADODB.Parameter
'Dim myLocalID As Integer

'myLocalID = CInt(Range("C2").Value)

' Set up the parameter for our Stored Procedure
' (Parameter types can be adVarChar,adDate,adInteger)

cmd.Parameters.Append cmd.CreateParameter("myLocalID", adBigInt, adParamInput, 10, myLocalID)
Application.StatusBar = "Running stored procedure..."
cmd.CommandText = "Return_TimeData_By_LocalID"
Set rs = cmd.Execute(, , adCmdStoredProc)

' Copy the results to cell B7 on the first Worksheet

If rs.EOF = False Then WSP1.Cells(8, 2).CopyFromRecordset rs

rs.Close
Set rs = Nothing
Set cmd = Nothing

con.Close
Set con = Nothing

Set WSP1 = Nothing
Workbooks(myFileName).Worksheets(mySheet).Activate

Application.StatusBar = "Data successfully updated."
Exit Sub
ErrHandler:

'rs.Close
Set rs = Nothing
Set cmd = Nothing

'con.Close
Set con = Nothing
  Workbooks(myFileName).Worksheets(mySheet).Activate
  Application.StatusBar = "VBSub:Fetch_TimeSeries_By_LocalID() error " & Err.Number & Err.Description
  Err.Clear

End Sub
 
If it helps, The VBA code below is taken from Excel but it connects to a Database via ODBC and executes a stored procedure that returns a recordset.

Code:
Sub Fetch_TimeSeries_By_LocalID(myLocalID As Long)
Dim con As ADODB.Connection
Dim cmd As ADODB.Command
Dim rs As ADODB.Recordset
Dim par As String
Dim WSP1 As Worksheet
Set con = New ADODB.Connection
Set cmd = New ADODB.Command
Set rs = New ADODB.Recordset
Dim mySheet, myFileName, strTemp As String

On Error GoTo ErrHandler

If myConfig.DSN = "" Then
    ConfigOK = Read_Config
End If


myFileName = ActiveWorkbook.Name
mySheet = ActiveSheet.Name

Application.DisplayStatusBar = True
Application.StatusBar = "Contacting SQL Server..."

Set WSP1 = Worksheets("Time Series Data")
WSP1.Activate
' Remove any values in the cells where we want to put our Stored Procedure's results.
Dim rngRange As Range
Set rngRange = Range(Cells(7, 2), Cells(Rows.Count, 1)).EntireRow
rngRange.ClearContents

' Log into our SQL Server, and run the Stored Procedure

con.Open myConfig.DSN, myConfig.UName, myConfig.PWord
cmd.ActiveConnection = con

Dim prmLocalID As ADODB.Parameter
'Dim myLocalID As Integer

'myLocalID = CInt(Range("C2").Value)

' Set up the parameter for our Stored Procedure
' (Parameter types can be adVarChar,adDate,adInteger)

cmd.Parameters.Append cmd.CreateParameter("myLocalID", adBigInt, adParamInput, 10, myLocalID)
Application.StatusBar = "Running stored procedure..."
cmd.CommandText = "Return_TimeData_By_LocalID"
Set rs = cmd.Execute(, , adCmdStoredProc)

' Copy the results to cell B7 on the first Worksheet

If rs.EOF = False Then WSP1.Cells(8, 2).CopyFromRecordset rs

rs.Close
Set rs = Nothing
Set cmd = Nothing

con.Close
Set con = Nothing

Set WSP1 = Nothing
Workbooks(myFileName).Worksheets(mySheet).Activate

Application.StatusBar = "Data successfully updated."
Exit Sub
ErrHandler:

'rs.Close
Set rs = Nothing
Set cmd = Nothing

'con.Close
Set con = Nothing
  Workbooks(myFileName).Worksheets(mySheet).Activate
  Application.StatusBar = "VBSub:Fetch_TimeSeries_By_LocalID() error " & Err.Number & Err.Description
  Err.Clear

End Sub

thanks for helping,

i will try to write from DB to excel .csv file and then import to plc.

is that possible to do?

im kinda lost in your code
 
I think you will need WinCCAdvanced for this project or another SCADA (Ignition?) to make the connection to the database via scripting and extract data from it. I've never done it directly in the SCADA so I'm unsure whether WinCC Advanced can do it but I'd be surprised if it can't. Many people suggest that it would be easier in Ignition.

The other option is to get the database to export the data as a text file in a defined format but you will still need to use scripting to read the file and extract the data.

It sounds to me like you need two stored procedures in the database: 1) List available orders 2) return data based on order number. Your program must first query the database to get a list of the available orders and then, using the order number, select the relevant data. You might also need a third stored procedure to mark a job number as complete.

Good luck,

Nick
 
I think you will need WinCCAdvanced for this project or another SCADA (Ignition?) to make the connection to the database via scripting and extract data from it. I've never done it directly in the SCADA so I'm unsure whether WinCC Advanced can do it but I'd be surprised if it can't. Many people suggest that it would be easier in Ignition.

The other option is to get the database to export the data as a text file in a defined format but you will still need to use scripting to read the file and extract the data.

It sounds to me like you need two stored procedures in the database: 1) List available orders 2) return data based on order number. Your program must first query the database to get a list of the available orders and then, using the order number, select the relevant data. You might also need a third stored procedure to mark a job number as complete.

Good luck,

Nick

i have a plan to buy wincc advanced because ignition seems a bit expensive for that kind of project.

anyone did such thing with wincc advanced?

thanks
 
I have done database reporting and recipes with WinCC Advanced and MS SQL Server 2014. Also data management with other SCADA software.

Basically, WinCC has to be looking for a new work order. If every minute is fast enough, use the Scheduler. If not a PLC timer is needed to execute your script.

Something like... SELECT TOP 1 <columns> ... FROM <columns> WHERE job_complete = 0 ORDER BY date ASC

Then when you finish: UPDATE <table> SET <other process variables>, job_complete = 1 WHERE order_id = <current work order id>
 
I have used FTTM but it isn't for the feint hearted. I actually think it's a great product, just difficult to set up.

Nick

I set up a large scale recipe system using FTTM and SQL for one of the F500 baking companies. It was a bit of a learning curve, but it's a powerful program once you figure it all out, and they were thrilled with the result. I use it whenever I can now for data collection/recall.
 
I have only used FTTM twice for event triggered data logging and it works very well. The Excel VBA code I posted previously comes from a spreadsheet that retrieves the logged data from SQL server for a specific batch.

Could you make FTTM work with a Siemens PLC? Maybe via an OPC Server?

Nick
 
I have done database reporting and recipes with WinCC Advanced and MS SQL Server 2014. Also data management with other SCADA software.

Basically, WinCC has to be looking for a new work order. If every minute is fast enough, use the Scheduler. If not a PLC timer is needed to execute your script.

Something like... SELECT TOP 1 <columns> ... FROM <columns> WHERE job_complete = 0 ORDER BY date ASC

Then when you finish: UPDATE <table> SET <other process variables>, job_complete = 1 WHERE order_id = <current work order id>

have you done something like that? just an example:

"Dim sqlConnection1 As New SqlConnection("Your Connection String")
Dim cmd As New SqlCommand
Dim reader As SqlDataReader

cmd.CommandText = "SELECT * FROM Customers"
cmd.CommandType = CommandType.Text
cmd.Connection = sqlConnection1

sqlConnection1.Open()

reader = cmd.ExecuteReader()
' Data is accessible through the DataReader object here.

sqlConnection1.Close()
"
 

Similar Topics

Question: How can I store order data in PLC (Siemens S7-300) from HMI (Siemens TP177B)? What is the best way to do this? I am stuck at inserting...
Replies
3
Views
1,995
Hi! Is it possible to have a hardware list with the order numbers of a TIA v12 project? I would like to receive a quotation for a list of...
Replies
0
Views
1,299
I have been asked by a company to look into electronic data recorders. I have done some research, but it is hard to find the combination they are...
Replies
7
Views
3,028
Apologies for not being the best IDEC programmer. I recently was doing some inspections on a site that had 3 FC6A IDEC processors. The issue is...
Replies
0
Views
13
"Hello! Good day! Excuse me, I have a question regarding the 1761-NET-ENI. RSLinx has already detected it but it's not connecting to the PLC...
Replies
1
Views
21
Back
Top Bottom