Debugging Functions VB- scripts

Debugging Functions YouTube Tutorial

vb action scripts debugging function

Project Informations VB-Action scripts

The records you place in the Project Information region will not be supplanted naturally, which implies you can have all drives referring to a particular adaptation of an archive. It additionally implies you should check consistently to ensure the archives are as yet current.

ย Project Information


Sub OnClick(ByVal Item)ย  ย 

Dim Infos

Dim DSN

Set DSN = HMIRuntime.Tags(โ€œ@DatasourceNameRTโ€)

DSN.Read

Infos = โ€œProjectname: โ€ & HMIRuntime.ActiveProject.Name & vbCrLf & _

โ€œProject Pfad: โ€ & HMIRuntime.ActiveProject.Path & vbCrLf & _

โ€œProject DSN: โ€ & DSN.Valueย 

Dim TextFeld

Set TextFeld = ScreenItems(โ€œStatischerText5โ€)

TextFeld.Text = Infos

Set DSN = Nothing

End Sub


Color Change HMI Objects in picture window VB-Action script

ย Color HMI Objects in the picture window


Sub OnClick(ByVal Item)ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย 

Dim PictureInWindow

Dim HMIObjects

Set PictureInWindow = ScreenItems(โ€œWindowโ€).Screen.ScreenItems โ€˜.ScreenItems โ€˜.ScreenItems(โ€œCircle1โ€)

For Each HMIObjects In PictureInWindow

If HMIObjects.Backcolor = RGB(255,0,0) Then

HMIObjects.Backcolor = RGB(0,0,255)

Else

HMIObjects.Backcolor = RGB(255,0,0)

End if

Next

End Sub


Read Network Drives VB-Action scripts

Read Network Drives


Sub OnClick(ByVal Item)ย ย 

Dim FileSyst, LW, LWListe, Ausgabe, Bezeichnung

Set FileSyst = CreateObject(โ€œSCRIPTING.FILESYSTEMOBJECTโ€)

Set LWListe = FileSyst.Drives

For Each LW In LWListe

If LW.DriveType = 3 Then โ€˜Network

Bezeichnung = LW.Sharename

Elseif LW.DriveType = 4 Then โ€˜CD-ROM

ย  Bezeichnung = โ€œCD-ROMโ€

Else

On Error Resume Next

Bezeichnung = LW.VolumeName

End If

Ausgabe = Ausgabe & LW.DriveLetter & โ€ โ€“ โ€ & Bezeichnung & Chr(13)

Bezeichnung = โ€œโ€

Next

MsgBox Ausgabe

End Sub


Date/Time VB-Action scripts

ย Adding Date/Timeย 


Sub OnClick(ByVal Item)ย  ย  ย ย 

Dim TagX, MonatX, JahrX, StundeX, MinuteX, SekundeX

Dim objOldTime

Dim objNewTime

Dim AktTime

Set objOldTime = ScreenItems(โ€œOrginalDateโ€)

Set objNewTime = ScreenItems(โ€œNewDateโ€)

Dim ZahlTage

Dim ZahlStunden

Dim ZahlMinuten

Set ZahlTage = ScreenItems(โ€œeaTageโ€)

Set ZahlStunden = ScreenItems(โ€œeaStundenโ€)

Set ZahlMinuten = ScreenItems(โ€œeaMinutenโ€)

TagX = Day(Now)

MonatX = Month(Now)

JahrX = Year(Now)

StundeX = Hour(Now)

MinuteX = Minute(Now)

SekundeX = Second(Now)

โ€˜Msgbox โ€œTag: โ€ & TagX & vbcrlf & โ€œMonat: โ€ & MonatX & vbcrlf & โ€œJahr: โ€ & JahrX & vbcrlf & โ€œStunde: โ€ & StundeX & vbcrlf & โ€œMinute: โ€ & MinuteX & vbcrlf & โ€œSekunde: โ€ & SekundeX

objOldTime.Text = Now

AktTime = Now

AktTime = DateAdd(โ€œdโ€,ZahlTage.InputValue,AktTime) โ€˜ Tag hinzuaddieren

AktTime = DateAdd(โ€œhโ€,ZahlStunden.InputValue,AktTime) โ€˜ Stunden hinzuaddieren

AktTime = DateAdd(โ€œnโ€,ZahlMinuten.InputValue,AktTime) โ€˜ Minuten hinzuaddieren

