<%@ LANGUAGE="VBSCRIPT" %> <%=WebTitle%>
Select Employee Department for CSV output <% OpenDB subSql = "SELECT DISTINCT department FROM employee" ChoiceVal = Request.Form("optDept") deptSEL = Request.Form("optDept") intn = 1 theWidth = 30 Response.Write "" %>
<% if Request.Form("optDept") <> "" then Response.Write "Department Selected:" + Request.Form("optDept") else Response.End end if %>
<% function chkEmpName(dvar) if isNull(dvar) or dvar = "" then chkEmpName = subRS.Fields("card_no") else chkEmpName = dvar end if end function function chkCardNo(dvar) if IsNull(dvar) then chkCardNo = "0" else chkCardNo = dvar end if end function function removeleadingzero(dvar) on error resume next xxx = cdbl(dvar) if err then removeleadingzero = dvar exit function end if removeleadingzero = cstr(xxx) end function function removeunwantedchar(dvar) dvar = replace(dvar, ",", "") dvar = replace(dvar, "'", "") dvar = replace(dvar, "/", "") removeunwantedchar = dvar end function Dim subSQL Dim memtowr Dim memforhtml openDB on error resume next subSQL = "Select sysdate() " subSQL = "SELECT emp_id, card_no, emp_name, length(emp_name) namelen FROM employee " subSQL = "SELECT * FROM (" + subSQL + ")tbl1 ORDER BY namelen DESC " strWhere = "WHERE department = '" + deptSEL + "' " if Request.Form("optDept") = "ALL" then strWhere = "" end if 'strWhere = "WHERE department = 'TEST' " 'Response.Write "strWhere: " + strWhere + "
" subSQL = "SELECT emp_id, card_no, emp_name, department " + _ "FROM employee " + _ strWhere + _ "ORDER BY emp_id " 'Response.Write subSQL + "
" 'Response.end Set subRS = Server.CreateObject("ADODB.Recordset") subRS.Open subSQL, adoCON 'getSysDate = subRS.Fields("emp_id") if err then Response.Write("There is an Error
") Response.Write(err.description + "
") Response.Write(subSQL+"
") end if memforhtml = "" memforhtml = memforhtml + "" memforhtml = memforhtml + "" memforhtml = memforhtml + "" memforhtml = memforhtml + "" memforhtml = memforhtml + "" memforhtml = memforhtml + "" 'memtowr = "*Person ID,*Organization,*Person Name,Card No." + chr(13) + chr(10) memtowr = "*Person ID,*Organization,*Person Name,*Gender,Contact,Email,Effective Time,Expiry Time,Card No." + chr(13) + chr(10) while subRS.EOF = false memforhtml = memforhtml + "" memforhtml = memforhtml + "" memforhtml = memforhtml + "" memforhtml = memforhtml + "" memforhtml = memforhtml + "" memforhtml = memforhtml + "" memtowr = memtowr + _ removeleadingzero(chkNull(subRS.Fields("card_no"))) + "," + _ "Carsem/" + removeunwantedchar(subRS.Fields("department")) + "," + _ chkEmpName(removeunwantedchar(chkNull(subRS.Fields("emp_name")))) + "," + _ "1," + _ "," + _ "," + _ "'2019/11/18 00:00:00," + _ "'2029/11/17 23:59:59," + _ chkNull(subRS.Fields("card_no")) + _ chr(13) + chr(10) subRS.MoveNext wend memforhtml = memforhtml + "
" + "EmpID" + "" + "CardNo" + "" + "EmpName" + "" + "Department" + "
" + chkNull(subRS.Fields("emp_id")) + "" + chkCardNo(subRS.Fields("card_no")) + "" + chkEmpName(chkNull(subRS.Fields("emp_name"))) + "" + chkNull(subRS.Fields("department")) + "
" 'Response.Write(cstr(getSysDate)) Set subRS = Nothing 'Response.Write("This is a test
") Response.Write("

") 'Response.Write(memtowr) 'Response.Write("ServerPath:" + Server.MapPath("uploaded") + "
") 'Response.Flush() dim fs,f set fs=Server.CreateObject("Scripting.FileSystemObject") set f=fs.CreateTextFile(Server.MapPath("uploaded") + "\employee.csv",true) f.write(memtowr) f.close set f=nothing set fs=nothing %>
<% Response.Write ("Download the ") Response.write ("CSV here") %>


<% Response.Write memforhtml %>