PDA

View Full Version : Having trouble creating an auto number


Crash1hd
11-27-2002, 01:03 PM
I have a script here that is giving me an error and I cant figure out why

<html><head><title>Book</title></head>

<body>

<%
BAFA=Request("BAFA")
Name=Request("Name")
City=Request("City")
BR=Request("BR")
CS=Request("CS")

DateField=Now

//create connection to database with dns-less connection
Set Conn = Server.CreateObject("ADODB.Connection")
Set RS = Server.CreateObject("ADODB.Recordset")
StrConn = "DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" & Server.Mappath("Book.mdb")

strSQL2 = "Select Top 1 NumberField From Book;"

Conn.Open(StrConn)
rst2.Open strSql2, conn, adOpenForwardOnly, adLockOptimistic

//Check if there are records in the recordset
//If (Conn.EOF) Then
//NewNumber = 1
//Else
NewNumber = rst2.Fields("NumberField") + 1
//End If

//insert the variable to the table
sql = "insert into Book (NumberField, BAFA, Name, City, BR, CS, DateField) values (" & NewNumber & ", '" & BAFA & "','" & Name & "', '" & City & "', '" & BR & "', '" & CS & "', '" & DateField & "');"
Conn.Execute(sql)

%>

<%
//Visitor information and Message was now saved to database; To create a simple table for displaying all data from database
//The ORDER BY DateField DESC is what makes everything show up newest to oldest remove to have it go oldest to newest

//Don't forget to close database connection when all is done
Set RS = Nothing
Set Conn = Nothing
//Set rst2 = Nothing

Conn.Close
//rst2.Close

ANy help would be appreciated :)

JustAsking
11-27-2002, 02:05 PM
Crash1hd,

To start with, I believe there are a couple of basic mistakes.


BAFA=Request("BAFA")
Name=Request("Name")
City=Request("City")
BR=Request("BR")
CS=Request("CS")


Where are you requesting the variables from:
1. From a form on the previous page (e.g. <input type="text" name="field1">... etc)
2. From the url when going to this page (e.g. page2.htm?BAFA=text&Name=text... etc)

If number 1 the code should look like:


BAFA=Request.Form("BAFA")
Name=Request.Form("Name")
City=Request.Form("City")
BR=Request.Form("BR")
CS=Request.Form("CS")


If number 2 the code should look like:


BAFA=Request.Querystring("BAFA")
Name=Request.Querystring("Name")
City=Request.Querystring("City")
BR=Request.Querystring("BR")
CS=Request.Querystring("CS")


-----------------------
I believe you need to add () to your code, if you want to use VBScript:


DateField=Now()


-----------------------


strSQL2 = "Select Top 1 NumberField From Book;"


What is Top - 1 - NumberField?

If these are columns in your database your code will need to look like this:


strSQL2 = "SELECT Top,1,NumberField FROM Book"


You could tidy your code up alittle, perhaps look at
http://www.w3schools.com/asp for tutorials on asp and sql.

Crash1hd
11-27-2002, 09:33 PM
Thats all very helpfull but I am still getting the

Microsoft VBScript runtime error '800a01a8'

Object required: ''

/Autoupdate Test1/Thankyou.asp, line 22

Error?

Morgoth
11-27-2002, 10:57 PM
ER..

Line22:
"rst2.Open strSql2, conn, adOpenForwardOnly, adLockOptimistic"

I am not sure what the problem is, but that doesn't look right anyways.
You must be doing that wrong.

Try something LIKE:
Set oRS = Conn.Execute(strSQL2)


Hum.. By the looks of the way your code is writen out, it seems you are very new to ASP is that true? Ever program in C++ or C#?

JustAsking
11-28-2002, 01:02 AM
I may be blind but where are you getting the variable 'rst2'

I thought you would replace 'rst2' with 'rs'.

Look at the code below

Set Conn = Server.CreateObject("ADODB.Connection")
Set RS = Server.CreateObject("ADODB.Recordset")
StrConn = "DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" & Server.Mappath("Book.mdb")
Conn.Open StrConn
strSQL2 = "SELECT Top,1,NumberField FROM Book"
RS.Open strSQL2, conn, adOpenForwardOnly, adLockOptimistic


Thats all very helpfull but I am still getting the

Microsoft VBScript runtime error '800a01a8'

Object required: ''

/Autoupdate Test1/Thankyou.asp, line 22


Would have been good in the first post :)

Morgoth
11-28-2002, 01:18 AM
Originally posted by JustAsking
I may be blind but where are you getting the variable 'rst2'

I thought you would replace 'rst2' with 'rs'.

Look at the code below

Set Conn = Server.CreateObject("ADODB.Connection")
Set RS = Server.CreateObject("ADODB.Recordset")
StrConn = "DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" & Server.Mappath("Book.mdb")
Conn.Open StrConn
strSQL2 = "SELECT Top,1,NumberField FROM Book"
RS.Open strSQL2, conn, adOpenForwardOnly, adLockOptimistic



Would have been good in the first post :)

In the first post, in your code I see rst2, maybe it should be rs.open then.

I don't know what "adOpenForwardOnly, adLockOptimistic" is.
Remove it if you don't know what it is either. Also, try removing it even if you know what it is.

JustAsking
11-28-2002, 03:42 AM
Crash1hd,

You don't have this in your code so I'll assume you are not aware of it. To use the cursor types you need to include the adovbs.inc file which declares all the cursors.

I have attached a zip file with this for you, unzip it and put this in the <head> section: <!--#include file="adovbs.inc"-->


I don't know what "adOpenForwardOnly, adLockOptimistic" is.


These are cursor types, cursors keep track of the position in a result set of a database.

Check out Microsoft (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ado270/htm/mdconcursortypes.asp) for info on the types of cursors available.

Crash1hd
11-28-2002, 02:21 PM
Ok yes I am very new at this coding thing lol newbie hehe the code that you see above though was given to me from someone said that it would work but I cant get it to work!

If I just ask what it is that I am trying to acomplish then maybe someone can give me a code that would work that doesnt need any additional files hopefully!

I am trying to create an input field that when someone fills out a form they get a number like an id number or an account number that they can reference to at a later date!

I am very wet behind the ears and I am aware of the fact that I am diving into the deepend right away and sinking fast lol!

P.s. if anyone can show me to a webpage that has a tutorial to something similar to what I need would be also very apreciative

Adam

I have added a zip file of the complete sample website that i am making :)

mark123
11-30-2002, 05:42 PM
Originally posted by Crash1hd
Ok yes I am very new at this coding thing lol newbie hehe the code that you see above though was given to me from someone said that it would work but I cant get it to work!

I uploaded the webpage you attatched to my web server and it worked quite well. If you are still having trouble with it, perhaps make sure that your web server (or local machine) can run ASP.

Crash1hd
12-01-2002, 07:56 AM
Oh I didnt say that it didnt work I am aware that it works! But the part that I am trying to get to work is not working :) which is to add a field that is autonumber! I have finished what I am trying to do and I am going to post it on here so that if anyone wants to see what it was I was talking about or how to do the same it is readily available!

Adam