|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Ajax Application Generator Generate database and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
|
|
#1
|
|||
|
|||
|
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 03:16 AM. Reason: Edited to include the CODE tags. Preview is your friend =D |
|
#2
|
|||
|
|||
|
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. |
|
#3
|
|||
|
|||
|
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. |
|
#4
|
|||
|
|||
|
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. |
![]() |
| Viewing: ASP Free Forums > Programming > Visual Basic Programming > Creating/Deleting Tables with ADO/VB6 |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|
|
|