43429members
217967posts

[Imported] [Solved] Importing data into a Data Table from an Excel file

Highlighted
Sisko

[Imported] [Solved] Importing data into a Data Table from an Excel file

>>Message imported from previous forum - Category:Scripts and Tips<<
User: mchartrand, originally posted: 2018-10-19 18:03:08 Id:153
This is a re-posting from the obsoleted (October 2018) "Schneider Electric Telemetry & SCADA" forum.

-------------------------------

**_hardin4019:
[Solved] See code below for working version. Note its a good bit longer than some of the other posts and includes enough comments so people can figure it out and use it again.
I need a little help understand what I'm doing wrong.
Client wants to import a list of names, addresses, phone numbers, etc from an excel file stored in a mapped network drive or locally on the server. They want that list to appear on a mimic in CS.
I have seen examples of using ST to fill in a Data Table, but I'm not having any luck with getting that ST linked to my Excel file and the correct fields in the Excel. The Excel file for now has 3 columns: Name, Address, Phone. My Data Table has the same 3 columns. The updating of the Data table doesn't need to be automated, but would be nice if at least once a day it updated on its own. From there, I am assuming a simple Query of that Data Table can display the info in a list on a mimic._**

_Sub InsertData()
Dim EmptyDB, iRowNo As Integer
Dim Name, PhoneNumber, Address As String
'All of the data being inserted is in Sheet1
With Sheets("Sheet1")
Set ado = CreateObject("ADODB.Connection")
Set adoRS = CreateObject("ADODB.Recordset")
EmptyDB = 1
'Skip the header row with header info
iRowNo = 2
'Put in the DSN name, Username, Password below
ado.Open "DSN=ClearSCADA", "username", "password"
'Show message box if connected to DB.
'If ado.State = 1 Then
' MsgBox "Connected"
'End If
'Delete all of the data in the table if the EmptyDB equals 1
If EmptyDB = 1 Then
ado.Execute "DELETE FROM PhoneList"
End If
'Loop until empty row
Do Until .Cells(iRowNo, 1) = "" And .Cells(iRowNo, 2) = "" And .Cells(iRowNo, 3) = ""
sName = .Cells(iRowNo, 1)
sPhoneNumber = .Cells(iRowNo, 2)
sAddress = .Cells(iRowNo, 3)
'Build the query
queryText = "INSERT INTO PhoneList (Name, PhoneNumber, Address) VALUES ('" & sName & "', '" & sPhoneNumber & "', '" & sAddress & "')"
'Execute the query
ado.Execute queryText
'Increment iRowNo and repeat until empty row
iRowNo = iRowNo + 1
Loop
'Close DB connection and message done
ado.Close
Set ado = Nothing
MsgBox "Done"
End With
End Sub_

-------------------------------

bevanweiss:
This *might* be possible using a linked table...
There is an ODBC driver for Excel workbooks.

Failing that, you could create a Data Grid that allows easy copy/paste from Excel directly into it. Then display the Data Grid on the mimic (via an embedded query).

I'll check on the linked table idea when I get into the office..

_________

AWoodland:
I don't believe Excel will work, the Windows permissions will probably block ClearSCADA creating the ODBC connection as its a file rather than a service like MSSQL.

One way could be some simple VB running as a user under Windows Task Scheduler, bit of VBS to read Excel via COM and push it into ClearSCADA via ODBC or COM

__________________

du5tin:
We managed to do this in Excel using the ADODB object to create an ODBC connection to ClearSCADA. The VBA code followed this general form:

_Sub InsertData
Set ado = CreateObject("ADODB.Connection")
Set adoRS = CreateObject("ADODB.Recordset")
ado.Open "DSN=ClearSCADA", username, password
For i = 1 To rowcount
queryText = Build your insert query with data from spreadsheet rows here
ado.Execute queryText
Next
MsgBox "Done"
End Sub_

Notes:

-Your DSN using the ClearSCADA must be configured in the 32-bit ODBC Control Panel. User or System tab likely won't matter, we used the system tab so the DSN was available to multiple users.

- We had to write another bit of logic to delete everything from the datatable for future bulk loads. This script had no logic for modifying existing data. Essentially the query text was "DELETE FROM _DataTableName_".

____________________________________________

du5tin:
On reading the post again I realize that manually loading the data into ClearSCADA via Excel was not Shawn's original intent.

