Reading, writing and deleting records in access database, Please help

vadarv

Member
Join Date
Aug 2006
Location
Norway
Posts
2
Hia!
I'm a total amateur to VBS but need help on a VBS script. This is used in a HMI system from Siemens called WinCC, used for process viewing and control.

What I need to is to write to a table in Access, then read these values and then delete records (rows) in Access.
By using help functions in WinCC I have this set up:

I created an Access database with the WINCC_DATA table and columns (ID,
TagValue) with the ID as the Auto Value.

Then I can write to Access by the following code:

Dim objConnection
Dim strConnectionString
Dim lngValue
Dim strSQL
Dim objCommand
strConnectionString = "Provider=MSDASQL;DSN=SampleDSN;UID=;PWD=;"
lngValue = HMIRuntime.Tags("Tag1").Read
strSQL = "INSERT IGNORE INTO WINCC_DATA (TagValue) VALUES (" & lngValue & ");"
Set objConnection = CreateObject("ADODB.Connection")
objConnection.ConnectionString = strConnectionString
objConnection.Open
Set objCommand = CreateObject("ADODB.Command")
With objCommand
.ActiveConnection = objConnection
.CommandText = strSQL
End With
objCommand.Execute
Set objCommand = Nothing
objConnection.Close
Set objConnection = Nothing

This actually works fine, pure luck I guess!

Can anyone help me with scripts for:
1. Reading one record (row) according to a ID number
2. Deleting the a record (row) according to a ID number

A complete script would of course be the best thing for a "stupid" amateurlike me! But I would be very thankful for any help!

Best regards
Vadar
 
I think all you realy need is an SQL call to do what you want. Sometimes when I want to make something work in SQL I will do it in a query in Access. Then you can change the view in Access to show the SQL commands. Also note that Access SQL commands can be different from other SQL database commands.
 
I'm not sure what namespace, members, etc would be right for your application but the SQL commands you seek should look something like these:

to read a record...
strSQL = "SELECT * FROM WINCC_DATA WHERE ID=" & ID_VALUE_STRING & ";"

to delete a record...
strSQL = "DELETE * FROM WINCC_DATA WHERE ID=" & ID_VALUE_STRING & ";"
 
Dim access_app, db, rec_set

Set access_app = CreateObject("access.application")
access_app.OpenCurrentDatabase "C:\MUVCO1.mdb"
Set db = access_app.CurrentDb
Set rec_set = db.OpenRecordset("batches")

With rec_set

.addnew
.fields("Date")=Date()
.fields("Time")=Time()
.fields("Batch")=BatchNo
.fields("Weigher")=WeigherNo
.fields("Tol")=Tolerence
.fields("Silo")=SiloNo
.fields("Element Code")=Code
.fields("Element Name")=Name
.fields("Prog Wt (KG)")=ProgWt
.fields("Act Wt (KG)")=ActWt
.fields("Diff (KG)")=DiffWt
.update

End With

I use this way to write a record to access.
You must first construct a database in access.
I use queries in access to prepare reports then i print it.
I'm not sure how to read.
 
Dim objConnection
Dim strConnectionString
Dim strSQL
Dim objCommand

strConnectionString = "Provider=MSDASQL;DSN=SampleDSN;UID=;PWD=;"
strSQL = "Delete from WINCC_DATA Where ID=1"
Set objConnection = CreateObject("ADODB.Connection")
objConnection.ConnectionString = strConnectionString
objConnection.Open
Set objCommand = CreateObject("ADODB.Command")
With objCommand
.ActiveConnection = objConnection
.CommandText = strSQL
End With
objCommand.Execute
Set objCommand = Nothing
objConnection.Close
Set objConnection = Nothing


This worked fine!!! I'm a happy man and a bit less amateur ?!
tongue.gif


Thank you for your help and input!!
Great forum!


Cheers
Vadar
 

Similar Topics

Summary: How to remotely toggle BOOL values on a vendor-supplied system having a L24ER PLC? (RSLogix Studio 5000 version 33.01.00) I have a...
Replies
5
Views
1,698
Hello Friends My experience with Siemens is limited. From HMI I would like introduce Minutes and Seconds to a timer. Also, in HMI I would like...
Replies
5
Views
3,923
I am reading and writing to I/O tags in Logix5571 using python. While dissecting using Wireshark, I could see that the Symbol Class(0x6b) is being...
Replies
0
Views
1,329
Hi again guys, This forum has been great for all the questions ive asked these last few days. Another one now.. haha I am currently working...
Replies
3
Views
4,194
Hey guys, i'm controlling altivar 71 drive from modicon m340 cpu 2020 plc through modbus communication, when I read and write at the same time...
Replies
5
Views
2,192
Back
Top Bottom