| | |
Will this work
Please support our ASP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Oct 2007
Posts: 48
Reputation:
Solved Threads: 0
Hi I am experimenting with VBScript and SQL and am unsure if the following code will actually work. I do not have access to IIS at present so I am unable to test it. I would be greatfull if anyone could just take a brief look at the code to see if the syntax is correct. Thanks
ASP Syntax (Toggle Plain Text)
<%@Language = "VBScript" %> <%Option Explicit%> <!--#include virtual="/advobs.inc"--> <% Dim newDate Dim newTime Dim newMessage Dim objConn Dim sqlInsert Dim objRecordSet Set objConn = Server.CreatObject("ADODB.Connection") objConn.ConnectionString="DRIVER={Microsoft Access Driver (*.mdb)};"&_"DBQ=C:/inetpub/wwwroot/feedBack.mdb" objConn.Open newDate = Date() newTime = Time() newMessage = request.form("strMessage") if newMessage <> "" then sqlInsert="INSERT INTO tblMessages(date,time,message) VALUES(newDate,newTime,newMessage)" end if Set objRecordSet=Server.CreateObject("ADODB.Recordset") objRecordSet.Open "tblMessages", objConn,,,adCmdTable Do while Not objRecordSet.EOF Response.Write"<B>" & objRs ("time") & "</B><BR>" Response.Write objRs("date") & "<BR>" Response.Write objRs ("message") & "<BR>" Response.Write "<p><hr></p>" objRecordSet.MoveNext Loop objRecordSet.Close Set objRecordSet = Nothing objConn.Close Set objConn = Nothing
•
•
Join Date: Oct 2007
Posts: 48
Reputation:
Solved Threads: 0
sorry the above code needs to be amended for the loop to read
ASP Syntax (Toggle Plain Text)
Do while Not objRecordSet.EOF Response.Write"<B>" & objRecordSet ("time") & "</B><BR>" Response.Write objRecordSet("date") & "<BR>" Response.Write objRecordSet ("message") & "<BR>" Response.Write "<p><hr></p>" objRecordSet.MoveNext Loop
•
•
Join Date: Sep 2007
Posts: 1,080
Reputation:
Solved Threads: 68
Here is the edited code and the reasons why:
ASP Syntax (Toggle Plain Text)
<%@Language = "VBScript" %> <%Option Explicit%> <!--#include virtual="/advobs.inc"--> <% Dim newDate Dim newTime Dim newMessage Dim objConn Dim sqlInsert Dim objRecordSet ' Added below for SQL statement below Dim strSQL Set objConn = Server.CreatObject("ADODB.Connection") objConn.ConnectionString="DRIVER={Microsoft Access Driver (*.mdb)};"&_"DBQ=C:/inetpub/wwwroot/feedBack.mdb" objConn.Open 'newDate = Date() 'newTime = Time() 'Not needed as below. If this doesn't work (the current_timestamp), you can always use the following line and substitute it in below: 'newDateTime = now() newMessage = Trim(Request.Form("strMessage")) ' You should always trim variables coming in to lose beginning or trailing white spaces if newMessage <> "" then sqlInsert="INSERT INTO tblMessages(datecreated,message) VALUES(CURRENT_TIMESTAMP,newMessage)" end if ' Great thing about SQL, you can automatically put the date and time inside the database without having to code it in. You can set this to be default on your database, or do it from the SQL query. Change the date and time columns on your database to one. And also realize that date is probably a protected word in SQL. Use something more suitable like, datecreated. Set objRecordSet=Server.CreateObject("ADODB.Recordset") ' You should never open the entire table like that, even if that works.. Use a select statement. strSQL = "SELECT * FROM tblMessages" objRecordSet.Open strSQL, objConn, adCmdTable ' Another way to do the below line is 'Do Until objRecordSet.EOF ' But what you have is fine. Do while Not objRecordSet.EOF Response.Write"<B>" & FormatDateTime(objRs .fields("datecreated"),3) & "</B><BR>" Response.Write FormatDateTime(objRs("datecreated"),2) & "<BR>" Response.Write objRs.fields("message") & "<BR>" Response.Write "<p><hr></p>" objRecordSet.MoveNext Loop ' What the formatdatetime does is take the value and format what you want out of it. there are many ways to do this. the 2 after the value just creates time as 00:00:00 PM/AM, the 3 means 00/00/0000 format. ' Also remember that whenever you are dealing with more than one row, you need to put the recordsetname.fields attribute above. objRecordSet.Close Set objRecordSet = Nothing objConn.Close Set objConn = Nothing
•
•
Join Date: Sep 2007
Posts: 1,080
Reputation:
Solved Threads: 68
So you know, if you are using a DATETIME field in your SQL database, you use the CURRENT_TIMESTAMP to get the current date and time. If you are using just date, use CURDATE(). When retrieving your information out of your database, especially for dates, you can do it this way:
DATE_FORMAT(datecreated, '%m/%d/%Y')
this will pull something out of your datetime or date column as: 2/7/2007
A full list on what you can do is here:
http://dev.mysql.com/doc/mysql/en/da...functions.html
Oh, and please mark the question as solved above the bottom banner on the page.
DATE_FORMAT(datecreated, '%m/%d/%Y')
this will pull something out of your datetime or date column as: 2/7/2007
A full list on what you can do is here:
http://dev.mysql.com/doc/mysql/en/da...functions.html
Oh, and please mark the question as solved above the bottom banner on the page.
Last edited by SheSaidImaPregy; Dec 12th, 2007 at 11:22 am.
![]() |
Similar Threads
- Why won't this code work? (VB.NET)
- My nic card won't work! (*nix Hardware Configuration)
- Motorola T720 Phone and Verizon Wireless (Cellphones, PDAs and Handheld Devices)
- Can't get samba to work! (*nix Software)
- Mega Nerd (Geeks' Lounge)
Other Threads in the ASP Forum
- Previous Thread: Extract cell value from Excel and import data via email
- Next Thread: File Upload in MySQL Database with ASP
| Thread Tools | Search this Thread |
Tag cloud for ASP
archive asp asp.net aspandmssqlserver2005 aspandmssqlserver2005connection aspconnection calendar changeable connection current database databaseconnection diagnostics dreamweaver excel fso html iis microsoft msmsql mssql2005 mssqlserver2005 mssqlserver2005andasp mssqlserverandasp opentextfile record searchbox selectoption server single specfic sqlserver sqlserverconnection toolkit web webserver windows7






