Algumas características do JET 4 (Access 2000 e superior) são suportados somente sob o ADO.
Uma situação na qual o DDL é realmente utilizável é quanto a mudança do Tipo ou Tamanho dos campos. Você não pode fazer isto com o DAO ou ADOX, utilizar o DDL é a técnica mais prática para estes fins. Obviamente existem outras saídas mais trabalhosas e incoerentes pelo simples fato de serem mais demoradas tanto na implementação quanto na execução.
Abaixo disponibilizo alguns exemplos para que você possa iniciar-se nas técnicas de utilização do DDL.
Índice das Funções
|
Descrição
|
CreateTableDDL()
|
Cria duas tabelas, seus índices e relacionamentos, ilustrando os diferentes tipos de campos suas propriedades configuradas.
|
CreateFieldDDL()
|
Ilustra como adicionar um campo para uma tabela.
|
CreateFieldDDL2()
|
Adiciona um campo a uma tabela em outra base de dados.
|
CreateViewDDL()
|
Cria uma nova query.
|
DropFieldDDL()
|
Deleta o campo de uma tabela.
|
ModifyFieldDDL()
|
Muda o tipo ou tamanho de um campo. (Este é o mais comum uso do DDL.)
|
AdjustAutoNum()
|
Configura o start da AutoNumeração.
|
DefaultZLS()
|
Cria um campo que tem por default ser uma stringque não suporta ficar vazia.
|
Dim cmd As New ADODB.Command
Let cmd.ActiveConnection = CurrentProject.Connection
'Cria o "Contractor" na tabela.
Let strSql = "CREATE TABLE tblDdlContractor " & _ "(ContractorID COUNTER CONSTRAINT PrimaryKey PRIMARY KEY, " & _ "Surname TEXT(30) WITH COMP NOT NULL, " & _ "FirstName TEXT(20) WITH COMP, " & _ "Inactive YESNO, " & _ "HourlyFee CURRENCY DEFAULT 0, " & _ "PenaltyRate DOUBLE, " & _ "BirthDate DATE, " & _ "EnteredOn DATE DEFAULT Now(), " & _ "Notes MEMO, " & _ "CONSTRAINT FullName UNIQUE (Surname, FirstName));"
"(ContractorID COUNTER CONSTRAINT PrimaryKey PRIMARY KEY, " & _
"Surname TEXT(30) WITH COMP NOT NULL, " & _
"FirstName TEXT(20) WITH COMP, " & _
"Inactive YESNO, " & _
"HourlyFee CURRENCY DEFAULT 0, " & _
"PenaltyRate DOUBLE, " & _
"BirthDate DATE, " & _
"EnteredOn DATE DEFAULT Now(), " & _
"CONSTRAINT FullName UNIQUE (Surname, FirstName));"
Let cmd.CommandText = strSql cmd.Execute Debug.Print "tblDdlContractor criada." 'Cria a tabela de Booking.
Debug.Print "tblDdlContractor criada."
'Cria a tabela de Booking.
Let strSql = "CREATE TABLE tblDdlBooking " & _ "(BookingID COUNTER CONSTRAINT PrimaryKey PRIMARY KEY, " & _ "BookingDate DATE CONSTRAINT BookingDate UNIQUE, " & _ "ContractorID LONG REFERENCES tblDdlContractor (ContractorID) " & _ "ON DELETE SET NULL, " & _ "BookingFee CURRENCY, " & _ "BookingNote TEXT (255) WITH COMP NOT NULL);"
"(BookingID COUNTER CONSTRAINT PrimaryKey PRIMARY KEY, " & _
"BookingDate DATE CONSTRAINT BookingDate UNIQUE, " & _
"ContractorID LONG REFERENCES tblDdlContractor (ContractorID) " & _
"ON DELETE SET NULL, " & _
"BookingFee CURRENCY, " & _
"BookingNote TEXT (255) WITH COMP NOT NULL);"
Let cmd.CommandText = strSql cmd.Execute Debug.Print "tblDdlBooking criado." End Sub Sub CreateFieldDDL() Dim strSql As String Dim db As DAO.Database
Debug.Print "tblDdlBooking criado."
Let strSql = "ALTER TABLE MyTable ADD COLUMN MyNewTextField TEXT (5);" db.Execute strSql, dbFailOnError Set db = Nothing Debug.Print "MyNewTextField adicionado para MyTable" End Sub Function CreateFieldDDL2() Dim strSql As String Dim db As DAO.Database Set db = CurrentDb()
db.Execute strSql, dbFailOnError
Debug.Print "MyNewTextField adicionado para MyTable"
Function CreateFieldDDL2()
Let strSql = "ALTER TABLE Table IN 'C:\A&A\Junkki.mdb' ADD COLUMN MyNewField TEXT (5);" db.Execute strSql, dbFailOnError Set db = Nothing Debug.Print "MyNewField Adicionado!" End Function Function CreateViewDDL() Dim strSql As String
db.Execute strSql, dbFailOnError
Debug.Print "MyNewField Adicionado!"
Let strSql = "CREATE VIEW qry1 as SELECT tblInvoice.* from tblInvoice;" CurrentProject.Connection.Execute strSql End Function Sub DropFieldDDL() Dim strSql As String
CurrentProject.Connection.Execute strSql
Let strSql = "ALTER TABLE [MyTable] DROP COLUMN [DeleteMe];" DBEngine(0)(0).Execute strSql, dbFailOnError End Sub Sub ModifyFieldDDL() Dim strSql As String
DBEngine(0)(0).Execute strSql, dbFailOnError
Let strSql = "ALTER TABLE MyTable ALTER COLUMN MyText2Change TEXT(100);" DBEngine(0)(0).Execute strSql, dbFailOnError End Sub Function AdjustAutoNum() Dim strSql As String
DBEngine(0)(0).Execute strSql, dbFailOnError
Let strSql = "ALTER TABLE MyTable ALTER COLUMN ID COUNTER (1000,1);" CurrentProject.Connection.Execute strSql End Function Function DefaultZLS() Dim strSql As String
CurrentProject.Connection.Execute strSql
Let strSql = "ALTER TABLE MyTable ADD COLUMN MyZLSfield TEXT (100) DEFAULT """";" CurrentProject.Connection.Execute strSql End Function
CurrentProject.Connection.Execute strSql