var
sql,exefile,fvn:string;
fcomputername,fdepartment,fhn,found_hn : string;
begin
fvn:= GetGlobalValue('VN');
fcomputername := GetGlobalValue('CURRENT_COMPUTER');
fdepartment := GetGlobalValue('CURRENT_DEPCODE');
//showmessage('Current VN = '+fvn+' , Computer Name = '+fcomputername+' , Department Code = '+fdepartment);
sql:= 'select AA.vn as vn_an,AA.hn,AA.vstdate,AA.type,AA.u_reg '+
'from(select vn.vn,vn.hn,vn.vstdate,"Visit OPD" as type,ou.name as u_reg from ovst vn '+
'left outer join patient pt on pt.hn=vn.hn '+
'left outer join opduser ou on ou.loginname=vn.staff '+
'where vn.vstdate="2019-12-08" and vn.pttype in ("24","25","26") '+
'and pt.pttype in ("24","25","26")'+
'UNION '+
'select an.an,an.hn,an.regdate,"Admit IPD" as type,ou.name as u_reg from ipt an '+
'left outer join patient pt on pt.hn=an.hn '+
'left outer join opduser ou on ou.loginname=an.staff '+
'where an.regdate="2019-12-08" and an.pttype="10" '+
'and pt.pttype in ("24","25","26") '+
') as AA '+
'ORDER BY vn_an DESC '+
'limit 1';
found_hn:=getsqlsubquerydata(sql);
exefile:='AppLineNT.exe code Token "'+ +'Visit error\nvn-an :
WinExec(exefile,nil);
end;