Function ReadFromDatabase(strDate,iType)
'iType:
    '1:Mmm dd,YYYY (Jan 13,1999)
    '2:MM/DD/YYYY (01/13/1999)
    '3:MM-DD-YYYY (01-13-1999)
    '4:YYYY/MM/DD (1999/01/13)
    '5:YYYY-MM-DD (1999-01-13)
    '6:DD/MM/YYYY (13/01/1999)
    '7:DD-MM-YYYY (13-01-1999)

	Dim m_Date
	Dim strYear
	Dim strMonth
	Dim strDay

	If not IsDate(strDate) Then 
		ReadFromDatabase = ""					'If it's empty then error.
		Exit Function
	End If

	If Len(Trim(strDate))<1 Then 
		ReadFromDatebase = ""
		Exit Function
	End If 

    If iType < 6 Then
        strYear = Year(strDate)
        strMonth = Month(strDate)
        strDay = Day(strDate)
    Else
        If InStr(Left(strDate, 4), "-") > 0 Or InStr(Left(strDate, 4), "/") > 0 Then
            strYear = ParseDate(strDate, iType, 0)
            strMonth = ParseDate(strDate, iType, 1)
            strDay = ParseDate(strDate, iType, 2)
        Else
            strYear = Year(strDate)
            strMonth = Month(strDate)
            strDay = Day(strDate)
        End If
    End If
	If len(strMonth)=1 then
		strMonth = "0" & strMonth
	end if
	if len(strDay)=1 then
		strDay = "0" & strDay
	end If
	Select Case iType
		Case 1
			m_Date = ConvertMonth(strMonth) & " " & strDay & "," & strYear
		Case 2
			m_Date = strMonth & "/" & strDay & "/" & strYear
		Case 3
			m_Date = strMonth & "-" & strDay & "-" & strYear
		Case 4
			m_Date = strYear & "/" & strMonth & "/" & strDay
		Case 5
			m_Date = strYear & "-" & strMonth & "-" & strDay
		Case 6
			m_Date = strDay & "/" & strMonth & "/" & strYear
		Case 7
			m_Date = strDay & "-" & strMonth & "-" & strYear
	End Select

	ReadFromdatabase = m_Date
End Function

Function ConvertMonth(strMonth)
	dim m_Month
	Select Case CStr(strMonth)
		Case "01","1"
			m_Month = "Jan"
		Case "02","2"
			m_Month = "Feb"
		case "03","3"
			m_Month = "Mar"
		Case "04","4"
			m_Month = "Apr"
		Case "05","5"
			m_Month = "May"
		Case "06","6"
			m_Month = "Jun"
		Case "07","7"
			m_Month = "Jul"
		Case "08","8"
			m_Month = "Aug"
		Case "09","9"
			m_Month = "Sep"
		Case "10"
			m_Month = "Oct"
		Case "11"
			m_Month = "Nov"
		Case "12"
			m_Month = "Dec"
		Case Else
			m_Month = "error"
	End Select
	ConvertMonth = m_Month
End Function

Function vbIsDate(DateStr,iType)
	dim sDate

	if IsDate(DateStr) then
		sDate = FmtDate(DateStr,iType)
		if sDate = "" then
			vbIsDate = ""
		else
			if IsDate(sDate) then
				vbIsDate = sDate
			else
				vbIsDate = ""
			end if
		end if
	else
		vbIsDate = ""
	end if
End Function

Function FmtDate(sDate,iType)
	dim iPos
	dim sYear,sMonth,sDay
	dim i

	Select Case iType
		Case 6					'13/01/1999
	        for i=1 to len(sDate)
				if mid(sDate,i,1)>"9" or mid(sDate,i,1)<"0" then
					sDate = Replace(sDate,mid(sDate,i,1),"/")
				end if
			next
			ipos = instr(sDate,"//")
			do while ipos>0
				sDate = Replace(sDate,"//","/")
				ipos = instr(sDate,"//")
			loop

			sYear = right(sDate,4)
       	    iPos = InStr(sDate, "/")
           	sMonth = Mid(sDate, iPos + 1, InStrRev(sDate, "/") - iPos - 1)
            sDay = Left(sDate, iPos - 1)

			if isnumeric(sYear) and isnumeric(sMonth) and isnumeric(sDay) then
				FmtDate = sYear & "-" & sMonth & "-" & sDay
			else
				FmtDate = ""
			end if
		Case 7					'13-01-1999
	        for i=1 to len(sDate)
				if mid(sDate,i,1)>"9" or mid(sDate,i,1)<"0" then
					sDate = Replace(sDate,mid(sDate,i,1),"-")
				end if
			next
			ipos = instr(sDate,"--")
			do while ipos>0
				sDate = Replace(sDate,"--","-")
				ipos = instr(sDate,"--")
			loop

			sYear = right(sDate,4)
       	    iPos = InStr(sDate, "-")
           	sMonth = Mid(sDate, iPos + 1, InStrRev(sDate, "-") - iPos - 1)
            sDay = Left(sDate, iPos - 1)

			if isnumeric(sYear) and isnumeric(sMonth) and isnumeric(sDay) then
				FmtDate = sYear & "-" & sMonth & "-" & sDay
			else
				FmtDate = ""
			end if
		Case Else
			FmtDate = sDate
	End Select