objNewTime.Text = โ€ Neue Zeit : โ€ & AktTime

โ€˜MsgBox โ€œOrginal Zeit: โ€ & Now & vbcrlf & โ€ Neue Zeit : โ€ & AktTime

End Sub


Change date toย  SQL format


Sub OnClick(ByVal Item)ย  ย  ย  ย ย 

Dim TagX, MonatX, JahrX, StundeX, MinuteX, SekundeX

Dim objOldTime

Dim objNewTime

Dim AktTime

Set objOldTime = ScreenItems(โ€œOrginalDateโ€)

Set objNewTime = ScreenItems(โ€œNewDateโ€)

Dim ZahlTage

Dim ZahlStunden

Dim ZahlMinuten

Set ZahlTage = ScreenItems(โ€œeaTageโ€)

Set ZahlStunden = ScreenItems(โ€œeaStundenโ€)

Set ZahlMinuten = ScreenItems(โ€œeaMinutenโ€)

TagX = Day(Now)

MonatX = Month(Now)

JahrX = Year(Now)

StundeX = Hour(Now)

MinuteX = Minute(Now)

SekundeX = Second(Now)

โ€˜Msgbox โ€œTag: โ€ & TagX & vbcrlf & โ€œMonat: โ€ & MonatX & vbcrlf & โ€œJahr: โ€ & JahrX & vbcrlf & โ€œStunde: โ€ & StundeX & vbcrlf & โ€œMinute: โ€ & MinuteX & vbcrlf & โ€œSekunde: โ€ & SekundeX

objOldTime.Text = Now

AktTime = Now

AktTime = DateAdd(โ€œdโ€,ZahlTage.InputValue,AktTime) โ€˜ Tag hinzuaddieren

AktTime = DateAdd(โ€œhโ€,ZahlStunden.InputValue,AktTime) โ€˜ Stunden hinzuaddieren

AktTime = DateAdd(โ€œnโ€,ZahlMinuten.InputValue,AktTime) โ€˜ Minuten hinzuaddieren

objNewTime.Text = โ€ Neue Zeit : โ€ & AktTime

โ€˜MsgBox โ€œOrginal Zeit: โ€ & Now & vbcrlf & โ€ Neue Zeit : โ€ & AktTime

Dim TimeSelectionStart,TimeSelectionEnd

TimeSelectionStart = Year(Now) & โ€œ-โ€ & Month(Now) & โ€œ-โ€ & Day(Now) & โ€ โ€ & _

Hour(Now) & โ€œ:โ€ & Minute(Now) & โ€œ:โ€ & Second(Now)

TimeSelectionEnd =ย  Year(AktTime) & โ€œ-โ€ & Month(AktTime) & โ€œ-โ€ & Day(AktTime) & โ€ โ€ & _

Hour(AktTime) & โ€œ:โ€ & Minute(AktTime) & โ€œ:โ€ & Second(AktTime)

Dim SQLStart,SQLEnd

Set SQLStart = ScreenItems(โ€œSQLStartโ€)

Set SQLEnd = ScreenItems(โ€œSQLEndโ€)

SQLStart.Text = TimeSelectionStart

SQLEnd.Text = TimeSelectionEnd

End Sub


Set Date VB-Action scripts

Set Date


Sub OnLButtonDown(ByVal Item, ByVal Flags, ByVal x, ByVal y)ย 

Dim DatumText

Set DatumText = ScreenItems(โ€œDatumโ€)

DatumText.Text = InputBox(โ€œInsert Dateโ€)

Set DatumText = Nothing

End Sub


Change Button Color VB-Action scripts

ย Change Button Color


Sub OnLButtonDown(Byval Item, Byval Flags, Byval x, Byval y)ย  ย  ย  ย  ย  ย ย 

Dim objButton

Set objButton = ScreenItems(โ€œWindowโ€).Screen.ScreenItems(โ€œButton1โ€)

If objButton.Backcolor = RGB(255,0,0) Then

objButton.Backcolor = RGB(0,255,255)

Else

objButton.Backcolor = RGB(255,0,0)

End If

Set objButton = Nothing

End Sub


Read Archives VB-Action scripts

From the start WinCC Archive 7.4 SP1 supportsย string type variables. Weโ€™re also using an interface that provides a query string variable archive. But so far as of the TIA Portal V15.1, TIA WinCC Professional does not support archive string type variables.ย 


Read Archives


