Wrox Programmer Forums
|
Classic ASP Databases Discuss using ASP 3 to work with data in databases, including ASP Database Setup issues from the old P2P forum on this specific subtopic. See also the book forum Beginning ASP.NET Databases for questions specific to that book. NOT for ASP.NET 1.0, 1.1, or 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Databases section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 22nd, 2003, 02:29 AM
Authorized User
 
Join Date: Jun 2003
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default creating a table in a database using asp

Is it possible to create a table in a database using asp?
For example... if a user enters the name of the table and the table
is created without having to open the access database.
If this is possible... how is it done?
Thanks for any help :)
 
Old June 22nd, 2003, 12:09 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

Yes, you can. The following code not only creates a table, but it creates the database as well. This code uses ADOX, an extension of ADO.
Let me know if you have any specific questions regarding the tables, columns etc.

Cheers,

Imar

Code:
<%

Dim sDatabaseName
sDatabaseName = "C:\testDatabase.mdb"

' First call the Create Database method

CreateAccessDatabase sDatabaseName


' Then add a table and columns to this database
CreateAccessTable sDatabaseName


Sub CreateAccessDatabase(strDBPath)
        Dim catNewDB ' As ADOX.Catalog
        Set catNewDB = Server.CreateObject("ADOX.Catalog")
        catNewDB.Create "Provider=Microsoft.Jet.OLEDB.4.0;" & _
                "Data Source=" & strDBPath & _
                ";Jet OLEDB:Engine Type=5;"
                        ' Engine Type=5 = Access 2000 Database
                        ' Engine Type=4 = Access 97 Database
        Set catNewDB = Nothing
End Sub


Sub CreateAccessTable(strDBPath)
        Dim catDB ' As ADOX.Catalog
        Set catDB = Server.CreateObject("ADOX.Catalog")
        Dim tblNew  ' As ADOX.Table
        Set tblNew = Server.CreateObject("ADOX.Table")
        ' Open the catalog.
        catDB.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
                "Data Source=" & strDBPath
        ' First Create an Autonumber column, called ID. This is just for demonstration.
        ' I could have done this below with all the other columns as well
        Dim col ' As ADOX.Column
        Set col = Server.CreateObject("ADOX.Column")
        With col
                .ParentCatalog = catDB
                .Type = adInteger
                .Name = "ID"
                .Properties("Autoincrement") = True
        End With
        tblNew.Columns.Append col
        ' Now add the rest of the columns
        With tblNew
                .Name = "Contacts"
                ' Create fields and append them to the
                ' Columns collection of the new Table object.
                        With .Columns
                                .Append "aNumberColumn", adInteger
                                   .Append "FirstName", adVarWChar
                                   .Append "LastName", adVarWChar
                                   .Append "Phone", adVarWChar
                                   .Append "Notes", adLongVarWChar
                        End With
                        Dim adColNullable       
                        ' Is not defined in adovbs.inc, so we do it
                        '   by hand. 
                        ' The other option is:
                        ' adColFixed with a value of 1
                        adColNullable = 2
                        With .Columns("FirstName")
                                .Attributes = adColNullable
                        End With
        End With
        ' Add the new Table to the Tables collection of the database.
        catDB.Tables.Append tblNew
        Set Col = Nothing
        Set tblNew = Nothing
        Set catDB = Nothing
End Sub
%>
----------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old June 22nd, 2003, 07:02 PM
Authorized User
 
Join Date: Jun 2003
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank Imar
Thats really helpful.
:)
 
Old June 24th, 2003, 08:02 PM
Authorized User
 
Join Date: Jun 2003
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi...
I made a column called Win% and i want to change some of its properties...
I tried.

With .Columns("Win %")
  .Properties("Format") = "Fixed"
  .Properties("Decimal Places") = 2
End With

but of course it didnt work...
i was wondering how to make these changes...
any help would be appreciated
thanks
 
Old June 24th, 2003, 08:37 PM
Authorized User
 
Join Date: Jun 2003
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default

also..
in the same asp page..
i have tried to open a recordset of the table i have just created
but it says
The Microsoft Jet database engine cannot find the input table or query 'Stats28'. Make sure it exists and that its name is spelled correctly.

I know the table definatly exists because i just created it...
I even opened my database and checked it was created properly

I was wondering...
Does it take a little while before the table is created?
and if so...
is there a way to make sure the databse is updated before i open the recordset?

thanks
droopy
 
Old June 25th, 2003, 01:12 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

Unfortunately, working with ADOX is not as intuitive as you'd like.

Anyway, check out http://msdn.microsoft.com/library/de...asp?frame=true to find out more about the Properties property of a column. You may be interested in Properties like NumericScale and Precision.

The "entire" ADOX docs can be found here:

http://msdn.microsoft.com/library/de...asp?frame=true

I think you should consider naming your tables differently than you do right now. The percentage sign may not even be allowed (not 100% sure) because it has special meaning in the SQL syntax.
Using spaces is not wise either. It makes querying harder, and you'll run into troubles when you try to upgrade to SQL server later.
Consider PascalCasing (WinPercentage) or camelCasing (winPercentage) for your table naming scheme.

Cheers,

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old June 25th, 2003, 02:03 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

You may need to close the current connection first, and then reopen it to refresh the new tables. Not sure, but it's worth trying it out.

Cheers,

Imar


Quote:
quote:Originally posted by Droopy
I was wondering...
Does it take a little while before the table is created?
and if so...
is there a way to make sure the databse is updated before i open the
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old June 25th, 2003, 05:39 AM
Authorized User
 
Join Date: Jun 2003
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default

imar...
thanks for all your help..

but
im not sure what you mean by closing the connecting...
i havent created a connection...
all i have done..
is make the table like you said..
and then try to create a recordset

thanks..
droopy
 
Old June 25th, 2003, 06:08 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

The code I posted uses the Active Connection to reach the database. You may need to close the connection, or set your objects to Nothing to free the connection / link to the database.

Are you trying to open the Recordset from the same Sub / Function where you create the tables? If so, move the Recordset code to its own method, and use a new Connection object to make a connection to the database.

Cheers,

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old June 25th, 2003, 06:32 AM
Authorized User
 
Join Date: Jun 2003
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default

yes!
thank you imar!!

i created a connection object...
and closed it inbetween creating the table and making the recordset

it worked perfectly

thanks again
droopy :D





Similar Threads
Thread Thread Starter Forum Replies Last Post
database paging using asp table+ oledbdataadapter yamensaleh ASP.NET 2.0 Basics 0 January 27th, 2007 10:09 AM
List Database table through ASP abhisheksud Classic ASP Databases 0 September 5th, 2005 05:10 AM
Creating Pop-up Window from ASP.Net Table Cell sueolo Classic ASP Basics 0 September 1st, 2005 01:54 PM
ASP 0177 error creating database [email protected] Classic ASP Databases 6 March 3rd, 2005 01:43 PM
Creating table with existing table without value kumar_kumar Oracle 1 January 4th, 2005 07:12 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.