Wednesday, 25 September 2013

Oracle PL/SQL cursor example


--A Procedure ProcCur is created
create or replace procedure ProcCur is

--A cursor is declared selecting 2 fields from employee_master emp_code and emp_name
cursor rec is select emp_code,emp_name from employee_master
              where emp_grade='S'
              order by emp_code;
-- 2 variables are declared of filed type emp_code and emp_name
ecode employee_master.emp_code%type;
ename employee_master.emp_name%type;
begin

-- add this  statement otherwise  ORU-10027: buffer overflow will rise
  dbms_output.enable(1000000);

-- cursor opened
  open rec;

-- column header printed
  dbms_output.put_line(lpad('Code',10,' ')||lpad('Name',60,' '));

-- loop started
  loop

--   current record fetched into ecode and ename. remember that number of fields in the cursor should
--   be equal to number of variables on which records are fetched.
    fetch rec into ecode,ename;

--  exit the loop if when no more records are fetched
    exit when rec%NOTFOUND;

-- fetched values printed
    dbms_output.put_line(lpad(ecode,10,' ')||lpad(ename,60,' '));

-- end of loop
  end loop;

--end of procedure
end TestProc;

-------------------------------------------------------------------------------------------------------------------------
-- Alternate method using for loop, this is the easiest and shortest wa.
-------------------------------------------------------------------------------------------------------------------------

--A Procedure ProcCur is created
create or replace procedure ProcCur is

begin

-- add this  statement otherwise  ORU-10027: buffer overflow will rise
  dbms_output.enable(1000000);

-- column header printed
  dbms_output.put_line(lpad('Code',10,' ')||lpad('Name',60,' '));

-- loop started
  for rec in (select emp_code,emp_name from employee_master
              where emp_grade='S'
              order by emp_code)

  loop

-- fetched values printed
    dbms_output.put_line(lpad(rec.emp_code,10,' ')||lpad(rec.emp_name,60,' '));

-- end of loop
  end loop;

--end of procedure
end TestProc;

Tuesday, 24 September 2013

How to create a dynamic combolist and add data from the database in HTML, Javascript

<html>
<head>
<script>
function myFunction()
{
var con = new ActiveXObject('ADODB.Connection');
var rs = new ActiveXObject('ADODB.Recordset');
con.ConnectionString="Provider=MSDAORA.1;User ID=hr_man;Password=hr_man;Data Source=ORCL;Persist Security Info=False";
con.Open();
rs.Open("select emp_id,emp_name from employee_master order by emp_name").value,con);
var s="<select>";
while(!rs.EOF)
{
s+="<option value='" + rs.Fields("emp_id").Value + "'>" +   rs.Fields("emp_name").Value  + "</option>";
rs.MoveNext();
}
s+-"</select> ";
rs.Close();
con.close;
document.getElementById("comboDiv").innerHTML=s;
}
</script>
</head>
<body>
<div id="comboDiv">
</div>
</body>
 </html>

Monday, 23 September 2013

How to Connect to Oracle and retrieve data using ActiveX object in Javascript

<html>
<head>
<script>
function myFunction()
{
var con = new ActiveXObject('ADODB.Connection');
var rs = new ActiveXObject('ADODB.Recordset');
con.ConnectionString="Provider=MSDAORA.1;User ID=hr_man;Password=hr_man;Data Source=ORCL;Persist Security Info=False";
con.Open();
rs.Open(document.getElementById("sqlTxt").value,con);
var s="<table><tr>";
for(var i=0;i<rs.Fields.count;i++)
s+="<td>" + rs.Fields(i).Name + "</td>";
s+="</tr> <tr>";
while(!rs.EOF)
{
for(var i=0;i<rs.Fields.count;i++)
s+="<td>" + rs.Fields(i).Value + "</td>";
rs.MoveNext();
}
s+-"</tr></table>";
rs.Close();
con.close;
document.getElementById("outputSection").innerHTML=s;
}
</script>
</head>
<body>
<div id="inputSection">
<textArea id="sqlTxt"></textArea>
<input type="Button" id="btn" value="Execute" onclick="myFunction();"/>
</div>
<div id="outputSection">
</div>
</body>
 </html>

Sunday, 22 September 2013

How to invoke Microsoft Common dialog box dynamically using createobject

option explicit

private function OpenDialog() as String
  dim dlg as object
  on error goto  OpenDialog_Error
  set dlg=CreateObject("MSCOMDlg.Commondialog")
  if dlg is nothing then exit sub
  dlg.Filter="All Files (*.*)|*.*"
  dlg.CancelError=True
  dlg.ShowOpen
  OpenDialog=dlg.FileName
 exit sub