Sub OnLButtonDown(ByVal Item, ByVal Flags, ByVal x, ByVal y)ย  ย 

Dim objConnection

Dim strConnectionString

Dim lngValue

Dim strSQL

Dim objCommand

Dim DataSource

Dim Archive

Set Archive = ScreenItems(โ€œArchiveโ€)

Archive.Text = โ€œโ€

Dim DSN_Name

Set DSN_Name = HMIRuntime.Tags(โ€œ@DatasourceNameRTโ€)

DataSource = DSN_Name.Read

HMIRuntime.Trace โ€œDSN Name : โ€ & DataSource & vbCrLf

strConnectionString = โ€œProvider=MSDASQL.1;PersistSecurityInfo=False; Data Source=โ€ &DataSource&โ€;โ€ย 

HMIRuntime.Trace โ€œstrConnectionString: โ€ & strConnectionString & vbCrLf

โ€˜Auswahl des Archives

strSQL = โ€œArchiveโ€

โ€˜Auswahl der Tabelle

Set objConnection = CreateObject(โ€œADODB.Connectionโ€)

objConnection.ConnectionString = strConnectionString

objConnection.Open

โ€˜Verbindung zur Datenbank

Set objCommand = CreateObject(โ€œADODB.Commandโ€)

objCommand.CommandType = 2

โ€˜Im OCX der Type der Tabelle ist 2

With objCommand

ย  ย  .ActiveConnection = objConnection

ย  ย  .CommandText = strSQL

โ€˜Commandtext ist der Name der Tabelle

End With

โ€˜Verbindung herstellen

Dim oRs

Set oRs = objCommand.Execute

โ€˜oRs ist der Rรผckgabewert der Tabelle

Do While Not oRs.eof

โ€˜eof bedeutet End of File

Archive.Text = Archive.Text & oRs.Fields(โ€œValuenameโ€).value & vbCrLf

โ€˜Auswahl der Spalte in der Tabelle die in das Textfenster geschrieben werden sollen

oRs.movenext

โ€˜ร  sonst wรผrde es eine Endlosschleife (movenext bedeutet geh zur nรคchsten Zeile )

Loop

oRs.close

Set oRs=Nothing

Set Archive = Nothing

โ€˜ oRs wird auf null gesetzt

Set objCommand = Nothing

objConnection.Close

Set objConnection = Nothing

โ€˜SchlieรŸen der Verbindung

End Sub


File Transfer info VB-Action scripts

The application example shows a reverse osmosis plant. The plant has a motor and several valves to control the flows within the plant. The mapping of the analog measuring points, the states of valves and motors, as well as the states of the system, is carried out via VB script. The procedures for the state and analog measurement loop simulation are called via triggered actions. Thus a complete simulation of the reverse osmosis plant is guaranteed. The values of the analog measuring points are stored via a process value archive and the status messages via anย alarm archive can be File Transfer.


File Transfer


Sub OnClick(ByVal Item)ย ย 

Dim FileSyst

Dim Datei

Const PFAD = โ€œC:Test.txtโ€

Set FileSyst = CreateObject(โ€œSCRIPTING.FILESYSTEMOBJECTโ€)

Set Datei = FileSyst.OpenTextFile( PFAD, 2, True, False)

Datei.WriteLine(โ€œFirst Line insertedโ€ฆ.โ€)

Datei.WriteLine(โ€œHier kรถnnen viele Zeilen stehenโ€)

Datei.WriteLine(โ€œLast Line insertedโ€ฆ.โ€)

โ€˜MsgBox Datei.ShortName

Datei.Close

MsgBox โ€ Die Datei: -โ€ & PFAD & โ€œ- wurde erstelltโ€

โ€˜Set datei = FileSyst.GetFile(PFAD)

End Sub


Read RegKey VB-Action scripts

ย Read RegKey


Sub OnClick(ByVal Item)ย 

Const REGPATH = โ€œHKLMSoftwareSiemensWinCCSetupBuildNrโ€

Dim Shell

Dim Build

Set Shell = CreateObject(โ€œWSCRIPT.SHELLโ€)

Build = Shell.RegRead(REGPATH)

MsgBox Build

End Sub


Theย GetValueย method of theย My.Computer.Registryย object can be used to read values in the Windows registry.

If the key, โ€œSoftwareMyAppโ€ in the following example, does not exist, an exception is thrown. If theย ValueName, โ€œNameโ€ in the following example, does not exist,ย Nothingย is returned.

