Check A Date Is Valid

Posted on March 4th, 2009 in ASP by admin

This example displays a form on a page . The user then enters a date , if the date is invalid then a message will be displayed on the screen . Examples of valid dates are as follows

12-12-09

12/12/09
<form method =”post” action=”<%= Request.ServerVariables(”SCRIPT_NAME”) %>”>
<input type =”text” name=”date”><br>
<input type=”submit”>
</form>
<%

Dim strDate , blnValid , dteDate
If Request.ServerVariables(”CONTENT_LENGTH”) <>0 Then
strDate = Trim(Request.Form(”date”))
‘check and see if the user entry is a date
If isDate(strDate) Then
dteDate = CDate(strDate)
’set variable to true
blnValid = True
Else
‘display invalid date message
Response.Write “You entered an invalid date”
End If
‘displayvalid date message
If blnValid = True Then
Response.Write “The date you entered was ” & dteDate
End If
End If
%>

Text Counter

Posted on March 4th, 2009 in ASP by admin

Now create a folder called counters or you can call it something else if you want just remember and change the code above . Now create a text file called counter1.txt and enter whatever you wish to start the count at.

<%@ Language=VBScript %>
<%
Dim fso
Set fso = Server.CreateObject(”Scripting.FileSystemObject”)
//this is the path to your counter1.txt file
counter = Server.MapPath(”/counters”) & “\counter1.txt”
//open the text file
Set InStream = fso.OpenTextFile(counter , 1, False)
oldCount = Trim(InStream.ReadLine)
//increment the oldcount by 1 and store in newcount
newCount = oldCount + 1
Set OutStream = fso.CreateTextFile(counter, True)
//copy newcount to the text file
OutStream.WriteLine(newCount)
%>

Now you would place the folllowing where you want the counter to appear on your site

<%= newCount%>

Saving a CSV file to a database

Posted on March 4th, 2009 in ASP by admin

Create a database in access called stock.mdb .

Now create a table like in the image in figure 1 and save this as stock

stock database

stock database

Now ensure that the last price , change , open price , daily high , daily low fields all have the field size set as Double , this is because the data is fractional and if you use the default Field size for a Number data type in Access this will give you a Long Integer and you will lose some of the data.

<!– #include file =”adovbs.inc”–>
<%

‘declare variables for the database
Dim objConn , objRS
‘create instance of the Connection object
Set objConn = Server.CreateObject(”ADODB.Connection”)
‘open our stock database
objConn.Open “DBQ=” & Server.MapPath(”stock.mdb”) & “;Driver={Microsoft Access Driver (*.mdb)}”
‘create instance of the recordset object
Set objRS = Server.CreateObject(”ADODB.Recordset”)
‘our SQL statement
strSQL = “SELECT * FROM stock”
‘open the database
objRS.Open strSQL, objConn , adOpenStatic , adLockOptimistic
‘declare our variables for the file handling
Dim objFSO , strURL , objFile
‘create an instance of the file system object
Set objFSO = Server.CreateObject(”Scripting.FileSystemObject”)
‘this is the csv file downloaded from yahoo
strURL = Server.MapPath(”quotes.csv”)
‘open the file
Set objFile = objFSO.OpenTextFile(strURL)
‘while we are not at the end of the file
Do While Not objFile.AtEndOfStream
’store the contents of the file in strText
strText = objFile.readLine
’split the strText
arrText = split(strText, “,”, 9)
‘add our new records
objRS.AddNew
objRS(”symbol”) = arrText(0)
objRS(”last price”) = arrText(1)
objRS(”date”) = arrText(2)
objRS(”time”) = arrText(3)
objRS(”change”) = arrText(4)
objRS(”open price”) = arrText(5)
objRS(”daily high”) = arrText(6)
objRS(”daily low”) = arrText(7)
objRS(”volume”) = arrText(8)
‘update the database
objRS.Update
Loop
‘close and destroy objects
objRS.Close
objFile.Close
Set objRS = nothing
Set objConn = nothing
Set objFile = Nothing
Set objFSO = Nothing
%>

More accurate text counter

Posted on March 4th, 2009 in ASP by admin

This is a more accurate counter than our previous example , in this example the count only gets incremented once per session . This means your count will be more accurate if you want it to be that is.

Create a txt file and put your starting number in it . Then copy the code snippet below where you want the counter to display on your page.

In the example below the counter.txt is placed in the same directory as this page , if you put the txt file elsewhere remember and alter the Server.MapPath part of the code.

Visitor number

<%
‘create a FileSystemObject
Set objFSO = CreateObject(”Scripting.FileSystemObject”)
‘this is the path to our counter file
CounterFile = Server.MapPath(”counter.txt”)
Set objCount = objFSO.OpenTextFile(CounterFile)
visitorcount = CLng(objCount.ReadLine)
if Session(”visitorcount”) = “” then
Session(”visitorcount”) = visitorcount
visitorcount = visitorcount + 1
objCount.close
Set objCount = objFSO.CreateTextFile(”counter.txt”, True)
objCount.WriteLine(visitorcount)
end if
objCount.Close
‘display count
Response.Write visitorcount
%>

Next Page »