iEntry 10th Anniversary Webforumz RegistrationAnnouncements Contact Webforumz StaffContact
Home Resources Blogs Meet the Team Contact Register
 

Go Back   WebForumz.com > The Code > Classic ASP

Reply
 
LinkBack Thread Tools
Old May 20th, 2009, 05:08 PM   #1
New Member
 

Join Date: May 2009
Location: texas
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0 sunny99 is on a distinguished road
Check field exists and dynamically add column/field in Access Table

I am trying to dynamically insert form field values into the database. The code has 3 parts to it
1) Check if field already exists and Alter table to add columns/fields
2) Insert the values in the column/fields of the table

Html form url is below

users1.jabry.com/sunny/test.html

I am unable to check if fields are existing in the table and keep getting error-

Field 'notes1' already exists in table 'LUReview'.

OR

Item cannot be found in the collection corresponding to the requested name or ordinal.
/geo/Test/insert.asp, line 78

the line 78 corresponds to following code (inserting values)

Code:
 
rsAdd.Fields("notes" & i & "") = Request.Form("Name" & i & "")
which I believe is because fields have not been created hence not able to insert the values.

I will appreciate if you can look into code and let me know the fix.

---------------------------------------------------
html file is at: users1.jabry.com/sunny/test.html
---------------------------------------------------

and heres the complete ASP code again.

i will greately appreciate if you can fix the bug.

---------------------------------------------------
I have attached access database, along with html and asp files
---------------------------------------------------
Code:
 
 
<%
'**********************
'Variable Declarations
'Setting up Objects
'**********************
Dim myRS, objConn, connString, maxfields, strSQL, oField, nameExists, oRecordset
 
connString = Server.MapPath("misc.mdb")
 
Set objConn = Server.CreateObject("ADODB.Connection")
objConn.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & connString
Set rsAdd = Server.CreateObject("ADODB.Recordset")
 
maxfields = Request.Form("NoOfPpl")
%>
 
<%
'**********************
'Check if form field exists in database table
'**********************
 
 
dbname = "misc.mdb" 
tablename = "LUReview" 
 
ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;data source=" 
ConnStr = ConnStr & server.mappath(dbname) 
 
set adoxConn = CreateObject("ADOX.Catalog") 
set adodbConn = CreateObject("ADODB.Connection") 
adodbConn.open ConnStr 
adoxConn.activeConnection = adodbConn 
 
 
Set oRecordset = adodbConn.Execute("SELECT TOP 1 * FROM LUReview") 
For Each oField In oRecordset.Fields
    nameExists = False
    if left(oField.Name,5) = "notes" Then
        for i = 1 to maxfields
            If oField.Name = Request.Form("Name" & i) Then 
                nameExists = True 
                Exit for 
            End If
            If nameExists = False Then
                adodbConn.Execute("ALTER TABLE LUReview ADD COLUMN notes" & i & " TEXT(50);")
            end if 
        next
    end if
next 
 
 
set table = nothing 
adodbConn.close: set adodbConn = nothing 
set adoxConn = nothing 
 
%>
 
<% 
'**********************
'Insert form field values in the columns/fields of table
'**********************
 
Set objConn = Server.CreateObject("ADODB.Connection")
objConn.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & connString
 
Set rsAdd = Server.CreateObject("ADODB.Recordset")
 
strSQL = "SELECT LUReview.* FROM LUReview;"
 
rsAdd.CursorType = 2
rsAdd.LockType = 3
rsAdd.Open strSQL, objConn
rsAdd.AddNew
 
for I = 1 to maxfields
rsAdd.Fields("notes" & i & "") = Request.Form("Name" & i & "")
next
 
rsAdd.Update
rsAdd.Close
Set rsAdd = Nothing
Set objConn = Nothing
 
Response.Write "Values successfully added."
 
%>
Attached Files
File Type: zip dynamic.zip (43.0 KB, 0 views)

Last edited by sunny99; May 20th, 2009 at 05:12 PM.. Reason: Attachment
sunny99 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
fastest way to check if table exists CloudedVision PHP 3 February 20th, 2008 01:15 PM
Unknown column in 'field list' Emzi PHP 4 December 19th, 2007 04:38 PM
Alter Access field online AndyUK Databases 0 March 29th, 2007 04:13 PM
Access Memo Field sarahrobin Databases 0 February 13th, 2007 05:15 AM
Update autonumber field in Access Database redkyna Databases 3 August 20th, 2004 06:18 AM


Search Engine Optimization by vBSEO 3.2.0 RC8