End Function

Function ParseDate(sDate, DateType, iType)
    Dim iPos
    Dim i

    Select Case DateType
        Case 1      'Mmm dd, yyyy (Jan 13, 1999)
            Select Case iType
                Case 0          'year
                    ParseDate = Right(sDate, 4)
                Case 1          'month
                    ParseDate = Month(sDate)
                Case 2          'day
                    ParseDate = Day(sDate)
            End Select
        Case 2      'MM/DD/YYYY (01/13/1999)
            Select Case iType
                Case 0
                    ParseDate = Right(sDate, 4)
                Case 1
                    iPos = InStr(sDate, "/")
                    ParseDate = Left(sDate, iPos - 1)
                Case 2
                    iPos = InStrRev(sDate, "/")
                    ParseDate = Mid(sDate, InStr(sDate, "/") + 1, iPos - InStr(sDate, "/") - 1)
            End Select
        Case 3      'MM-DD-YYYY (01-13-1999)
            Select Case iType
                Case 0
                    ParseDate = Right(sDate, 4)
                Case 1
                    iPos = InStr(sDate, "-")
                    ParseDate = Left(sDate, iPos - 1)
                Case 2
                    iPos = InStrRev(sDate, "-")
                    ParseDate = Mid(sDate, InStr(sDate, "-") + 1, iPos - InStr(sDate, "/") - 1)
            End Select
        Case 4      'YYYY/MM/DD (1999/01/13)
            Select Case iType
                Case 0
                    ParseDate = Left(sDate, 4)
                Case 1
                    iPos = InStr(sDate, "/")
                    ParseDate = Mid(sDate, iPos + 1, InStrRev(sDate, "/") - iPos - 1)
                Case 2
                    iPos = InStrRev(sDate, "/")
                    ParseDate = Right(sDate, Len(sDate) - iPos)
            End Select
        Case 5      'YYYY-MM-DD (1999-01-13)
            Select Case iType
                Case 0
                    ParseDate = Left(sDate, 4)
                Case 1
                    iPos = InStr(sDate, "-")
                    ParseDate = Mid(sDate, iPos + 1, InStrRev(sDate, "-") - iPos - 1)
                Case 2
                    iPos = InStrRev(sDate, "-")
                    ParseDate = Right(sDate, Len(sDate) - iPos)
            End Select
        Case 6      'DD/MM/YYYY (13/01/1999)
	        for i=1 to len(sDate)
				if mid(sDate,i,1)>"9" or mid(sDate,i,1)<"0" then
					sDate = Replace(sDate,mid(sDate,i,1),"/")
				end if
			next
			ipos = instr(sDate,"//")
			do while ipos>0
				sDate = Replace(sDate,"//","/")
				ipos = instr(sDate,"//")
			loop
            Select Case iType
                Case 0
                    ParseDate = Right(sDate, 4)
                Case 1
                    iPos = InStr(sDate, "/")
                    If iPos < 1 Then
                        ParseDate = "01"
                        Exit Function
                    End If
                    ParseDate = Mid(sDate, iPos + 1, InStrRev(sDate, "/") - iPos - 1)
                Case 2
                    iPos = InStr(sDate, "/")
                    If iPos < 1 Then
                        ParseDate = "01"
                        Exit Function
                    End If
                    ParseDate = Left(sDate, iPos - 1)
            End Select
        Case 7      'DD-MM-YYYY (13-01-1999)
	        for i=1 to len(sDate)
				if mid(sDate,i,1)>"9" or mid(sDate,i,1)<"0" then
					sDate = Replace(sDate,mid(sDate,i,1),"-")
				end if
			next
			ipos = instr(sDate,"--")
			do while ipos>0
				sDate = Replace(sDate,"--","-")
				ipos = instr(sDate,"--")
			loop
            Select Case iType
                Case 0
                    ParseDate = Right(sDate, 4)
                Case 1
                    iPos = InStr(sDate, "-")
                    If iPos < 1 Then
						msgbox (sdate)
                        MsgBox ("Date format error.")
                        ParseDate = "01"
                        Exit Function
                    End If
                    ParseDate = Mid(sDate, iPos + 1, InStrRev(sDate, "-") - iPos - 1)
                Case 2
                    iPos = InStr(sDate, "-")
                    If iPos < 1 Then
                        MsgBox ("Date format error.")
                        ParseDate = "01"
                        Exit Function
                    End If
                    ParseDate = Left(sDate, iPos - 1)
            End Select
    End Select
End Function

Function vbMsgBox(theMsg, theStyle)
	vbMsgBox = MsgBox(theMsg, theStyle, "Microsoft Internet Explorer")
End Function

Function vbMsgBox1(theMsg, theStyle, theTitle)
	vbMsgBox1 = MsgBox(theMsg, theStyle, theTitle)
End Function

