|
-
Jan 17th, 2006, 08:40 AM
#1
Thread Starter
Fanatic Member
Create Table using ADO?
I found a page at the MSDN Library witch list all the ADO properties but I couldn't find anything there about Creating a Table. I found some code which creates a table but I need to find the properties, what they mean and what they are. Are these SQL properties?
VB Code:
'create new table TestTable
Call CreateConnection(objConn)
objConn.Execute "CREATE TABLE TestTable(my_id TEXT(50) NOT NULL)"
Call CloseConnection(objConn)
He who never made a mistake never made a discovery?
-
Jan 17th, 2006, 08:46 AM
#2
Re: Create Table using ADO?
Take a look at this link and this one.
Regards,
Mark
Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."
-
Jan 17th, 2006, 08:55 AM
#3
Thread Starter
Fanatic Member
Re: Create Table using ADO?
Thanks those links were pretty helpful. Creating a table or Altering a table looks fairyly simple. How would I check to see if a table already exists?
He who never made a mistake never made a discovery?
-
Jan 17th, 2006, 09:00 AM
#4
Re: Create Table using ADO?
 Originally Posted by Navarone
Thanks those links were pretty helpful. Creating a table or Altering a table looks fairyly simple. How would I check to see if a table already exists?
I would think you could either try creating the table and then catch the "Table Exists Error" or try SELECT * FROM YourTableName" and then again trap for the error.
Regards,
Mark
Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."
-
Jan 17th, 2006, 09:10 AM
#5
Thread Starter
Fanatic Member
Re: Create Table using ADO?
Where can I find out more info on this or how to use it?
He who never made a mistake never made a discovery?
-
Jan 17th, 2006, 09:13 AM
#6
Re: Create Table using ADO?
 Originally Posted by Navarone
Where can I find out more info on this or how to use it?
Check this out
Regards,
Mark
Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."
-
Jan 17th, 2006, 11:10 AM
#7
Re: Create Table using ADO?
 Originally Posted by Mark Gambo
or try SELECT * FROM YourTableName" and then again trap for the error.
This is the method I use, with a minor twist. I use "SELECT * FROM YourTableName WHERE 1=2"
As 1 does not equal 2, no data will be returned if the table exists - and no error will occur (if it exists) either.
-
Jan 17th, 2006, 11:25 AM
#8
Thread Starter
Fanatic Member
Re: Create Table using ADO?
I am not sure I understand, if the table doesn't exist to start with and then lets say when the form loads the table is created, all is well. The next time the form loads, it will try and create the table and thus generate an error.
But how can you do a SELECT statement to a non existing table when the form loads for the first time?
He who never made a mistake never made a discovery?
-
Jan 17th, 2006, 11:38 AM
#9
Re: Create Table using ADO?
 Originally Posted by Navarone
I am not sure I understand, if the table doesn't exist to start with and then lets say when the form loads the table is created, all is well. The next time the form loads, it will try and create the table and thus generate an error.
But how can you do a SELECT statement to a non existing table when the form loads for the first time? 
Are you going to be Creating and then Dropping a table each time your app runs ? Can you explain exactly what you are trying to do and we probably can help you out.
Regards,
Mark
Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."
-
Jan 17th, 2006, 11:45 AM
#10
Re: Create Table using ADO?
 Originally Posted by Navarone
But how can you do a SELECT statement to a non existing table when the form loads for the first time? 
You can try to run any Select statement you like, if the table doesn't exist you will get an error (which you can trap with an error handler).
-
Jan 17th, 2006, 12:11 PM
#11
Re: Create Table using ADO?
 Originally Posted by Navarone
Thanks those links were pretty helpful. Creating a table or Altering a table looks fairyly simple. How would I check to see if a table already exists?
Personally I wouldn't use the methods that has been suggested. Most database systems has schema tables that gives you information about tables, views/queries, columns etc. It's usually better to query these to check if an object already exist than to trap an error.
The way to check if a table exists depends on the database system. In SQL Server you can call the sp_tables 'tablename' sproc or quyery the sysobjects table in the database. If sp_tables returns a row, then the table exists and you don't have to execute the CREATE TABLE. If it is Access you can a table called MSysObjects where the column Name contains the name of the table/query/Form/Report etc.
-
Jan 17th, 2006, 12:17 PM
#12
Re: Create Table using ADO?
That's fair enough.. you could also do the same thing with ADOX, so that you don't need to use specific methods for each DBMS.
-
Jan 17th, 2006, 12:23 PM
#13
Re: Create Table using ADO?
 Originally Posted by si_the_geek
