File: D:/web archive/conferencesearch (old) (to delete)/bookdev/dbfunctions.asp
<%
' ===============
' dbfunctions.asp
' ===============
' Version 1.33 (07/10/14)
' ============
' HISTORY
' ============
' (SS,27/06/05) Add check for Null in Function CleanSQLStr and setting it to "" to avoid invalid use of null errors '
' (SS,18/09/06) Added Sub OpenDatabaseCommon
' (SS,17/10/06) Added IsDatabaseOpen
' (SS,18/05/07) Added Function NZD
' Added Function GetQueryRecordCount
' (SS,21/05/07) Added Function NZL
' Added Sub SetDebugLevel and gnDebugLevel
' Added 'And gnDebugLevel > 1' to GetQueryValue and GetQueryValue2
' to ensure GetQueryValue debugs only shown when DebugLevel is > 1
' default debug level is 1 (use SetDebugLevel(2) to see GetQueryValue logs)
' (SS,22/05/07) Added new function ENKN, slight mods to NZ and NB
' (SS,26/05/07) Added new function StrToBool
' Added ShowOpenDatabaseCount and global gnOpenDatabaseCount, gnCloseDatabaseCount to show count of database opens and closes
' Changes to OpenDatabaseCommon
' Added CloseDatabaseCommon, OpenQueryCommon, CloseQueryCommon to help with single OpenDatabase and CloseDatabase in apputils
' Added IncOpenQueryCount, IncCloseQueryCount, IncExecQueryCount (and gnOpenQueryCount, gnCloseQueryCount, gnExecQueryCount)
' (SS,27/05/07) Added Function FieldExistsInTable
' (SS,28/05/07) Modified Function Round2dp to return a Double instead of String
' Added Function FunctionExists
' (SS,30/05/07) Added ToNamedHTML
' (SS,01/06/07) Renamed IntAsBool to IntToBool, BoolAsInt to BoolToInt
' Added FieldsExistInQuery, FieldExists
' (SS,04/06/07) Added Function TrimAll
' (SS,14/06/07) Renamed GetAppSetting to GetSetting and SetAppSetting to SetSetting, remove TypeID and EntryID from these functions
' (SS,05/07/07) Added HTMLEncode function
' (SS,14/09/07) Added ExecuteQueryCommon
' (SS,14/11/07) Added Sub AddSetting
' (SS,21/10/08) Added Function IsCharAlpha, Function IsCharNumeric, Function StripSpaces and Function SplitPostcode
' (SS,12/11/08) Added Function Format2dpnc
' (SS,17/11/08) Added functions SingleQuotedJavascriptString and DoubleQuotedJavascriptString
' (SS,17/04/09) Added Functions IsWorkingDay, NextWorkingDay, GetDeliveryDateCombo, ConvertUKToMySQLDate
' (SS,22/04/09) Change to Function HTMLEncode to prevent "Type Mismatch" error
' (SS,08/05/09) Change to Function ConvertUKToMySQLDate to return date without "-" and Null as string
' (SS,28/05/09) Added Function IsURL
' (SS,29/05/09) Moved Function ReplaceNewLinesWithBR here from apputils.asp
' (SS,04/06/09) Added Function RemoveNewLines
' (SS,05/06/09) Added Function URLEncode
' (SS,12/06/09) Added Function RandomInteger
' (SS,23/06/09) Added functions TrimChar and TrimChars, changed TrimAll to call TrimChars
' Added Function CleanPageName (from siteutil.asp)
' (SS,14/07/09) Changed to CleanSQLStr to handle ASCII controls codes like linefeed & carriage return
' (SS,16/07/09) Changes to allow this dbfunctions.asp to be used with csl/book app
' Newer version of GetLookupCombo with extra AValueField
' Improved version of Function ConvertNewlinesToHTML
' Added Function ConvertNewlinesForJavascript
' Function GetSetting renamed to Function GetSettingFromField with new AValueField
' New Function GetSetting nows calls GetSettingFromField
' Also added Function GetSettingMemo to get memo value
' Also copied Function FileExists, Sub DeleteFile, Sub CopyFile
' Added extra set of query functions and subs OpenQuery3, CloseQuery3, EndOfQuery3, NextQueryRecord3, GetQueryValue3, also oRS3 global
' (SS,21/07/09) Underscore bug fix to CleanSQLStr and added new CleanSQLStrForLike (not tested)
' Bugfix to Function GetSettingFromField
' (SS,29/07/09) Change to Function IntToBool to treat null as False also
' (SS,07/10/14) Added Sub DisableCache
' ADO constants used here '
' take from adovbs.inc (which is 500 lines long) '
'---- CursorTypeEnum Values ---- '
Const adOpenDynamic = 2
'---- LockTypeEnum Values ---- '
Const adLockOptimistic = 3
'---- CommandTypeEnum Values ---- '
Const adCmdText = &H0001
Const adCmdTable = &H0002
Dim oConn, oConnCommon, oRS, oRS2, oRS3, nRecs, gsADOErrors, gbDatabaseOpen, gbQueryOpen, gbDebugOn, gsErrorMessage, gnDebugLevel
Dim gnOpenDatabaseCount, gnCloseDatabaseCount, gnOpenQueryCount, gnCloseQueryCount, gnExecQueryCount
gbDatabaseOpen = False
gbQueryOpen = False
gbDebugOn = False
gnDebugLevel = 1
gsErrorMessage = ""
gnOpenDatabaseCount = 0
gnCloseDatabaseCount = 0
gnOpenQueryCount = 0
gnCloseQueryCount = 0
gnExecQueryCount = 0
'----------------- start of common database routines ------------------------
' (SS,18/9/06) opens the common mysql database
' (SS,26/5/07) modified to use oConnCommon instead of calling OpenDatabaseConnection
Sub OpenDatabaseCommon
If gbDebugOn Then DebugLog("OpenDatabaseCommon called")
Set oConnCommon = Server.CreateObject("ADODB.Connection")
oConnCommon.ConnectionString = "DSN=MySQL_common;"
oConnCommon.Open
gsADOErrors = ""
End Sub
' (SS,26/5/07) new that works with oConnCommon
Sub CloseDatabaseCommon
If gbDebugOn Then DebugLog("CloseDatabaseCommon called")
oConnCommon.Close
Set oConnCommon = Nothing
End Sub
' (SS,26/5/07) common database version
Sub OpenQueryCommon(ASql)
If gbDebugOn Then DebugLog("OpenQueryCommon called with " & ASql)
IncOpenQueryCount ' (SS,27/5/07)
Set oRS = oConnCommon.Execute(ASql, nRecs, adCmdText)
End Sub
' (SS,26/5/07) common database version
Sub CloseQueryCommon
If gbDebugOn Then DebugLog("CloseQueryCommon called")
IncCloseQueryCount ' (SS,27/5/07)
oRS.Close
Set oRS = Nothing
End Sub
'----------------- end of common database routines ------------------------
' (SS,30/09/03) special version for MySQL '
' (SS,18/9/06) modified to now call OpenDatabaseConnection
Sub OpenDatabase
gnOpenDatabaseCount = gnOpenDatabaseCount + 1
OpenDatabaseConnection(Application("ConnectionString"))
End Sub
Sub OpenDatabaseConnection(AConnection)
If gbDebugOn Then DebugLog("OpenDatabase called")
' open database if not already open '
If Not gbDatabaseOpen Then
Set oConn = Server.CreateObject("ADODB.Connection")
oConn.ConnectionString = AConnection
oConn.Open
gbDatabaseOpen = True
gsADOErrors = ""
End If
End Sub
Sub CloseDatabase
gnCloseDatabaseCount = gnCloseDatabaseCount + 1
If gbDebugOn Then DebugLog("CloseDatabase called")
If gbDatabaseOpen Then
oConn.Close
Set oConn = Nothing
gbDatabaseOpen = False
End If
End Sub
' (SS,17/10/06)
Function IsDatabaseOpen
IsDatabaseOpen = gbDatabaseOpen
End Function
Sub OpenQuery(ASql)
If gbQueryOpen Then CloseQuery ' if query already open then close it first '
If gbDebugOn Then DebugLog("OpenQuery called with " & ASql)
IncOpenQueryCount ' (SS,27/5/07)
Set oRS = oConn.Execute(ASql, nRecs, adCmdText)
gbQueryOpen = True
End Sub
Sub CloseQuery
If gbDebugOn Then DebugLog("CloseQuery called")
If gbQueryOpen Then
IncCloseQueryCount ' (SS,27/5/07)
oRS.Close
Set oRS = Nothing
gbQueryOpen = False
End If
End Sub
Function EndOfQuery
EndOfQuery = oRS.Eof
End Function
Sub NextQueryRecord
oRS.MoveNext
End Sub
' (SS,10/10/01) '
Sub FirstQueryRecord
oRS.MoveFirst
End Sub
' (SS,27/5/07)
Sub IncOpenQueryCount
gnOpenQueryCount = gnOpenQueryCount + 1
End Sub
' (SS,27/5/07)
Sub IncCloseQueryCount
gnCloseQueryCount = gnCloseQueryCount + 1
End Sub
' (SS,27/5/07)
Sub IncExecQueryCount
gnExecQueryCount = gnExecQueryCount + 1
End Sub
' (SS,25/2/02) returns query value. Null is returned if field not found '
' useful when a crosstab query does not create an expected field '
Function GetQueryValueNullIfNotFound(AFieldName)
GetQueryValueNullIfNotFound = Null
On Error Resume Next
GetQueryValueNullIfNotFound = oRS(AFieldName)
End Function
Function GetQueryValue(AFieldName)
If gbDebugOn And gnDebugLevel > 1 Then DebugLog("GetQueryValue called with " & AFieldName)
GetQueryValue = oRS(AFieldName)
End Function
' (SS,27/7/09) alias to GetQueryValue
Function GetQueryField(AFieldName)
GetQueryField = GetQueryValue(AFieldName)
End Function
' (SS,18/5/07) RecordCount doesn't work with MySQL so devised this method
' to do a count, not efficient but fine for small record sets
' starts from first record and moves back to first record
Function GetQueryRecordCount
Dim LRecordCount
LRecordCount = 0
If Not EndOfQuery Then FirstQueryRecord ' if used to prevent Either BOF or EOF is True... error
Do While Not EndOfQuery
LRecordCount = LRecordCount + 1
NextQueryRecord
Loop
If LRecordCount > 0 Then FirstQueryRecord ' if used to prevent Either BOF or EOF is True... error
GetQueryRecordCount = LRecordCount
End Function
' (SS,1/6/07)
Function FieldsExistInQuery(AFieldName)
FieldsExistInQuery = FieldExists(oRS, AFieldName)
End Function
' (SS,27/5/07) returns true if given field exists in given table
Function FieldExistsInTable(ATableName, AFieldName)
If gbDebugOn Then DebugLog("FieldExistsInTable called with " & ATableName & ", " & AFieldName)
IncOpenQueryCount ' (SS,27/5/07)
Dim LRS, LRecs, i, LFound
Set LRS = oConn.Execute("SELECT * FROM " + ATableName + " LIMIT 0", LRecs, adCmdText)
FieldExistsInTable = FieldExists(LRS, AFieldName)
LRS.Close
Set LRS = Nothing
End Function
' (SS,1/6/07) moved code here from FieldExistsInTable, called from 2 places
Function FieldExists(ARS, AFieldName)
Dim i, LFound
LFound = False
For i = 0 To ARS.Fields.Count - 1
If LCase(ARS.Fields.Item(i).Name) = LCase(AFieldName) Then
LFound = True
Exit For
End If
Next
FieldExists = LFound
End Function
' (SS,3/10/01) '
Sub OpenQuery2(ASql)
If gbDebugOn Then DebugLog("OpenQuery2 called with " & ASql)
IncOpenQueryCount ' (SS,27/5/07)
Set oRS2 = oConn.Execute(ASql, nRecs, adCmdText)
End Sub
Sub CloseQuery2
If gbDebugOn Then DebugLog("CloseQuery2 called")
IncCloseQueryCount ' (SS,27/5/07)
oRS2.Close
Set oRS2 = Nothing
End Sub
Function EndOfQuery2
EndOfQuery2 = oRS2.Eof
End Function
Sub NextQueryRecord2
oRS2.MoveNext
End Sub
Function GetQueryValue2(AFieldName)
If gbDebugOn And gnDebugLevel > 1 Then DebugLog("GetQueryValue2 called with " & AFieldName)
GetQueryValue2 = oRS2(AFieldName)
End Function
' (SS,27/7/09) alias to GetQueryValue2
Function GetQueryField2(AFieldName)
GetQueryField2 = GetQueryValue2(AFieldName)
End Function
' (SS,16/7/09)
Sub OpenQuery3(ASql)
If gbDebugOn Then DebugLog("OpenQuery3 called with " & ASql)
IncOpenQueryCount
Set oRS3 = oConn.Execute(ASql, nRecs, adCmdText)
End Sub
' (SS,16/7/09)
Sub CloseQuery3
If gbDebugOn Then DebugLog("CloseQuery3 called")
IncCloseQueryCount ' (SS,27/5/07)
oRS3.Close
Set oRS3 = Nothing
End Sub
' (SS,16/7/09)
Function EndOfQuery3
EndOfQuery3 = oRS3.Eof
End Function
' (SS,16/7/09)
Sub NextQueryRecord3
oRS3.MoveNext
End Sub
' (SS,16/7/09)
Function GetQueryValue3(AFieldName)
If gbDebugOn And gnDebugLevel > 1 Then DebugLog("GetQueryValue3 called with " & AFieldName)
GetQueryValue3 = oRS3(AFieldName)
End Function
' (SS,27/7/09) alias to GetQueryValue3
Function GetQueryField3(AFieldName)
GetQueryField3 = GetQueryValue3(AFieldName)
End Function
Sub ExecuteQuery(ASql)
' On Error Resume Next '
If gbDebugOn Then DebugLog("ExecuteQuery called with " & ASql)
IncExecQueryCount ' (SS,27/5/07)
oConn.Execute(ASql)
' CheckForADOErrors(oConn)
End Sub
' (SS,14/9/07)
Sub ExecuteQueryCommon(ASql)
' On Error Resume Next '
If gbDebugOn Then DebugLog("ExecuteQueryCommon called with " & ASql)
IncExecQueryCount
oConnCommon.Execute(ASql)
End Sub
' (SS,11/5/01)
Function CheckKeyExists(ATableName, AFieldName, AValue)
Dim LoRS
Set LoRS= oConn.Execute("SELECT * FROM " + ATableName + " WHERE " + AFieldName + "=" + AValue, nRecs, adCmdText)
CheckKeyExists = not LoRS.Eof
LoRS.Close
set LoRS = Nothing
End Function
' (SS,15/5/01) '
Sub OpenTable(ATableName)
If gbDebugOn Then DebugLog("OpenTable called")
Set oRS = Server.CreateObject("ADODB.Recordset")
oRS.Open ATableName, oConn, adLockOptimistic, adCmdTable
'oRS.Open ATableName, oConn, 1, ,2 '
' rsProds.Open "products", strProvider, 1, , adCmdTable '
End Sub
' (SS,22/10/01) add because FindFirstRecord is too slow '
' opens recordset and find the record in ACriteria '
Sub OpenTableRecordForEdit(ATableName, ACriteria)
Dim LSQL
Set oRS = Server.CreateObject("ADODB.Recordset")
LSQL = "SELECT * FROM " + ATableName + " WHERE " + ACriteria
oRS.Open LSQL, oConn, adOpenDynamic, adLockOptimistic, adCmdText
End Sub
' (SS,15/5/01) '
Sub CloseTable
If gbDebugOn Then DebugLog("CloseTable called")
' On Error Resume Next
oRS.Close
Set oRS = Nothing
' CheckForADOErrors(oConn)
End Sub
' (SS,15/5/01) must be a better and quicker way, using Locate, FindFirst or something '
' will it work in SQL? '
Function FindRecord(AFieldName, AValue)
Dim LFound
oRS.MoveFirst
LFound = False
Do Until oRS.EOF or LFound
If oRS(AFieldName) & "" = AValue & "" then
LFound = True
Else
oRS.MoveNext
End If
Loop
FindRecord = LFound
End Function
' (SS,19/7/01) for ADO '
Function FindFirstRecord(ACriteria)
oRS.Find ACriteria
FindFirstRecord = Not oRS.EOF
End Function
' (SS,15/5/01) '
Sub EditRecord
End Sub
' (SS,15/5/01) '
Sub AddRecord
oRS.AddNew
End Sub
' (SS,26/2/02) '
Function GetFieldValue(AFieldName)
GetFieldValue = oRS(AFieldName)
End Function
' (SS,15/5/01) '
Sub PutFieldValue(AFieldName, AValue)
' On Error Resume Next
oRS(AFieldName) = AValue
' CheckForADOErrors(oConn)
End Sub
' (SS,15/5/01) '
' (SS,3/10/01) added error handling '
Sub PostRecord
' On Error Resume Next
oRS.Update
' CheckForADOErrors(oConn)
End Sub
' (SS,3/10/01) developed from Wrox ASP 3.0 page 349 '
' sets global gsADOError variable to "" of no error, else message '
Function CheckForADOErrors(objConn)
Dim objError
If objConn.Errors.Count > 0 Then
' loop through the errors
For Each objError in objConn.Errors
' errors with number 0 are informational
If objError.Number <> 0 Then
gsADOErrors = gsADOErrors + objError.Description + vbCrLf
CheckForADOErrors = True
End If
Next
Else
CheckForADOErrors = False
End If
End Function
' Replace in String AStr, all occurrences of AReplace with AWith '
Function ReplaceStr(AStr, AReplace, AWith)
Dim p, LStr
ReplaceStr = ""
LStr = AStr
Do While LStr <> ""
p = InStr(LStr, AReplace)
If p > 0 Then
ReplaceStr = ReplaceStr + Mid(LStr, 1, p - 1) + AWith
LStr = Mid(LStr, p + Len(AReplace), Len(LStr))
Else
ReplaceStr = ReplaceStr + LStr
LStr = ""
End if
Loop
End Function
' (SS,20/9/01) creates lookup combo from table
' (SS,17/12/01) added AOrderField parameter to allow ordering by other fields
' (SS,16/2/04) added ASelectName parameter which overrides AFieldName to give different name for select name
' (SS,17/2/04) added AExtras to add extra to the end e.g. disabled etc.
' (SS,19/2/04) added AWhereClause to do things like WHERE Country = "United Kingdom"
' (SS,21/5/04) if ATableName starts with "SELECT" then it is used as the full SQL
' (SS,25/11/04) added AValueField, if not blank then this Field will be used to get the Value for the combo instead of AFieldName
' (SS,16/7/09) replaced with newer version from csl/book/dbfunction.asp, with few minor mods
Function GetLookupCombo(ATableName, AFieldName, AOrderField, ADefaultValue, AShowBlank, ASelectName, AExtras, AWhereClause, AValueField)
Dim s, strFieldValue, LSelectName, LShowBlank, LBlankValue, LValueValue
LSelectName = AFieldName
If ASelectName <> "" Then LSelectName = ASelectName
' (SS,28/10/04) changed so that class="form" is not added if AExtras contains class=, to allow class to be overriden '
s = "<select name=""" + LSelectName + """"
If InStr(LCase(AExtras), "class=") = 0 Then
s = s + " class=""form"""
End If
If AExtras <> "" Then
s = s + " " + AExtras
End If
s = s + ">"
' (SS,18/5/04) added following to allow special blank value to be passed '
If VarType(AShowBlank) = vbBoolean Then
LShowBlank = AShowBlank
LBlankValue = ""
Else
LShowBlank = True
LBlankValue = AShowBlank
End If
If LShowBlank Then
If ADefaultValue = "" Then
s = s + "<option value = """" selected>" & LBlankValue & "</option>"
Else
s = s + "<option value = """">" & LBlankValue & "</option>"
End If
End If
If AOrderField = "" Then AOrderField = AFieldName
' (SS,21/5/04) if table name start with "SELECT " then use it has the SQL else build it from table name, where and order clauses '
Dim LSQL
If UCase(Left(ATableName, 7)) = "SELECT " Then
LSQL = ATableName
Else
LSQL = "SELECT * FROM " + ATableName + Iif(AWhereClause = "", "", " WHERE " + AWhereClause) + " ORDER BY " + AOrderField
End If
OpenQuery(LSQL)
Do While Not EndOfQuery
strFieldValue = GetQueryValue(AFieldName)
If AValueField = "" Then
LValueValue = strFieldValue
Else
LValueValue = GetQueryValue(AValueField)
End If
' (SS,5/10/04) replaced "+ strFieldValue +" with "& strFieldValue &"' later LValueValue
s = s + "<option value=""" & LValueValue & """"
' (SS,5/10/04) added Trim to following to ensure values are converted to strings for comparison '
If Trim(LValueValue) = Trim(ADefaultValue) Then s = s + " selected"
' (SS,16/7/09) replaced "+ strFieldValue +" with "& strFieldValue &"'
s = s + ">" & strFieldValue & "</option>" + Chr(13) + Chr(10) ' Chr(13) + Chr(10) to made HTML more readable
NextQueryRecord
Loop
CloseQuery
s = s + "</select>"
GetLookupCombo = s
End Function
' (SS,17/2/04) '
Function GetMonthCombo(ASelectName, ADefaultValue, ABlankValue)
Dim s, i, m
s = "<select name=""" + ASelectName + """ class=""form"">"
s = s + "<option>" & ABlankValue & "</option>"
For i = 1 to 12
m = Right("0" & i, 2)
If m = ADefaultValue Then
s = s + "<option selected>"
Else
s = s + "<option>"
End If
s = s + m + "</option>" + Chr(13) + Chr(10)
Next
s = s + "</select>"
GetMonthCombo = s
End Function
' (SS,17/2/04)
Function GetYearCombo(ASelectName, ADefaultValue, AStartYear, AEndYear, ABlankValue)
Dim s, i, y
s = "<select name=""" + ASelectName + """ class=""form"">"
s = s + "<option>" & ABlankValue & "</option>"
For i = AStartYear To AEndYear
y = Right("000" & i, 4)
If y = ADefaultValue Then
s = s + "<option selected>"
Else
s = s + "<option>"
End If
s = s + y + "</option>" + Chr(13) + Chr(10)
Next
s = s + "</select>"
GetYearCombo = s
End Function
' (SS,17/4/09)
Function IsWorkingDay(ADate)
IsWorkingDay = Weekday(ADate) <> vbSaturday And Weekday(ADate) <> vbSunday
End Function
' (SS,17/4/09)
Function NextWorkingDay(ADate)
Dim LDate
LDate = ADate
Do
LDate = LDate + 1
Loop While Not IsWorkingDay(LDate)
NextWorkingDay = LDate
End Function
' (SS,17/4/09)
Function GetDeliveryDateCombo(ASelectName, ADefaultValue, ANoOfDays)
Dim s, i, d, LStartDate
LStartDate = NextWorkingDay(Date)
' if past midday then start from next working day
If Hour(Now) >= 12 Then LStartDate = NextWorkingDay(LStartDate)
s = "<select name=""" + ASelectName + """ class=""form"">"
For i = 0 to ANoOfDays - 1
d = FormatDateTime(LStartDate, VbShortDate)
If d = ADefaultValue Then
s = s + "<option selected>"
Else
s = s + "<option>"
End If
s = s + d + "</option>" + Chr(13) + Chr(10)
LStartDate = NextWorkingDay(LStartDate)
Next
s = s + "</select>"
GetDeliveryDateCombo = s
End Function
' (SS,11/11/01) creates lookup combo from table, special version used to fill in values
' (SS,17/12/01) added AOrderField parameter to allow ordering by other fields
Function GetLookupFillCombo(ATableName, AFieldName1, AFieldName2, AFieldName3, AFieldName4, AOrderField, ADefaultValue, AShowBlank, AFunctionName)
Dim s, strFieldValue
s = "<select name=""u" + AFieldName1 + """ onchange=""" + AFunctionName + """>"
If AShowBlank Then
If ADefaultValue = "" Then
s = s + "<option selected></option>"
Else
s = s + "<option></option>"
End If
End If
If AOrderField = "" Then AOrderField = AFieldName1
OpenQuery("SELECT * FROM " + ATableName + " ORDER BY " + AOrderField)
Do While Not EndOfQuery
strFieldValue = GetQueryValue(AFieldName1)
If strFieldValue = ADefaultValue Then
s = s + "<option selected>"
Else
s = s + "<option>"
End If
s = s + strFieldValue
If AFieldName2 <> "" Then s = s & "; " & GetQueryValue(AFieldName2)
If AFieldName3 <> "" Then s = s & "; " & GetQueryValue(AFieldName3)
If AFieldName4 <> "" Then s = s & "; " & GetQueryValue(AFieldName4)
s = s + "</option>" + Chr(13) + Chr(10) ' Chr(13) + Chr(10) to made HTML more readable
NextQueryRecord
Loop
CloseQuery
s = s + "</select>"
GetLookupFillCombo = s
End Function
' (SS,19/10/01) yes no combo
Function GetYesNoCombo(AFieldName, ADefaultValue)
Dim s
s = "<select name=""" + AFieldName + """>" + Chr(13) + Chr(10)
If ADefaultValue = "Yes" Then
s = s + "<option>No</option>" + Chr(13) + Chr(10)
s = s + "<option Selected>Yes</option>" + Chr(13) + Chr(10)
Else
s = s + "<option Selected>No</option>" + Chr(13) + Chr(10)
s = s + "<option>Yes</option>" + Chr(13) + Chr(10)
End If
s = s + "</select>" + Chr(13) + Chr(10)
GetYesNoCombo = s
End Function
' (SS,31/5/070 returns html combo for given list, items should be separated by new lines
Function GetListCombo(ASelectName, AList, ADefaultValue, AShowBlank)
Dim LList, LShowBlank, LBlankValue, s, i, LValue
LList = Split(AList, Chr(13))
' (SS,18/5/04) added following to allow special blank value to be passed '
If VarType(AShowBlank) = vbBoolean Then
LShowBlank = AShowBlank
LBlankValue = ""
Else
LShowBlank = True
LBlankValue = AShowBlank
End If
s = "<select name=""" + ASelectName + """ class=""form"">"
If LShowBlank Then
If ADefaultValue = "" Then
s = s + "<option value = """" selected>" & LBlankValue & "</option>"
Else
s = s + "<option value = """">" & LBlankValue & "</option>"
End If
End If
For i = 0 To UBound(LList)
LValue = TrimAll(LList(i)) ' TrimAll used instead of Trim to also remove leading and trailing newline/carriage returns
s = s + "<option value=""" & LValue & """"
If LValue = ADefaultValue Then s = s + " selected"
s = s + ">" & LValue & "</option>" + Chr(13) + Chr(10) ' Chr(13) + Chr(10) to made HTML more readable
Next
s = s + "</select>"
GetListCombo = s
End Function
' (SS,15/10/01) get access rights
Function GetAccessRights(AUserID, APassword)
GetAccessRights = "" ' default to no access rights
OpenDatabase("Main")
OpenQuery("SELECT * FROM Users WHERE UserID='" + AUserID + "' AND Password='" + APassword + "'")
If Not EndOfQuery Then
GetAccessRights = GetQueryValue("Rights")
End If
CloseQuery
CloseDatabase
End Function
' (SS,15/10/01) get access rights
Function GetAccessLevel(AAccessRights)
GetAccessLevel = 0 ' default to minimum access level
OpenDatabase("Main")
OpenQuery("SELECT * FROM Rights WHERE Rights='" + AAccessRights + "'")
If Not EndOfQuery Then
GetAccessLevel = GetQueryValue("AccessLevel")
End If
CloseQuery
CloseDatabase
End Function
' (SS,25/10/01) change password
Function ChangePassword(AUserID, APassword, ANewPassword)
ChangePassword = False ' default to not successful
OpenDatabase("Main")
OpenTableRecordForEdit "Users", "UserID='" + AUserID + "' AND Password='" + APassword + "'"
If Not EndOfQuery Then
ChangePassword = True
PutFieldValue "Password", ANewPassword
PostRecord
End If
CloseTable
CloseDatabase
End Function
Function Format2dp(ANumber)
If IsNull(ANumber) Then
Format2dp = Null
Else
Format2dp = FormatNumber(ANumber, 2, vbTrue, vbFalse, vbTrue)
End If
End Function
' (SS,11/12/08) same as Format2dp but with no commas (i.e. grouping), always returns a string, also unlike Format2dp it returns 0.00 for null
Function Format2dpnc(ANumber)
If IsNull(ANumber) Then
Format2dpnc = "0.00"
Else
Format2dpnc = FormatNumber(ANumber, 2, vbTrue, vbFalse, vbFalse)
End If
End Function
' (SS,19/9/06) rounds the number to 2dp (as Format2dp does) without adding commas, returns Double or Null
Function Round2dp(ANumber)
If IsNull(ANumber) Then
Round2dp = Null
Else
Round2dp = CDbl(FormatNumber(ANumber, 2, vbTrue, vbFalse, vbFalse)) ' (SS,28/5/07) added CDbl because FormatNumber returns a string
End If
End Function
' Immediate If function (SS,30/4/03)
Function IIf(a, b, c)
If a Then IIf = b Else IIf = c
End Function
' (SS,21/2/02) returns null as zero, useful for calculations where null needs to be treated as zero
Function NZ(ANumber)
If IsNull(ANumber) Then
NZ = 0
Else
NZ = ENKN(ANumber) ' (SS,22/5/07) added ENKN to ensure a proper number (especially MySQL Decimal)
End If
End Function
' (SS,14/9/06) returns null as "" (i.e. Null as Blank)
Function NB(AValue)
If IsNull(AValue) Then
NB = ""
Else
' (SS,22/5/07) replaced NB = AValue with following to ensure non string is converted to a string
If VarType(AValue) = VBString Then
NB = AValue
Else
NB = CStr(AValue)
End If
End If
End Function
' (SS,18/5/07) converts given value to a double, if null then it returns zero
Function NZD(ANumber)
NZD = CDbl(NZ(ANumber))
End Function
' (SS,18/5/07) converts given value to a long, if null then it returns zero
Function NZL(ANumber)
NZL = CLng(NZ(ANumber))
End Function
' (SS,22/5/07) ensure number but keep null
' useful to keep original number in it's own type, but converts MySQL decimal to CDbl
Function ENKN(ANumber)
If IsNull(ANumber) Then
ENKN = Null
Else
If IsNumeric(ANumber) Then
ENKN = ANumber
Else ' else convert to double, e.g. it could be VarType 14 (MySQL Decimal), which VB thinks is not a number
ENKN = CDbl(ANumber)
End If
End If
End Function
' (SS,24/9/03)
' returns number followed by word
' e.g. PluralString(1, "product", "products") returns "1 product"
' for any other value it returns "x products"
Function PluralString(ANumber, AStringForOne, AStringForOther)
If ANumber = 1 Then
PluralString = ANumber & " " & AStringForOne
Else
PluralString = ANumber & " " & AStringForOther
End If
End Function
' cleans the string, replacing quote and apostrophe with escape char
' also converts \ with \\ so it doesn't cause problems
' with SQL statements, works with MySQL, might not with other SQL Servers
' see MySQL manual page 432 (6.1.1.1)
' (SS,14/7/09) added code for ASCII control codes etc
Function CleanSQLStr(AValue)
Dim LNewValue
If IsNull(AValue) Then AValue = "" ' (SS,27/6/05) added this to prevent invalid use of null error '
LNewValue = AValue
LNewValue = Replace(LNewValue, "\", "\\")
LNewValue = Replace(LNewValue, "'", "\'")
LNewValue = Replace(LNewValue, """", "\""")
' (SS,14/7/09) added following to cope with ASCII control codes (also added to CleanSQLStr in itpssutils.pas)
' see http://dev.mysql.com/doc/refman/5.0/en/string-syntax.html for escape character details
LNewValue = Replace(LNewValue, Chr(0), "\0") ' An ASCII NUL (0x00) character.
LNewValue = Replace(LNewValue, Chr(8), "\b") ' A backspace character.
LNewValue = Replace(LNewValue, Chr(9), "\t") ' A tab character.
LNewValue = Replace(LNewValue, Chr(10), "\n") ' A newline (linefeed) character.
LNewValue = Replace(LNewValue, Chr(13), "\r") ' A carriage return character.
LNewValue = Replace(LNewValue, Chr(26), "\Z") ' ASCII 26 (Control-Z).
' (SS,21/7/09) removed following because they only work for wildcard search using LIKE,
' created new function called CleanSQLStrForLike which will allow search literal % and _ in LIKE query
' problem occurred when user with underscore in email tried logging in
' it tried to search for \_ and obviously failed
' LNewValue = Replace(LNewValue, "%", "\%") ' A �%� character.
' LNewValue = Replace(LNewValue, "_", "\_") ' A �_� character.
CleanSQLStr = LNewValue
End Function
' (SS,21/7/09) calls CleanSQLStr and then converts the wildcard characters % and _ to literals
Function CleanSQLStrForLike(AValue)
Dim LNewValue
LNewValue = CleanSQLStr(AValue)
' convert the % and _ wildcard characters to literals
LNewValue = Replace(LNewValue, "%", "\%") ' A �%� character.
LNewValue = Replace(LNewValue, "_", "\_") ' A �_� character.
CleanSQLStrForLike = LNewValue
End Function
' (SS,26/6/04) '
' converts newlines to breaks for HTML '
' (SS,16/7/09) improved version from csl/book/dbfunction.asp
Function ConvertNewlinesToHTML(AString)
Dim LNewString
If IsNull(AString) Then
ConvertNewlinesToHTML = ""
Exit Function
End If
LNewString = Replace(AString, Chr(13) & Chr(10), "<br>")
' newlines may also be as either Chr(10) & Chr(13), or either Chr(10) or Chr(13) '
' so all methods are tried '
LNewString = Replace(LNewString, Chr(10) & Chr(13), "<br>")
LNewString = Replace(LNewString, Chr(13), "<br>")
LNewString = Replace(LNewString, Chr(10), "<br>")
ConvertNewlinesToHTML = LNewString
End Function
' (SS,25/11/04) converts newlines to \n for Javascript alers '
' (SS,16/7/09) added from csl/book/dbfunction.asp
Function ConvertNewlinesForJavascript(AString)
Const JAVASCRIPT_NEWLINE = "\n"
Dim LNewString
If IsNull(AString) Then
ConvertNewlinesForJavascript = ""
Exit Function
End If
LNewString = Replace(AString, Chr(13) & Chr(10), JAVASCRIPT_NEWLINE)
' newlines may also be as either Chr(10) & Chr(13), or either Chr(10) or Chr(13) '
' so all methods are tried '
LNewString = Replace(LNewString, Chr(10) & Chr(13), JAVASCRIPT_NEWLINE)
LNewString = Replace(LNewString, Chr(13), JAVASCRIPT_NEWLINE)
LNewString = Replace(LNewString, Chr(10), JAVASCRIPT_NEWLINE)
ConvertNewlinesForJavascript = LNewString
End Function
' (SS,5/7/07) encode string to HTML, e.g. characters like " become "e; etc
' it actually calls Server.HTMLEncode
' (SS,22/4/09) added check for null to prevent "Type mismatch" error
Function HTMLEncode(AValue)
If IsNull(AValue) Then
HTMLEncode = ""
Else
HTMLEncode = Server.HTMLEncode(AValue)
End If
End Function
' (SS,5/6/09)
Function URLEncode(AValue)
If IsNull(AValue) Then
URLEncode = ""
Else
URLEncode = Server.URLEncode(AValue)
End If
End Function
' (SS,18/11/08) ensures string can be a valid javascript single quoted string literal by prefixing single quotes (') with \
Function SingleQuotedJavascriptString(AValue)
SingleQuotedJavascriptString = Replace(AValue, "'", "\'")
End Function
' (SS,18/11/08) ensures string can be a valid javascript double quoted string literal by prefixing double quotes (") with \
Function DoubleQuotedJavascriptString(AValue)
DoubleQuotedJavascriptString = Replace(AValue, """", "\""")
End Function
' 6/10/03 runs a query and returns the value from given field, null is returned if no records found
Function GetValueFromQuery(AFieldName, ASQL)
If gbDebugOn Then DebugLog("GetValueFromQuery called with " & AFieldName & ", " & ASQL)
Dim LoRS
Set LoRS = oConn.Execute(ASQL, nRecs, adCmdText)
If LoRS.Eof Then
GetValueFromQuery = Null
Else
GetValueFromQuery = LoRS(AFieldName)
End If
LoRS.Close
Set LoRS = Nothing
End Function
' 6/10/03 gets value from settings table, null is returned if not found '
' TypeID is APP, EntryID is MAIN '
' (SS,14/6/07) renamed from GetAppSetting to GetSetting, also removed TypeID = 'APP' AND EntryID = 'MAIN'
' (SS,16/7/09) renamed from GetSetting to GetSettingFromField and added AValueField parameter
' (SS,21/7/09) bug fix GetSetting = corrected to GetSettingFromField
' (SS,27/7/09) fixed bug, "SELECT FieldValue" changed to "SELECT *"
Function GetSettingFromField(AGroupName, AFieldName, AValueField)
If gbDebugOn Then DebugLog("GetSetting called with " & AGroupName & ", " & AFieldName & ", " & AValueField)
Dim LoRS
Set LoRS = oConn.Execute("SELECT * FROM Settings WHERE GroupName = '" & AGroupName & "' AND FieldName = '" & AFieldName & "'", nRecs, adCmdText)
If LoRS.Eof Then
GetSettingFromField = Null
Else
GetSettingFromField = LoRS(AValueField) ' (SS,16/7/09) replaced "FieldValue" with AValueField
End If
LoRS.Close
Set LoRS = Nothing
End Function
' (SS,16/7/09) this replaces previous GetSetting which has been renamed to GetSettingFromField, this calls it
Function GetSetting(AGroupName, AFieldName)
GetSetting = GetSettingFromField(AGroupName, AFieldName, "FieldValue")
End Function
' (SS,16/7/09) this one returns value from MemoValue field instead of FieldValue
Function GetSettingMemo(AGroupName, AFieldName)
GetSettingMemo = GetSettingFromField(AGroupName, AFieldName, "MemoValue")
End Function
' 6/10/03, save settings to settings table, the Group and Field must already exist '
' TypeID is APP, EntryID is MAIN '
' (SS,14/6/07) renamed from SetAppSetting to SetSetting, also removed TypeID = 'APP' AND EntryID = 'MAIN'
Sub SetSetting(AGroupName, AFieldName, AFieldValue)
If gbDebugOn Then DebugLog("SetSetting called with " & AGroupName & ", " & AFieldName)
oConn.Execute("UPDATE settings SET FieldValue = '" & CleanSQLStr(AFieldValue) & "' WHERE GroupName = '" & AGroupName & "' AND FieldName = '" & AFieldName & "'")
End Sub
' (SS,14/11/07) add new setting
Sub AddSetting(AGroupName, AFieldName, AFieldValue)
If gbDebugOn Then DebugLog("AddSetting called with " & AGroupName & ", " & AFieldName)
oConn.Execute("INSERT INTO settings SET GroupName = '" & AGroupName & "', FieldName = '" & AFieldName & "', FieldValue = '" & CleanSQLStr(AFieldValue) & "'")
End Sub
' shows the debug message, perhaps in the future add it to a log '
Sub DebugLog(ADebugMessage)
Response.Write "*** " & ADebugMessage & " ***<br><br>"
End Sub
' (SS,13/8/04)
Sub DebugOn
gbDebugOn = True
End Sub
' (SS,13/8/04)
Sub DebugOff
gbDebugOn = False
End Sub
' (SS,18/9/06) outputs given debug message straight to browser
Sub DebugMsg(ADebugMessage)
Response.Write "###" & ADebugMessage & "###<br>"
End Sub
' (SS,21/5/07)
Sub SetDebugLevel(AValue)
gnDebugLevel = AValue
End Sub
' (SS,26/5/07)
Sub ShowOpenDatabaseCount
DebugLog("No of Database Opens: " & gnOpenDatabaseCount)
DebugLog("No of Database Closes: " & gnCloseDatabaseCount)
DebugLog("No of Query Opens: " & gnOpenQueryCount)
DebugLog("No of Query Closes: " & gnCloseQueryCount)
DebugLog("No of Exec Queries: " & gnExecQueryCount)
End Sub
' (SS,12/8/05) converts integer to hex up to given amount of digits
' e.g. IntToHex(15, 2) returns "0F"
' IntToHex(15, 0) returns "F"
' similar to Delphi's IntToHex
Function IntToHex(Value, Digits)
Dim Result
Result = Hex(Value)
' if there are less digits that requested then left pad with zeros
If Len(Result) < Digits Then Result = Right(String(Digits, "0") + Result, Digits)
IntToHex = Result
End Function
' (SS,12/8/05) converts given hex string to integer
Function HexStrToInt(Value)
HexStrToInt = CLng("&H" & Value)
End Function
' (SS,12/8/05 converted from Delphi) encrypts a string using a simple method
' returns in hex format, can be used as a parameter
' when calling other application, because it only has
' 0123456789ABCDEF characters size will be twice as long
' as original string
' inverts each byte using XOR and reverses all characters
Function SimpleEncryptString(AStr)
Dim i, n, s, Result
Result = ""
n = Len(AStr)
For i = n To 1 Step -1 ' go through each character backwards
s = IntToHex(Asc(Mid(AStr, i, 1)) Xor &HFF, 2) ' convert to byte, invert and convert to decimal hex
Result = Result + Mid(s, 2, 1) + Mid(s, 1, 1) ' add the two nibbles in reverse
Next
SimpleEncryptString = Result
End Function
' (SS,12/8/05 converted from Delphi) opposite of SimpleEncryptString above
Function SimpleDecryptString(AStr)
Dim i, n, s
Result = ""
n = Len(AStr) \ 2
For i = n To 1 Step -1
s = Mid(AStr, i * 2, 1) + Mid(AStr, i * 2 - 1, 1) ' get the byte as hex with nibbles in reverse
Result = Result + Chr(HexStrToInt(s) Xor &HFF) ' convert the hex string to integer invert and change to char and add to result
Next
SimpleDecryptString = Result
End Function
' (SS,12/8/05) returns boolean as integer, suitable for MySQL, True returns 1, False returns 0
' (SS,1/6/07) renamed from BoolAsInt
Function BoolToInt(AValue)
If AValue then
BoolToInt = 1 ' i.e. true value
Else
BoolToInt = 0 ' i.e. false value
End If
End Function
' (SS,12/8/05) returns integer as boolean, suitable for MySQL, 1 returns True, 0 returns False
' (SS,1/6/07) renamed from IntAsBool
Function IntToBool(AValue)
' (SS,9/7/09) added IsNull(AValue) to treat null as false also
If IsNull(AValue) Or AValue = 0 then
IntToBool = False
Else
IntToBool = True
End If
End Function
' (SS,26/5/07) converts string to boolean
' true, yes, 1, -1 are retreated as True, others including Null are false, AValue must be a string or null
Function StrToBool(AValue)
Dim LResult, LValue
LResult = False
If Not IsNull(AValue) Then
LValue = LCase(AValue)
If LValue = "true" Or LValue = "yes" Or LValue = "1" Or LValue = "-1" Then LResult = True
End If
StrToBool = LResult
End Function
' (SS,28/5/07) returns true if give function exists,
' doesn't work with subs or functions with 30 parameters,
' given function will get called if it has 30 parameters
' there's no built in function in VB that does this
' required mainly to see if functions in templates exist (e.g. Shopping app)
Function FunctionExists(AFunctionName)
On Error Resume Next ' to trap error in following Eval function
' call the function with 20 parameters, assuming a function with 20 parameters is unlikely to occur
Eval(AFunctionName + "(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30)")
' err number will be 450 (Wrong number of arguments or invalid property assignment) if function exists
' err number will be 13 (Type mismatch) if function doesn't exist
FunctionExists = Err.Number = 450
'DebugMsg(Err.Number)
'DebugMsg(Err.Description)
End Function
' (SS,15/12/06) following converts given text into HTML W3C ISO etc standard
' currently just the 4 quote characters are converted
' (SS,30/5/07) was originally called ConvertToISONamedHTML
Function ToNamedHTML(AValue)
' following taken from http://theorem.ca/~mvcorks/code/charsets/named-entities.html
'145 U+2018 ‘ � ‘ � left single quotation mark
'146 U+2019 ’ � ’ � right single quotation mark
'147 U+201C “ � “ � left double quotation mark
'148 U+201D ” � ” � right double quotation mark
Dim LValue
LValue = AValue
LValue = Replace(LValue, """", """)
LValue = Replace(LValue, Chr(145), "‘")
LValue = Replace(LValue, Chr(146), "’")
LValue = Replace(LValue, Chr(147), "“")
LValue = Replace(LValue, Chr(148), "”")
ToNamedHTML = LValue
End Function
' (SS,4/6/07) the ASP Trim function only removes leading and trailing spaces
' created TrimAll which removes spaces and control characters (i.e. the same as Delphi's Trim)
' this was required because at times I also want to remove newlines and carriage returns characters
' if string is null then it's converted to a blank string ""
' (SS,23/6/09) code moved to TrimChars which handles a range of chars
' this routine now calls TrimChars for characters between 0 and 32
Function TrimAll(AString)
TrimAll = TrimChars(AString, Chr(0), Chr(32))
End Function
' (SS,23/6/09) trims given char from string
Function TrimChar(AString, ACharToTrim)
TrimChar = TrimChars(AString, ACharToTrim, ACharToTrim)
End Function
' (SS,23/6/09) trims given chars between AMinChar and AMaxChar from start and end of string
' code taken from TrimAll and modified to trim a range of chars
Function TrimChars(AString, AMinChar, AMaxChar)
Dim LString, LLen, LStart, LEnd, i
LLen = Len(AString)
LString = NB(AString)
If LString = "" Then
TrimChars = ""
Exit Function
End If
For i = 1 To LLen
If (Mid(LString, i, 1)) < AMinChar Or (Mid(LString, i, 1)) > AMaxChar Then
Exit For
End if
Next
LStart = i
LEnd = LLen
For i = LLen To 1 Step -1
If (Mid(LString, i, 1)) < AMinChar Or (Mid(LString, i, 1)) > AMaxChar Then
Exit For
End if
Next
LEnd = i
If LEnd < LStart Then
TrimChars = ""
Else
TrimChars = Mid(LString, LStart, LEnd - LStart + 1)
End If
End Function
' (SS,21/10/08) returns true if first character of given string is alphabetic i.e. A-Z or a-z
Function IsCharAlpha(AChar)
Dim LCharCode
If AChar = "" Then
LCharCode = 0
Else
LCharCode = Asc(UCase(Left(AChar, 1)))
End If
IsCharAlpha = (LCharCode >= 65 And LCharCode <= 90) ' 65 is "A", 90 is "Z"
End Function
' (SS,21/10/08) returns true if first character of given string is a number i.e. 0-9
Function IsCharNumeric(AChar)
Dim LCharCode
If AChar = "" Then
LCharCode = 0
Else
LCharCode = Asc(UCase(Left(AChar, 1)))
End If
IsCharNumeric = (LCharCode >= 48 And LCharCode <= 57) ' 48 is "0", 57 is "9"
End Function
' (SS,21/10/08) strips all spaces from given string
Function StripSpaces(AString)
Dim LString, i, LLen, Result, LChar
LString = Trim(AString)
LLen = Len(LString)
Result = ""
For i = 1 To LLen
LChar = Mid(LString, i, 1)
If LChar <> " " Then Result = Result + LChar
Next
StripSpaces = Result
End Function
' (SS,21/10/08) splits a UK postcode into its 4 parts, and returns true if it's valid
Function SplitPostcode(APostcode, ByRef APrefix, ByRef AOutwardNo, ByRef AInwardNo, ByRef ASuffix)
Dim LPostcode, LPostcodePrefix, LPostcodeNo, LChar, LIndexS, LIndexE, LAllowedLength
APrefix = ""
AOutwardNo = ""
AInwardNo = ""
ASuffix = ""
LAllowedLength = 5 ' minimum allowed length is 5, incremented later if prefix is 2 chars and/or outwardno is 2 chars
LPostcode = StripSpaces(UCase(APostcode))
' determine prefix
LIndexS = 1
LChar = Mid(LPostcode, LIndexS, 1)
If IsCharAlpha(LChar) Then
LIndexS = LIndexS + 1
APrefix = LChar
LChar = Mid(LPostcode, LIndexS, 1)
If IsCharAlpha(LChar) Then
LIndexS = LIndexS + 1
LAllowedLength = LAllowedLength + 1
APrefix = APrefix + LChar
End If
End If
' determine suffix, only if there is a prefix, not already at end
If APrefix <> "" Then
LIndexE = Len(LPostcode)
If LIndexS < LIndexE Then
LChar = Mid(LPostcode, LIndexE, 1)
If IsCharAlpha(LChar) Then
LIndexE = LIndexE - 1
ASuffix = LChar
LChar = Mid(LPostcode, LIndexE, 1)
If IsCharAlpha(LChar) Then
LIndexE = LIndexE - 1
ASuffix = LChar + ASuffix ' because we're going backwards
End If
End If
End If
End If
' determine inwardno, only if there is a suffix and end index points to a valid char
If ASuffix <> "" And LIndexE <> 0 Then
LChar = Mid(LPostcode, LIndexE, 1)
If IsCharNumeric(LChar) Then
LIndexE = LIndexE - 1
AInwardNo = LChar
End If
End If
' determine outwardno, only if there is a prefix
If APrefix <> "" Then
LChar = Mid(LPostcode, LIndexS, 1)
If IsCharNumeric(LChar) Then
LIndexS = LIndexS + 1
AOutwardNo = LChar
LChar = Mid(LPostcode, LIndexS, 1)
' 2nd number could be the inwardno if space has been missed
' therefore we only add if the char hasn't already been used as inwardno
' i.e. LIndexE >= LIndexS
If IsCharNumeric(LChar) And LIndexE >= LIndexS Then
LAllowedLength = LAllowedLength + 1
AOutwardNo = AOutwardNo + LChar
End If
End If
End If
' return true if it's a full valid postcode, i.e. has all 4 parts, with prefix of 1 or 2 characters, suffix of 2 characters, outwardno of 1 or 2 and inwardno of 1 character
SplitPostcode = LAllowedLength = Len(LPostcode) And (Len(APrefix) = 1 Or Len(APrefix) = 2) And Len(ASuffix) = 2 And (Len(AOutwardNo) = 1 Or Len(AOutwardNo) = 2) And Len(AInwardNo) = 1
End Function
' (SS,17/4/09) converts given UK date in string to MySQL format for use in SQL queries '
Function ConvertUKToMySQLDate(ADate)
If IsNull(ADate) or ADate = "" Then
' (SS,8/5/09) changed to return Null as a string so it can used directly in SQL statement
ConvertUKToMySQLDate = "Null" ' because blank date can only be saved as Null not empty string '
Else
' (SS,8/5/09) removed the two '& "-"' so it can be assigned without quotes, also added "0" for leading zero
ConvertUKToMySQLDate = DatePart("yyyy", ADate) & Right("0" & DatePart("m", ADate), 2) & Right("0" & DatePart("d", ADate), 2)
End If
End Function
' (SS,28/5/09) returns true if given string is a URL
Function IsURL(AURL)
Dim LStr
LStr = Trim(LCase(AURL))
IsURL = Left(LStr, 5) = "http:" Or Left(LStr, 6) = "https:" Or Left(LStr, 4) = "www."
End Function
' (SS,29/8/04) replaces new line characters with <br> for HTML formatting
' (SS,29/5/09) moved here from apputils.asp
' it actually does the same as Function ConvertNewlinesToHTML
Function ReplaceNewLinesWithBR(AValue)
ReplaceNewLinesWithBR = ReplaceStr(AValue, Chr(13) + Chr(10), "<br>")
End Function
' (SS,4/6/09) removes newlines from given string
Function RemoveNewLines(AValue)
RemoveNewLines = ReplaceStr(AValue, Chr(13) + Chr(10), "")
End Function
' (SS,12/6/09) returns random integer between the two given numbers
Function RandomInteger(AMin, AMax)
Randomize
RandomInteger = Int((AMax - AMin + 1) * Rnd + AMin)
End Function
' (SS,23/6/09) from siteutils used to determine the clean page name in URL containing &page=
' replaces all non alpha and number characters with '-', ignores quotes and converts to lowercase
' also trims leading and trailing dashes
Function CleanPageName(APageName)
Dim i, LChar, Result
Result = ""
For i = 1 To Len(APageName)
LChar = Mid(APageName, i, 1)
If IsCharAlpha(LChar) Or IsCharNumeric(LChar) Then
Result = Result + LChar
' (SS,23/6/09) ignore quotes
ElseIf LChar <> "'" And LChar <> """" Then
Result = Result + "-"
End If
Next
' trim leading and trailing dashes
Result = TrimChar(Result, "-")
' make sure 2 and 3 dash are just one dash
Result = Replace(Result, "--", "-")
Result = Replace(Result, "--", "-")
Result = LCase(Result)
CleanPageName = Result
End Function
' (SS,3/11/04) returns true if file exists '
' (SS,16/7/09) copied from csl/book/dbfunction.asp
Function FileExists(AFileName)
Dim objFSO
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
FileExists = objFSO.FileExists(AFileName)
Set objFSO = Nothing
End Function
' (SS,3/11/04) deletes given file if it exists '' (SS,16/7/09) copied from original csl/book/dbfunction.asp
' (SS,16/7/09) copied from csl/book/dbfunction.asp
Sub DeleteFile(AFileName)
Dim objFSO
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
If FileExists(AFileName) Then objFSO.DeleteFile(AFileName)
Set objFSO = Nothing
End Sub
' (SS,4/11/04) deletes given file if it exists '
' (SS,16/7/09) copied from csl/book/dbfunction.asp
Sub CopyFile(ASourceFileName, ADestinFileName)
Dim objFSO
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
If FileExists(ASourceFileName) Then objFSO.CopyFile ASourceFileName, ADestinFileName
Set objFSO = Nothing
End Sub
' (SS,7/10/14)
Sub DisableCache
If Session("CacheOn") <> True Then ' i.e. not False and ""
Response.CacheControl = "no-cache"
Response.AddHeader "Pragma", "no-cache"
Response.Expires = -1
End If
End Sub
%>