Saturday, 29 September 2012

Learn basics of Vb.Net in 10 Simple Posts - Part 1

Hi, this is the first post where I am going to give a brief introduction on VB.Net and how to start learning it. If you know any of the programming basics or good at logic and make use of existing resources then you are at the right place to gain all the knowledge.
All the posts in the series would use Microsoft Visual Studio 2010 (VS 2010) to demonstrate the VB.Net.

Creating First Project in VS 2010
1.       Open Microsoft VS 2010
2.       Goto File->New Project (Ctrl + N shortcut)
3.       Select Windows on the left pane and Project type as Windows Forms Application from middle pane. At the bottom of the Window Name: WindowsApplication1 is given by default change it to MYFirstProject and click on Ok.




4.       Now the screen should be showing the below the Form Form1.vb is created by default by VS and the solution Explorer shows the project files such as all the VB files, forms etc


5.       Now let us Debug the project created. Go to Debug -> Start Debugging (F5 shortcut).  And the below window should pop up.

6.       Next would be changing the Form name and Form Title. To do this we need to change the property of the Form.                The Property of any object in VB.Net can be changed in two ways one is by changing the property from properties window and other by changing the property from code. Both are equally helpful and would need understanding on how to use and when to use.



The Properties window shows the various properties that an object supports. Let us understand few of them

a. Name: specifies the name of the Object.
b. Text:  this property is to give the display value for the object. For a form it would be the Title and for a Button it would be a Button name and so on.
c.  Font :  this property is to change the font type.
d. ForeColor: this property is to change the color of the text.
e. BackColor: this property is to change the background color of the object.
f.  Enabled: this property is used to Enable or Disable the Object
g. StartPosition: this property is used to set the start position of the Form. Like the form should at the center of the screen, bottom right etc.

There are still a lot of properties that we will be using let use discuss on them when we come across.




7.       Let us change the Form name from Form1 to MyFirstForm and Form Text from Form1 to Hellow Form.
8.       Let us add the first control on the Form MyFirstForm. From the ToolBox click on Button and drag the Button on the Form and place it somewhere on the form.

9.       Let us rename the Button name and Button text. Select the Button on the Form and go to Properties window. And change the Button Name property to HellowButton and Button Text to Say !! Hellow . The form should as below now


Note: The button size might not be sufficient to hold the Button Text as “Say !! Hellow” increase the button width and should work. Also for the form can be decreased.
10.   Now that we have added a button on the form let us assign functionality to the form. When we click on the Button then a popup should come up saying “Hellow”. To do this double click on the Button and it should take to you to the code screen of Form1



The above code is generated by default. We shall discuss on each of them in details in later posts. As of now let us just add the required functionality.
Add the below lines of code before End Sub.

MsgBox (“Hellow !!!” )




11.   Now let us debug the code by pressing F5 and click on Say !! Hellow Button

12.   Congrats we just completed our first VB.Net Project. Stay tuned for more.

Monday, 16 January 2012

Excel Automation - Part 1

Automation Using Excel Macro

Most of us use the Excel for one reason or the other. with Excel we can automate all the manual work of data comparison.

Here is an example of how such a automation can be done.

Requirement.
1. There are two systems with Data1 and Data2
2. Because of some reason Data1 Accounts do not match with Data2 Accounts
3. The data of both the systems are provided in a separate excel sheets
4. Only the Accounts match in both sheets and rest of the data needs to be validated

Follow the below steps to accomplish the above requirement

=>Open a New Excel sheet
=>Copy the data of First System in Sheet "Data1"
=>Copy the data of Second System in Sheet "Data2"
=> Use Alt+F11 to open MS VS
=>Insert New Module
=>Create a new Sub by name Compare

The data I am using has a sample format. And the data does not remain same always, however a little change in the code can satisfy the requirement. The sample data here has the below format

Fig 1: Data Sheets

Fig 2: Data format in Data1 Sheet

Fig 3: Data format in Data2 Sheet


Use the below code (Modify the code as required)

Sub Compare()
    Dim iRowData1, iColumnData1 As Integer
    Dim iRowData2, iColumnData2 As Integer
    Dim MatchFlg As Integer
    iRowData1 = 2
    iColumnData1 = 1
    Sheets("Data1").Select
    While (Cells(iRowData1, iColumnData1) <> "")
    AccountData1 = Cells(iRowData1, iColumnData1)
    SsnData1 = Cells(iRowData1, iColumnData1 + 1)
    Sheets("Data2").Select
    iRowData2 = 2
    iColumnData2 = 1
    MatchFlg = 0
        While (Cells(iRowData2, iColumnData2) <> "")
        AccountData2 = Cells(iRowData2, iColumnData2)
        SsnData2 = Cells(iRowData2, iColumnData2 + 2)
            If AccountData1 = AccountData2 Then
                If SsnData1 = SsnData2 Then
                MatchFlg = 1
                GoTo NextRow
                End If
            End If
        iRowData2 = iRowData2 + 1
        Wend