That's fair enough.. you could also do the same thing with ADOX, so that you don't need to use specific methods for each DBMS.
Wow!! Now I feel stupid. For some strange reason I have never tried ADOX, and I'm curious by nature.
THANKS!!!!
Last edited by kaffenils; Jan 17th, 2006 at 12:35 PM.
-
Jan 17th, 2006, 12:30 PM
#14
Re: Create Table using ADO?
Lol... last week somebody let sz' know something new to him about debugging SQL Server SP's, and now I've found something new for you about ADO.
I guess all of us can learn new things in areas we excel at!
-
Jan 17th, 2006, 12:41 PM
#15
Thread Starter
Fanatic Member
Re: Create Table using ADO?
Wow! So much info at once. Ok here is what I need to do. I need to add a table to my existing MS Access database called TempDocAudio with a text field called "n_id" and add two fields to an existing table called Documents and the new text fields are "n_mp3" and "n_wavBol" all of the same database.
So maybe you guys can help me script this out. In my module I set up a sub like this to access my database then when ever I need to make a call to the db I do a Call CreateConnection(objConn) and then Call CloseConnection(objConn).
VB Code:
Sub CreateConnection(objConn As Connection)
' CONNECTION STRING FOR THE DATABASE
AccessConnect = "DRIVER={Microsoft Access Driver (*.mdb)};" & _
"DBQ=Westfield.MDB;" & _
"DefaultDir=" & App.Path & ";"
'MsgBox AccessConnect
' Create Connection Object and open it
Set objConn = New ADODB.Connection
objConn.CursorLocation = adUseClient
' Trap any error/exception
On Error GoTo AdoError
objConn.ConnectionString = AccessConnect
objConn.Open
Exit Sub
' ADO Error/Exception Handler
AdoError:
ErrNumber = Err.Number
ErrSource = Err.Source
ErrDescription = Err.Description
'AdoErrorEx List1, objConn
End Sub
Sub CloseConnection(objConn As Connection)
objConn.Close
Set objConn = Nothing
End Sub
So on my Sub Form Load I have this, how do I correctly scritp out what I want to do.
VB Code:
Private Sub Form_Load()
On Error GoTo FormLoadError
frmIntro.Caption = "1:1 card " & Chr(147) & "Because every customer is different" & Chr(153) & Chr(46) & Chr(148)
frmIntro.Label1.Caption = "© Copyright 2005 Westfield Insurance. All rights reserved."
'create directories if they do not already exist
If Dir(App.Path & "\" & "CDTypes", vbDirectory) = "" Then
'MsgBox "Directory doesn't exist"
MkDir (App.Path & "\" & "CDTypes") ' make new directory
MkDir (App.Path & "\" & "WavFiles") 'make wavFile directory
Else
'MsgBox "Directory Exist"
End If
'create new table TestTable
Call CreateConnection(objConn)
objConn.Execute "CREATE TABLE TestTable(my_id TEXT(50) NOT NULL)"
Call CloseConnection(objConn)
Call CreateConnection(objConn)
objConn.Execute "ALTER TABLE TestTable (my_name TEXT(50) NOT NULL)"
Call CloseConnection(objConn)
' Error handler
FormLoadError:
If Err.Number <> 0 Then
errLogger Err.Number, Err.Description, "Open File"
Err.Clear
Resume Next
'Exit Sub
End If
End Sub
He who never made a mistake never made a discovery?
-
Jan 17th, 2006, 02:39 PM
#16
Thread Starter
Fanatic Member
Re: Create Table using ADO?
I guess what I have will work, but very clumsly. How do I specify the Default Value for my field. I need the fields Default Value to be False.
He who never made a mistake never made a discovery?
-
Jan 17th, 2006, 04:23 PM
#17
Thread Starter
Fanatic Member
Re: Create Table using ADO?
Ok I have been searching thru threads and tried this but I get an error in the CONSTRAINT clause
-2147217900,"[Microsoft][ODBC Microsoft Access Driver] Syntax error in CONSTRAINT clause.
VB Code:
Call CreateConnection(objConn)
objConn.Execute "ALTER TABLE Documents ADD n_wavBol TEXT(50) CONSTRAINT n_wavBol_default DEFAULT False"
Call CloseConnection(objConn)
He who never made a mistake never made a discovery?
-
Jan 18th, 2006, 02:05 PM
#18
Thread Starter
Fanatic Member
Re: Create Table using ADO?
I also tried this
Code:
objConn.Execute "ALTER TABLE Documents ADD n_wavBol TEXT(50)CONSTRAINT n_wavBol_default DEFAULT 'FALSE'"
But this doesn't work either.
He who never made a mistake never made a discovery?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|