OpenDialog_Error:
end sub

private sub Form_Load()
    dim fileName as string
    fileName=OpenDialog()
end sub

Create XML with XML Document Object

option explicit
private xmlObj as Object  ' xml object
private xmlHead as object  ' xml node
private xmlNode as object  ' xml node
private xmlChildNode as object ' xml nodePrivate sub CreateXML
   set xmlObj = CreateObject("MSXML2.DOMDocument") ' dynamic xmlobject created
   if xmlObj is nothing then  ' if failed then exit procedure
        exit sub
  end if
  set xmlHead = xmlObj.createElement("Parent")  'first xml node created , head node
  set xmlNode = xmlObj.createElement("Child1")  ' xmlnode created, first child of head
  cal xmlHead.appenChild(xmlNode) ' set to child of head
  set xmlChildNode= xmlObj.createElement("Child1ofChild1")  ' xmlnode created
  call xmlNode.AppendChild(xmlChildNode)  ' set to first child of the first child of head
  set xmlChildNode= xmlObj.createElement("Child2ofChild1")  ' xmlnode created
  call xmlNode.AppendChild(xmlChildNode) ' set to second child of the first child of head
  set xmlNode = xmlObj.createElement("Child2") ' xmlnode created, second child of head
  call xmlHead.appenChild(xmlNode)  ' set to child of head
   set xmlChildNode= xmlObj.createElement("Child1ofChild2")  ' xmlnode created
  call xmlNode.AppendChild(xmlChildNode) ' set to first child of the second child of head
  set xmlChildNode= xmlObj.createElement("Child2ofChild2")   ' xmlnode created
  call xmlNode.AppendChild(xmlChildNode) ' set to second child of the second child of head
 debug.print xmlHead.documentElement.xml
 set xmlnode=nothing
set xmlchildnode=nothing
set xmlobj=nothing
End Sub

A simple subclassing example

Module Code
option explicit
Private Const GWL_WNDPROC = (-4)
Private Const WM_LBUTTONUP = &H202
Private Const WM_MOUSEMOVE = &H200
Private Declare Function SetWindowLong Lib "User32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
private originalHandler as long

'Window hooked with Customized function Winproc returning the original handle 
public sub SubClassWindow(handle as long)
   oldWindowHandler = SetWindowLong(handler, GWL_WNDPROC, AddressOf WinProc)
End Sub

'Window restore to it's original Windows procedire
public sub UnSubClass(handle as long)
   call setWindowsLong(handler,, GWL_WNDPROC, oldWindowHandler)
End Sub

'Customized Windows function

private sub WinProc(ByVal hWnd As Long, ByVal MSG As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
   select case MSG
      ' If user released it's left mouse button
       Case WM_LBUTTONUP
             debug.print "Left Button Up"
       ' If user moved it's mouse
       Case WM_MOUSEMOVE
            debug.print "Mouse Moving"
   End select
  ' Original windows procedure invoked, this is necessary other wise program will crash
  WinProc = CallWindowProc(OldHandler, hWnd, MSG, wParam, lParam)
End Sub

Validate email with Regxp (regular expression) in Visual Basic 6.0

option explicit
private Function IsValidEmail(ByVal emailStr As String) As Boolean
   
    ' if email is blank then return
    If VBA.Len(VBA.Trim$(emailStr)) = 0 Then
        IsValidEmail = False
        Exit Function
    End If
  
   
    Dim regex  As  Object
    Dim cMatch  As  Object ' For MatchCollection

    Set regex = CreateObject("VBScript.RegExp")  ' Object created with VBscript.RegExp class.

    regex.MultiLine = False ' email is a single line expression therefore multiline is set to false

    regex.IgnoreCase = False  ' Case sensitive checking is set to true

    regex.Pattern = "\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b" ' regular expression pattern

    Set cMatch = regex.Execute(emailStr)
   
    IsValidEmail = Not (cMatch.Count = 0) ' If no matches found then email is invalid

    ' first item of  collection cMatch return length of the email is it is not equal to the length of meail email is invalid.
    If IsValidEmail Then IsValidEmail = IIf(VBA.Len(cMatch.Item(0)) = VBA.Len(emailStr), True, False)
    'Release memory
    Set cMatch = Nothing
    Set regex = Nothing
End Function

Private sub Form_Load()
    if not IsValidEmail("subhroneelganguly@gmail.com")  then
        msgbox "Email not valid"
   else
      msgbox "Email valid"
    end if
End Sub