Visual Basic Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsProgrammingVisual Basic Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old August 11th, 2004, 04:12 AM
digitalscream digitalscream is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Location: Canada
Posts: 3 digitalscream User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Creating/Deleting Tables with ADO/VB6

Hello again,

My problem is regarding an ADO control that queries an Access DB that doesn't contain the table that is being referred to in the SQL query. Below is an example of what I'm trying to do...

Code:
strQuery = "SELECT * FROM PC" 
 
With adoFAST
	 .ConnectionString = strCnxn
	 .CursorLocation = adUseClient
	 .CursorType = adOpenStatic
	 .CommandType = adCmdText
	 .RecordSource = strQuery
	 .Refresh
End With
Set rstPriorities = adoFAST.Recordset


Once that code is run it generates a run-time error when it attempts to refresh that object if the table PC doesn't exist. What I want to do is create the table based on some default data if it doesn't exist. I could attempt to do this with error trapping but I don't know how to create (or delete) a table using the ADO control.

If this doesn't make sense just ask for some more info and I'll try to explain better.

Thanks for any help in advance!

Luke

Last edited by digitalscream : August 11th, 2004 at 04:16 AM. Reason: Edited to include the CODE tags. Preview is your friend =D

Reply With Quote
  #2  
Old August 11th, 2004, 04:38 AM
Mythomep Mythomep is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Location: Zaandam, The Netherlands
Posts: 70 Mythomep User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 m 37 sec
Reputation Power: 5
Send a message via MSN to Mythomep
Hi,

Creating tables with the ADO Datacontrol is not possible. It is possible with ADO though, you can execute "CREATE TABLE" commands on it. All you have to do is review your database reference manual for the syntax on the CREATE TABLE command and execute that on a connection object that has opened up your (empty) database. Inserting data is then very simple, since you can issue insert statements on the same connection object.

To execute an SQL query on the connection object, simply use:

Code:
  cnDatabaseConnection.Execute sSql


There are numerous options and things you can do with the execute statement, but for simple things like this it's not explicitly needed. There is another way to do it though, that involves the use of opening schema's. But that is a bit more complex. It depends on how rigid you want your solution to be.

If you can tell me the database system, I could cook up an example how to create the table and populate it with initial data, only if it does not exist in the database.

Grtz.©

M.

Reply With Quote
  #3  
Old August 11th, 2004, 04:40 AM
Mythomep Mythomep is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Location: Zaandam, The Netherlands
Posts: 70 Mythomep User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 m 37 sec
Reputation Power: 5
Send a message via MSN to Mythomep
Read man, read it carefully before blurting out....

Hi,

Saw Access as the DB... Hang on in there, I'll cook up an example.

Grtz.©

M.

Reply With Quote
  #4  
Old August 11th, 2004, 05:14 AM
Mythomep Mythomep is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Location: Zaandam, The Netherlands
Posts: 70 Mythomep User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 m 37 sec
Reputation Power: 5
Send a message via MSN to Mythomep
Hi,

I have an example here, it does need an additional reference to the "Microsoft ADO Ext. 2.x for DDL and Security". The X stands for the version number of the MDAC you are running (for me it's 2.8). If you have done so, create a button that is named "cmdCreateTable" and paste in the following code:

Code:
Option Explicit
 
Private Sub InsertInitialData()
  Dim rsPriorities As ADODB.Recordset
  
  Set rsPriorities = New ADODB.Recordset
  With rsPriorities
	.ActiveConnection = "Provider='Microsoft.Jet.OLEDB.4.0';" & _
						"Data Source='C:\Temp\TestDB.mdb';"
	.CursorLocation = adUseClient
	.CursorType = adOpenKeyset
	.LockType = adLockPessimistic
	.Source = "SELECT * FROM Priorities"
	.Open
	
	.AddNew
	.Fields("Priority_ID").Value = 1
	.Fields("Description").Value = "High"
	.Update
  End With
  
End Sub
 
Private Function CreateTable() As Boolean
  Dim catDatabase As ADOX.Catalog
  Dim oTable As ADOX.Table
  Dim bCreateTable As Boolean
' -------------------------------
  On Error GoTo PROC_ERR
  
  '
  ' Pre conditions. Assume we do not create a table.
  ' 
  bCreateTable = False
  
  Set catDatabase = New ADOX.Catalog
  With catDatabase
	.ActiveConnection = "Provider='Microsoft.Jet.OLEDB.4.0';" & _
						"Data Source='C:\Temp\TestDB.mdb';"
	
	Set oTable = .Tables("Priorities")
  End With
  
PROC_EXIT:
  '
  ' Clean up or do exit conditions and die gracefully.
  '
  If (bCreateTable) Then
	Set oTable = New ADOX.Table
	With oTable
	  .Name = "Priorities"
	  Call .Columns.Append("Priority_ID", adInteger)
	  Call .Columns.Append("Description", adVarWChar, 20)
	End With
	
	Call catDatabase.Tables.Append(oTable)
  End If
  
  If (Not catDatabase Is Nothing) Then
	Set catDatabase = Nothing
  End If
  '
  ' Let the caller know if we created a table.
  '
  CreateTable = bCreateTable
PROC_ERR:
  Select Case Err.Number
  Case 3265
	'
	' The table does not exist, enable the creation flag and
	' head for the exit.
	'
	bCreateTable = True
	Err.Clear
	Resume PROC_EXIT
  Case Is <> 0
	MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure cmdCreateTable_Click of Form frmMain"
	bCreateTable = False
	Err.Clear
	Resume PROC_EXIT
  End Select
End Function
 
Private Sub cmdCreateTable_Click()
  Dim bTableCreated As Boolean
  
  bTableCreated = CreateTable
  If (bTableCreated) Then
	Call InsertInitialData
  End If
End Sub


I hope the structure of my coding technique is clear enough to understand the example. Note however that this is one way to do it. There are numerous places where you can expand and improve the code, this should function. You need to change the database location and table names/fields where appropriate.

Any questions, feel free to ask.

Grtz.©

M.

Reply With Quote
Reply

Viewing: ASP Free ForumsProgrammingVisual Basic Programming > Creating/Deleting Tables with ADO/VB6


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT