Creating an MS Access database
This example illustrates the creation of a database from the PLC. In addition, a table is added, and the database that has been generated is declared in the XML configuration file.
Download: TcDBSrv_InfoSysSamples.zip

Database type used | MS Access |
Compatible database types | MS SQL, MS Compact SQL, MS Access, XML |
Function blocks used | FB_DBCreate, FB_DBConnectionAdd, FB_DBTableCreate |
Libraries to be integrated | Tc2_Database, Tc2_System, Tc2_Standard |
Download file list | TcDBSrv_InfoSysSamples.tszip |
A table with the name "myTable", which has the following structure, is added to the generated database:
Column name | Data type | Property |
---|---|---|
ID | Bigint | IDENTITY(1,1) |
Timestamp | datetime |
|
Name | Ntext |
|
Value | Float |
|
This table structure is generated with the following array:
tablestrc: ARRAY [0..3] OF ST_DBColumnCfg :=
[(sColumnName:='ID',sColumnProperty:='IDENTITY(1,1)',eColumnType:=EDBCOLUMN_BIGINT),
(sColumnName:='Timestamp',eColumnType:=EDBCOLUMN_DATETIME),
(sColumnName:='Name',eColumnType:=EDBCOLUMN_NTEXT),
(sColumnName:='Value',eColumnType:=EDBCOLUMN_FLOAT)];
Variable Declaration
PROGRAM MAIN
VAR
R_TRIG1 : R_TRIG;
bSTART : BOOL;
FB_FileDelete1 : FB_FileDelete;
FB_DBCreate1 : FB_DBCreate;
FB_DBConnectionAdd1: FB_DBConnectionAdd;
FB_DBTableCreate1 : FB_DBTableCreate;
bBusy_Delete : BOOL;
bBusy_CreateDB : BOOL;
bBusy_ConnAdd : BOOL;
bBusy_CreateTable : BOOL;
bErr : BOOL;
nErrid : UDINT;
nDBid : UDINT;
arrTablestrc : ARRAY [0..3] OF ST_DBColumnCfg :=
[(sColumnName:='ID',sColumnProperty:='IDENTITY(1,1)',eColumnType:=EDBCOLUMN_BIGINT),
(sColumnName:='Timestamp',eColumnType:=EDBCOLUMN_DATETIME),
(sColumnName:='Name',eColumnType:=EDBCOLUMN_NTEXT),
(sColumnName:='Value',eColumnType:=EDBCOLUMN_FLOAT)];
nState:BYTE := 0;
END_VAR
PLC program
CASE nState OF
0:
(*To start this sample you have to set a rising edge to the variable bSTART*)
R_TRIG1(CLK:=bSTART);
IF R_TRIG1.Q THEN
nState := 1;
FB_FileDelete1(bExecute:=FALSE);
FB_DBCreate1(bExecute:=FALSE);
FB_DBConnectionAdd1(bExecute:=FALSE);
FB_DBTableCreate1(bExecute:=FALSE);
bSTART := FALSE;
END_IF
1:
(*It isn't possible to overwrite an existing database file.
If the database file exist the FB_FileDelete block will delete the file*)
FB_FileDelete1(
sNetId := ,
sPathName:= 'C:\TwinCAT\TcDatabaseSrv\Samples\TestDB1000SPS.mdb',
ePath := PATH_GENERIC,
bExecute := TRUE,
tTimeout := T#5s,
bBusy => bBusy_Delete,
bError => ,
nErrId => );
IF NOT bBusy_Delete THEN
nState := 2;
END_IF
2:
(*The FB_DBCreate block will create the database file
"C:\TwinCAT\TcDatabaseSrv\Samples\TestDB1000SPS.mdb"*)
FB_DBCreate1(
sNetID := ,
sPathName:= 'C:\TwinCAT\TcDatabaseSrv\Samples',
sDBName := 'TestDB1000SPS',
eDBType := eDBType_Access,
bExecute := TRUE,
tTimeout := T#15s,
bBusy => bBusy_CreateDB,
bError => bErr,
nErrID => nErrid);
IF NOT bBusy_CreateDB AND NOT bErr THEN
nState := 3;
END_IF
3:
(*The FB_DBConnectionAdd adds the connection information to the
XML configuration file*)
FB_DBConnectionAdd1(
sNetID := ,
eDBType := eDBType_Access,
eDBValueType:= eDBValue_Double,
sDBServer := ,
sDBProvider := 'Microsoft.Jet.OLEDB.4.0',
sDBUrl := 'C:\TwinCAT\TcDatabaseSrv\Samples\TestDB1000SPS.mdb',
sDBTable := 'myTable',
bExecute := TRUE,
tTimeout := T#15s,
bBusy => bBusy_ConnAdd,
bError => bErr,
nErrID => nErrid,
hDBID => nDBid);
IF NOT bBusy_ConnAdd AND NOT bErr THEN
nState := 4;
END_IF
4:
(*The FB_DBTableCreate create the table "myTable"*)
FB_DBTableCreate1(
sNetID := ,
hDBID := nDBid,
sTableName := 'myTable',
cbTableCfg := SIZEOF(arrTablestrc),
pTableCfg := ADR(arrTablestrc),
bExecute := TRUE,
tTimeout := T#15s,
bBusy => bBusy_CreateTable,
bError => bErr,
nErrID => nErrid);
IF NOT bBusy_CreateTable AND NOT bErr THEN
nState := 0;
END_IF
END_CASE
In order to use this sample, you only need to transfer the NetID of the ADS device (on which the TwinCAT Database Server is installed) to the sNetID input.
Requirements
Development environment |
Target platform |
PLC libraries to be linked |
---|---|---|
TwinCAT v3.0.0 |
PC or CX (x86) |
Tc2_Database |