NextRow:
        Sheets("Data1").Select
        If MatchFlg = 0 Then
        Cells(iRowData1, iColumnData1 + 3) = "No Match Found"
        Else
        Cells(iRowData1, iColumnData1 + 3) = "Match Found"
        End If
        iRowData1 = iRowData1 + 1
    Wend
End Sub


After the Macro run the Data1 Sheet would be having the following data


This code really helped me a lot. For me it saved around three days work and I had to compare around 35K records. The Macro would run for 5 min. 

For more Stay Tuned.....

Batch File Automation - Part 4

Archive Files Using Automation - by Passing Arguments

In the previous post we had a scenario where we had archived files form one folder to Archive folder but the structure of the folder was fixed as we had hard coded the values in the script. Now let us see how we can write a code that can work on any folder structure. 

The best part of any VB Script is it can accept arguments from command line. As the script can accept the arguments from command line we can have a bat file that can call the VB.

The VB script below would accept four parameters and if four parameters not passed it would throw a message as "Not enough Arguments". The four arguments w.r.t to the position of the arguments is 
Arg(0):{DriveName, E:}, 
Arg(1):{PathName, Test_E}, 
Arg(2):{ArchivePath, E:\Test_E\Archive\}, 
Arg(3):{Prefix, Test_E}
These arguments are for the requirement if the source is E:\Test_D and Archive path is E:\Test_D\Archive and Files with prefix as Test_E*.txt would be archived.

strComputer = "."
If WScript.Arguments.Count = 4 Then
Arg_1 = cstr(WScript.Arguments.Item(0))
Arg_2 = cstr(WScript.Arguments.Item(1))
Arg_3 = cstr(WScript.Arguments.Item(2))
Arg_4 = cstr(WScript.Arguments.Item(3))
strName = strDrive & "\" & "Test\"
Else
 Wscript.Echo "Not enough Argumets"
 Wscript.Quit
End If
strDrive = Arg_1
StrPath = Arg_2
desPath = Arg_3
PrefixfName = Arg_4 & "%"
Set objWMIService = GetObject("winmgmts:\\" & strComputer)
Set objF = CreateObject("Scripting.FileSystemObject")
Set colFiles = objWMIService.ExecQuery("Select * from CIM_DataFile where Drive = '" & _
strDrive & "' AND Path = '\\" & strPath & "\\' AND FileName Like '"& prefixfName &"'  AND Extension = 'txt'")
For Each objFile In colFiles
objF.CopyFile objFile.Name, (desPath & objFile.Filename & "_" & _
Left(objFile.CreationDate, Len(objFile.CreationDate) - 11) & "." & objFile.Extension)
Next


The above script is saved as ArchiveAny.vbs

The requirement as below
 There are two folders in two different drives 
E:\Test_E and E:\Test_E\Archive\ and files have prefix as Test_E
E:\Test_D and D:\Test_D\Archive\ and files have prefix as Test_D

The bat file has following lines
@echo off
Cscript ArchiveAny.vbs "e:" Test_E "e:\Test_E\Archive\" "E_Test" 
Cscript ArchiveAny.vbs "d:" Test_D "d:\Test_D\Archive\" "D_Test" 

This is really a good way to automate the process of automation if scheduled. We shall discuss on how such a process can be scheduled. 

Stay Tuned....

Batch File Automation Part - 3

Archive Files Using Automation

Let me discuss on a new requirement that was in my project. The requirement is as follows.

1. A particular directory would have a number of text files with different names but they all have a similar prefixes.
2. The number of files are too many which would make manual work tedious and time consuming.
3. The files must be archived to the Archive file and they should be having a time stamp appended in the file name.
4. The appended time stamp should not be the archived date but the file created date. So that this helps in checking the date  which the file was created.
5. Once the files are archived they would be deleted form the source directory.

The requirement is very simple and a VB script would be doing the entire process with few lines in it.

Consider the folder structure E:\Test\ and E:\Test\Archive.
E:\Test\ folder would have 10 files namely Text_1.txt, Text_2.txt, Text_3.txt, Text_4.txt, Text_5.txt, Text_5.txt, Text_7.txt, Text_8.txt, Text_9.txt, Text_10.txt.

All these 10 files in folder E:\Test\ to be archived to E:\Test\Archive. 

Here is a sample VB Script which would do satisfy the above requirement.