Theย GetValueย the method can also be used to determine whether a given value exists in a specific registry key.

When code reads the registry from a Web application, the current user is determined by the authentication and impersonation that is implemented in the Web application.

Network Config VB-Action scripts

Below script is for network path and network drives.


Network Config


Sub OnClick(ByVal Item)ย 

Dim NW

Set NW = CreateObject(โ€œWSCRIPT.NETWORKโ€)

MsgBox NW.UserDomain

MsgBox nw.ComputerNameย 

MsgBox nw.UserNameย 

End Sub


Open File Dialog VB-Action scripts

Open File Dialog VB-Action scripts winCC
Open File Dialog

Utilization of shell VBS object โ€“ > I call DOS orders and catch the shell yield into a book record which I then, at that point read into List Objects on the HMI screen.ย 

utilization of File System Object โ€“ > fso objects give numerous techniques and properties that can be utilized similarlyย 

It doesnโ€™t look extravagant and it has a few disadvantages, for example, to enter a subdirectory, I utilize a pushbutton to call fitting content, while it would be more pleasant if the client would enter an index with double tap on chosen list thing (basic Click occasion on the List box object works defective when there is a parchment bar showed close to the rundown).


Open File Dialog


Sub OnClick(Byval Item)ย  ย 

โ€˜ACHTUNG Code funktioniert nur wenn Lizenz von Microsoft da ist

โ€˜ z.b. Visual Studio 6

dim dlgCommonDialog

set dlgCommonDialog = CreateObject(โ€œMSComDlg.CommonDialogโ€)

On Error Resume Next

ย  ย  ย  ย  dlgCommonDialog.CancelError = True

ย  ย  ย  ย  dlgCommonDialog.DialogTitle = โ€œEine Datei auswรคhlenโ€

ย  ย  ย  ย  dlgCommonDialog.Filter =โ€Alle Dateien(*.*) | *.*โ€

ย  ย  ย  ย  dlgCommonDialog.MaxFileSize = 255

ย  ย  ย  ย  dlgCommonDialog.ShowOpen

ย If .Filename <> โ€œโ€ then

ย  Dim objTextanzeige

ย  Set objTextanzeige = ScreenItems(โ€œPfadTextโ€)

ย  objTextanzeige.Text = dlgCommonDialog.Filename

ย Else

ย  MsgBox โ€œKeine Datei angewรคhltโ€, vbinformation

ย End if

set dlgCommonDialog=nothing

End Sub


Windows Color Dialog VB-Action scripts

Display a modular exchange box that permits the client to pick particular shading esteem. The client can pick a shading from either a bunch of essential or custom shading ranges. On the other hand, the client can produce shading esteem by changing the RGB or tone, immersion, radiance (HSL) shading upsides of the discourse box UI. The Color exchange box returns the RGB worth of the shading chosen by the client.ย 


ย Windows Color Dialog


Sub OnClick(ByVal Item)ย ย 

dim dlgCommonDialog

set dlgCommonDialog = createobject(โ€œMSComDlg.CommonDialogโ€)

dlgCommonDialog.ShowColor

Dim objButton

Set objButton = ScreenItems(โ€œButton1โ€)

objButton.BackColor = dlgCommonDialog.Color

โ€˜msgbox โ€œDez: โ€ & cstr(dlgCommonDialog.Color) & vbCR & _

โ€˜ย  ย  ย  ย โ€œHex: โ€ & hex(dlgCommonDialog.Color)

Set objButton = Nothing

set dlgCommonDialog=nothing

End Sub


Read Path VB-Action scripts

Read Path


Sub OnClick(ByVal Item)ย 

Dim objShell

Dim objEnv

Dim varPath

Set objShell = CreateObject(โ€œWSCRIPT.SHELLโ€)

Set objEnv = objShell.Environment(โ€œPROCESSโ€)

varPath = objEnv(โ€œPATHโ€)

MsgBox varPath

End Sub


NB Designer OMRON HMI Youtube Tutorial Videos
DOPSOFT DELTA HMI Youtube Tutorial Videos
Wonderware Intouch Youtube Tutorial Videos
GT Designer MITSUBISHI HMI Youtube Tutorial Videos
WinCC Siemens HMI Youtube Tutorial Videos
FACTORY TALK VIEW Youtube Tutorial Videos

Also Read:-