Bevan and Adam have good suggestions if you want to reference some network file somewhere as a bit of a 'live' data source instead.

You might be just as quick to build an interface to manage this data inside ClearSCADA without an external file. There are lots of ways an external file could break: If the client changes the file format (renames or adds a column) or the file gets renamed or deleted you might be reworking things inside ClearSCADA to get that data displayed again.

If all the name and phone number information is attached to users already in ClearSCADA you could just query ClearSCADA to display the info too. If the info changes I would expect the users in CLearSCADA to stay fairly up to date (for alarm redirections, etc.) so it should be a reliable source.

________________

**_hardin4019:
Spending a little time working through the suggestion from Dustin. Below is what I have, and I am getting an error message about "Access Denied"._**

_Sub InsertData()
Dim EmptyDB As Integer
Set ado = CreateObject("ADODB.Connection")
Set adoRS = CreateObject("ADODB.Recordset")
EmptyDB = 0
ado.Open "DSN=ClearSCADA", Training, 123456789123#
If ado.State = 1 Then
MsgBox "Connected"
End If
If EmptyDB = 1 Then
ado.Execute "DELETE FROM PhoneList2"
End If
queryText = "insert into PhoneList2 (Name, PhoneNumber, Address) values ('" & A$ & "', '" & B$ & "', '" & C$ & "')"
ado.Execute queryText
ado.Close
Set ado = Nothing
MsgBox "Done"
End Sub_

______________________________

du5tin:
I guess check to see if:

1. Your DSN is setup in both 32-bit and 64-bit ODBC control panels (I use the same name for both so it can be found no matter which architecture the ODBC client is)

2. Enclose your username and password in quotes (they are strings)

3. Check that you can log on using the user in ViewX (sounds silly, but sometimes the user is disabled or locked out).

__________________________________

**_I have both 32 bit and 64 bit ODBC connections setup. Using a username and password I am logging in with. I thought maybe it was happening because "logon as a service" wasn't enabled. But now after putting quotes around the username and password, I am getting the script to run, but still not seeing data in the table._**

__________

bevanweiss:
Where are you getting this error? (at which line)

I assume that it's ado.Open.
The syntax there looks a bit unusual, I guess I'm more used to putting the username and password within the ODBC / OLEDB connection string itself.

I'd generally do such development within Excel and VBA, then you can just migrate it into VBScript for ViewX/WebX pretty easily (just pulling out type definitions and a few other small items).


____________________

**_hardin4019:
I was getting "Access Denied" at row ado.Execute queryText. Now I put quotes around the username and password and I am no longer getting access denied, but nothing is being added to the table. So One step closer, but not quite there yet._**

_________________________

**_hardin4019:
[Solved] See code below for working version. Note its a good bit longer than some of the other posts and includes enough comments so people can figure it out and use it again._**

_Sub InsertData()
Dim EmptyDB, iRowNo As Integer
Dim Name, PhoneNumber, Address As String
'All of the data being inserted is in Sheet1
With Sheets("Sheet1")
Set ado = CreateObject("ADODB.Connection")
Set adoRS = CreateObject("ADODB.Recordset")
EmptyDB = 1
'Skip the header row with header info
iRowNo = 2
'Put in the DSN name, Username, Password below
ado.Open "DSN=ClearSCADA", "username", "password"
'Show message box if connected to DB.
'If ado.State = 1 Then
' MsgBox "Connected"
'End If
'Delete all of the data in the table if the EmptyDB equals 1
If EmptyDB = 1 Then
ado.Execute "DELETE FROM PhoneList"
End If
'Loop until empty row
Do Until .Cells(iRowNo, 1) = "" And .Cells(iRowNo, 2) = "" And .Cells(iRowNo, 3) = ""
sName = .Cells(iRowNo, 1)
sPhoneNumber = .Cells(iRowNo, 2)
sAddress = .Cells(iRowNo, 3)
'Build the query
queryText = "INSERT INTO PhoneList (Name, PhoneNumber, Address) VALUES ('" & sName & "', '" & sPhoneNumber & "', '" & sAddress & "')"
'Execute the query
ado.Execute queryText
'Increment iRowNo and repeat until empty row
iRowNo = iRowNo + 1
Loop
'Close DB connection and message done
ado.Close
Set ado = Nothing
MsgBox "Done"
End With
End Sub_