strComputer = "." ' Comuter Name
strDrive = "e:" ' Drive Letter
strPath = "Test" 'Directory Name
desName = strDrive & "\" & strPath & "\Archive\" 'Complete path of Target Directory
Set objWMIService = GetObject("winmgmts:\\" & strComputer)
Set objF = CreateObject("Scripting.FileSystemObject")
Set colFiles = objWMIService.ExecQuery("Select * from CIM_DataFile where Drive = '" & _
strDrive & "' AND Path = '\\" & strPath & "\\' AND FileName Like 'Test%' AND Extension = 'txt'") ' this would fetch a cursor like in PLSQL with all the matches
For Each objFile In colFiles
objF.CopyFile objFile.Name, (desName & objFile.Filename & "_" & _
Left(objFile.CreationDate, Len(objFile.CreationDate) - 11) & "." & objFile.Extension)  'Move each file and rename it to archived directory
Next

Save the above code snippet in a file by name Archive.vbs and then call the vbs file from bat file. I would call my bat file Archive.bat. This bat file would have following line

@echo off
Start Archive.vbs

The bat file would call the vbs and archive all the files as required.

How ever the above code would be specific only to a specific structure. We wuld be discusing how this can be generalized to any folder structure.

Stay Tuned....... 

Tuesday, 10 January 2012

Batch File Automation Part-2

As discussed in the previous thread. A Batch file along with VB can used to automate a process.

Let us consider a requirement where you are suppose to place a file in a directory. The directory might have a file already with same name as the one being moved. This is very easy when we do it manually as the OS would through a error saying "file with same name already exists and do you want to replace it"  and we can easily click on yes and continue. But when we automate we should be able to move the file with out any discrepancies.

Let us see how such a automation can be done.

Steps
1. Create a Batch file that would create a New file by name "Move_Me.txt" at C:\Test
2. The batch file would then try to move the file to C:\Test\Moved using a VBS

Fig : Showing the files used in the example 
The details of the files

CreateFile.bat
This file has the following lines to


@Echo off
Echo "I will be Moved" > C:\Test\Move_Me.txt
Echo "File Move_Me.txt has been created. File would be moved now...."
pause
Start MoveFile.vbs


The above piece of code would first create a File Move_Me.txt with test as "I will be Moved".
Then the code would display message on the screen saying "File Move_Me.txt has been created. File would be moved now...." and pause would wait for user input.
Start command would kick start the MoveFile.vbs which would move the Move_Me.txt to the destination folder.

MoveFile.vbs
VB script is as below



FilePath = "C:\Test\"
Filename = "Move_Me.txt"
NewPath = "C:\Test\Moved\"
Set ScriptObj = CreateObject("Scripting.FileSystemObject")

If ScriptObj.FileExists(FilePath & Filename) Then
    If ScriptObj.FileExists(NewPath & Filename) Then
        ScriptObj.DeleteFile (NewPath & Filename)
    End If
ScriptObj.MoveFile (FilePath & Filename), (NewPath & Filename)
    End If
Set ScriptObj = Nothing

The code is hard coded with the values of file path and file name. Not a good programming practice. But for beginner this should be fine. How ever we will discuss on how parameters can be passed with a different example.

Try this out and let us discuss if any better way of doing this.

Stay tunned for upcoming topics.........

Saturday, 31 December 2011

Batch File Automation Part-1


Batch File Automation



In the previous post a small detail on how to create a Batch file in MsDos was shown. Just a Batch file by itself is of no use until we have a specific task for it. As an example a Batch file when runs, creates a Notepad file and adds a set of lines in it and saves. This is a very simple example. This kind of requirement is simple in Unix platforms where the shell scripts would help. Similar task can be accomplished by using VB script.



 The structure for such automation is as below

Fig 1 Structure of Automation

In the Fig a batch file would trigger a VB script and the VB script would interact with the application in return

We shall discuss with example how such a structure can be used and automation can be achieved.

Stay tunned……..


Friday, 30 December 2011

Siebel Business Service Part-3

Siebel Business Service Part-3


In the previous post we discussed on different types of Business Services based on where they are created. Let us now discuss what are basic building blocks of Siebel BS.

A Siebel BS mainly consists of Methods and PropertySets.

Methods : one or more operations in BS. For our better understanding we can compare this as any C++/Java program where we have multiple functions defined in it. Based what function is called the functionality of the program would depend on. We will understand this in better way when we see a BS in detail. At this point this should give us a rough idea.

PropertySets : used to pass input/output arguments. As Methods may require Inputs/Outputs these are formed by PropertySets in Siebel. A Property set is a  data structure.

Methods take arguments that can be passed into the object programmatically or, in the case of Siebel EAI, declaratively by way of workflows. We will discuss on this in the upcoming post.




Fig 3.1 Block diagram showing Structure of Siebel Business Service at a High Level.




Fig 3.1 shows the block diagram of Siebel Business Service at a High level. In the above Fig we see that the Business service has Input arguments and Output arguments these can be property sets or just an individual input to the BS. And the Business Service has three methods defined in it namely Update, Reset, Delete Method. Depending on the requirement we can use the Business service by calling any of the Methods in it.