Tablas

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 11

' ' ' ' ' ' ' '

Created Modified Project Model Company Author Version Database

15/11/2011 28/11/2011

Access 2000

'======================================================= '=== MS Access 2000 database creation method '=== '=== 1. Create a new database in the MS Access 2000 '=== 2. Create a new module '=== 3. Copy the Toad Data Modeler output SQL script into the new MS Access 2000 module '=== 4. Select from main menu "Tools" item "References..." and check the "Micros oft DAO 3.6 Object Library." '=== 5. Place your mouse cursor somewhere in the main procedure Main() '=== 6. Run the module code (Click the "Run Sub/UserForm" button or press F5) '======================================================= Public Public Public Public dbs tdf idx rel As As As As DAO.Database DAO.TableDef DAO.Index DAO.Relation

Sub Main() Set dbs = CurrentDb() On Error GoTo ErrorHandler Call BeforeScript Call DropQueries Call DropTables Call DropIndexes Call CreateTables Call Call Call Call Call Call CreatePrimaryKeys CreateIndexes CreateAlterKeys CreateRelations CreateQueries AfterScript

MsgBox "Script successfully processed.", vbInformation Exit Sub ErrorHandler: Select Case Err.Number Case 3010 MsgBox "Table " & tdf.Name & " already exist!", vbInform ation Err.Clear Case 3284 MsgBox "Index " & idx.Name & " for table " & tdf.Name & " already exist !", vbInformation Err.Clear Case Else MsgBox Err.Description, vbCritical

End Select End Sub ' Drop queries '=============== Sub DropQueries()

End Sub ' Drop indexes '============== Sub DropIndexes() 'Drop all non unique indexes End Sub

' Drop tables '=============== Public Sub DropTables() 'Drop relations Call DropRelation("Relationship7") Call DropRelation("Relationship6") Call DropRelation("Relationship5") Call DropRelation("Relationship4") 'Drop tables Call DropTable( Call DropTable( Call DropTable( Call DropTable( Call DropTable( Call DropTable( End Sub ' Create tables '=============== Sub CreateTables() Call CreateTable1 Call CreateTable2 Call CreateTable3 Call CreateTable4 Call CreateTable5 Call CreateTable6 End Sub 'Clientes 'Alquileres 'Pistas 'Precios 'FranjaHoraria 'Noticias

"Noticias" ) "FranjaHoraria" ) "Precios" ) "Pistas" ) "Alquileres" ) "Clientes" )

'=== Create table Clientes ====== Sub CreateTable1() Set tdf = dbs.CreateTableDef( "Clientes" ) Call Call Call Call Call Call Call Call Call ) Call Call Call Call Call Call Call Call Call Call AddFieldToTable("DNI", dbText, 9, 0, "", "", "", TRUE, FALSE ) AddFieldToTable("Nombre", dbText, 10, 0, "", "", "", TRUE, FALSE ) AddFieldToTable("Apellidos", dbText, 30, 0, "", "", "", TRUE, FALSE ) AddFieldToTable("Telfono", dbText, 9, 0, "", "", "", TRUE, FALSE ) AddFieldToTable("Cdigo_Postal", dbText, 5, 0, "", "", "", FALSE, FALSE ) AddFieldToTable("Direccin", dbText, 30, 0, "", "", "", FALSE, FALSE ) AddFieldToTable("Poblacin", dbText, 20, 0, "", "", "", FALSE, FALSE ) AddFieldToTable("EMail", dbText, 30, 0, "", "", "", TRUE, FALSE ) AddFieldToTable("Fecha_Nacimiento", dbDate, 0, 0, "", "", "", FALSE, FALSE AddFieldToTable("Sexo", dbBoolean, 0, 0, "", "", "", FALSE, FALSE ) AddFieldToTable("Federado", dbBoolean, 0, 0, "", "", "", FALSE, FALSE ) AddFieldToTable("Nivel", dbText, 10, 0, "", "", "", FALSE, FALSE ) AddFieldToTable("Activo", dbBoolean, 0, 0, "Yes", "", "", FALSE, FALSE ) AddFieldToTable("Fecha_de_Alta", dbDate, 0, 0, "", "", "", TRUE, FALSE ) AddFieldToTable("Crditos", dbInteger, 0, 0, "", "", "", FALSE, FALSE ) AddFieldToTable("TipoPago", dbText, 10, 0, "", "", "", FALSE, FALSE ) AddFieldToTable("NombreUsusario", dbText, 10, 0, "", "", "", TRUE, FALSE ) AddFieldToTable("Contrasea", dbText, 20, 0, "", "", "", TRUE, FALSE ) AddFieldToTable("Tipo", dbInteger, 0, 0, "0", "", "", FALSE, FALSE )

dbs.TableDefs.Append tdf

End Sub '=== Create table Alquileres ====== Sub CreateTable2() Set tdf = dbs.CreateTableDef( "Alquileres" ) Call Call Call Call Call Call Call ) Call Call Call AddFieldToTable("DNI", dbText, 9, 0, "", "", "", TRUE, FALSE ) AddFieldToTable("Creditos", dbSingle, 0, 0, "", "", "", TRUE, FALSE ) AddFieldToTable("Cdigo", dbText, 5, 0, "", "", "", FALSE, FALSE ) AddFieldToTable("DiaReserva", dbDate, 0, 0, "", "", "", TRUE, FALSE ) AddFieldToTable("Hora_Inicio", dbDate, 0, 0, "", "", "", FALSE, FALSE ) AddFieldToTable("HoraFin", dbDate, 0, 0, "", "", "", TRUE, FALSE ) AddFieldToTable("Confirmada", dbBoolean, 0, 0, "Yes", "", "", FALSE, FALSE AddFieldToTable("FechaAlta", dbDate, 0, 0, "", "", "", FALSE, FALSE ) AddFieldToTable("Completa", dbBoolean, 0, 0, "Yes", "", "", FALSE, FALSE ) AddFieldToTable("Compaero", dbByte, 0, 0, "0", "", "", FALSE, FALSE )

dbs.TableDefs.Append tdf

End Sub '=== Create table Pistas ====== Sub CreateTable3()

Set tdf = dbs.CreateTableDef( "Pistas" ) Call Call Call Call Call AddFieldToTable("Cdigo", dbText, 5, 0, "", "", "", FALSE, FALSE ) AddFieldToTable("Nombre", dbText, 10, 0, "", "", "", FALSE, FALSE ) AddFieldToTable("Tipo", dbInteger, 0, 0, "", "", "", FALSE, FALSE ) AddFieldToTable("Completa", dbBoolean, 0, 0, "Yes", "", "", FALSE, FALSE ) AddFieldToTable("Caractersticas", dbMemo, 0, 0, "", "", "", FALSE, FALSE )

dbs.TableDefs.Append tdf

End Sub '=== Create table Precios ====== Sub CreateTable4() Set tdf = dbs.CreateTableDef( "Precios" ) Call AddFieldToTable("Creditos", dbSingle, 0, 0, "", "", "", TRUE, FALSE ) Call AddFieldToTable("Euros", dbSingle, 0, 0, "", "", "", TRUE, FALSE ) Call AddFieldToTable("Tipo", dbText, 10, 0, "", "", "", TRUE, FALSE ) dbs.TableDefs.Append tdf

End Sub '=== Create table FranjaHoraria ====== Sub CreateTable5() Set tdf = dbs.CreateTableDef( "FranjaHoraria" ) Call AddFieldToTable("Hora_Inicio", dbDate, 0, 0, "", "", "", FALSE, FALSE ) Call AddFieldToTable("Hora_Fin", dbText, 50, 0, "", "", "", FALSE, FALSE ) Call AddFieldToTable("Tipo", dbInteger, 0, 0, "", "", "", FALSE, FALSE ) dbs.TableDefs.Append tdf

End Sub '=== Create table Noticias ====== Sub CreateTable6() Set tdf = dbs.CreateTableDef( "Noticias" ) Call AddFieldToTable("Fecha", dbDate, 0, 0, "", "", "", FALSE, FALSE ) Call AddFieldToTable("Titulo", dbText, 50, 0, "", "", "", FALSE, FALSE ) Call AddFieldToTable("Contenido", dbMemo, 0, 0, "", "", "", FALSE, FALSE ) dbs.TableDefs.Append tdf

End Sub

' Create primary keys '===================== Sub CreatePrimaryKeys() '=== Create primary key for table Clientes ====== Set tdf = dbs.TableDefs( "Clientes" ) Set idx = tdf.CreateIndex( "pk_Clientes" ) idx.Primary = True idx.Unique = True idx.IgnoreNulls = False Call AddFieldToIndex( "DNI", False ) tdf.Indexes.Append idx '=== Create primary key for table Alquileres ====== Set tdf = dbs.TableDefs( "Alquileres" ) Set idx = tdf.CreateIndex( "pk_Alquileres" ) idx.Primary = True idx.Unique = True idx.IgnoreNulls = False Call Call Call Call Call AddFieldToIndex( AddFieldToIndex( AddFieldToIndex( AddFieldToIndex( AddFieldToIndex( "DNI", False ) "Creditos", False ) "Cdigo", False ) "DiaReserva", False ) "Hora_Inicio", False )

tdf.Indexes.Append idx '=== Create primary key for table Pistas ====== Set tdf = dbs.TableDefs( "Pistas" ) Set idx = tdf.CreateIndex( "pk_Pistas" ) idx.Primary = True idx.Unique = True idx.IgnoreNulls = False Call AddFieldToIndex( "Cdigo", False ) tdf.Indexes.Append idx '=== Create primary key for table Precios ====== Set tdf = dbs.TableDefs( "Precios" ) Set idx = tdf.CreateIndex( "pk_Precios" ) idx.Primary = True idx.Unique = True

idx.IgnoreNulls = False Call AddFieldToIndex( "Creditos", False ) tdf.Indexes.Append idx '=== Create primary key for table FranjaHoraria ====== Set tdf = dbs.TableDefs( "FranjaHoraria" ) Set idx = tdf.CreateIndex( "pk_FranjaHoraria" ) idx.Primary = True idx.Unique = True idx.IgnoreNulls = False Call AddFieldToIndex( "Hora_Inicio", False ) tdf.Indexes.Append idx End Sub ' Create indexes '================ Sub CreateIndexes() '=== Create indexes for table Clientes ====== Set tdf = dbs.TableDefs( "Clientes" ) Set idx = tdf.CreateIndex( "DNI" ) idx.Unique = True idx.Clustered = False idx.IgnoreNulls = False Call AddFieldToIndex( "DNI", False ) tdf.Indexes.Append idx End Sub ' Create alter keys (unique indexes in MS ACCESS) '================================================ Sub CreateAlterKeys() '=== Create alter keys (unique) for table Clientes ====== Set tdf = dbs.TableDefs( "Clientes" ) Set idx = tdf.CreateIndex( "DNI" ) idx.Unique = True Call AddFieldToIndex( "DNI", False ) tdf.Indexes.Append idx

'=== Create alter keys (unique) for table Clientes ====== Set tdf = dbs.TableDefs( "Clientes" ) Set idx = tdf.CreateIndex( "NombreUsusario" ) idx.Unique = True Call AddFieldToIndex( "NombreUsusario", False ) tdf.Indexes.Append idx '=== Create alter keys (unique) for table Precios ====== Set tdf = dbs.TableDefs( "Precios" ) Set idx = tdf.CreateIndex( "Tipo" ) idx.Unique = True Call AddFieldToIndex( "Tipo", False ) tdf.Indexes.Append idx End Sub ' Create relations '================== Sub CreateRelations() '=== Create relations between parent table Clientes and child table Alquileres = ===== Set rel = dbs.CreateRelation("Relationship4") rel.Table rel.ForeignTable rel.Attributes = 0 = "Clientes" = "Alquileres"

Call AddFieldToRelation("DNI", "DNI") dbs.Relations.Append rel '=== Create relations between parent table Pistas and child table Alquileres === === Set rel = dbs.CreateRelation("Relationship5") rel.Table rel.ForeignTable rel.Attributes = 0 = "Pistas" = "Alquileres"

Call AddFieldToRelation("Cdigo", "Cdigo") dbs.Relations.Append rel '=== Create relations between parent table Precios and child table Alquileres == ====

Set rel = dbs.CreateRelation("Relationship6") rel.Table rel.ForeignTable rel.Attributes = 0 = "Precios" = "Alquileres"

Call AddFieldToRelation("Creditos", "Creditos") dbs.Relations.Append rel '=== Create relations between parent table FranjaHoraria and child table Alquile res ====== Set rel = dbs.CreateRelation("Relationship7") rel.Table rel.ForeignTable rel.Attributes = 0 = "FranjaHoraria" = "Alquileres"

Call AddFieldToRelation("Hora_Inicio", "Hora_Inicio") dbs.Relations.Append rel

End Sub ' Create queries '================ Sub CreateQueries() Dim qdf As QueryDef End Sub ' Drop queries '============== Sub DropQuery(QueryName As String) Dim qdf As QueryDef Set qdf = Nothing On Error Resume Next Set qdf = dbs.QueryDefs(QueryName) On Error GoTo 0 If Not qdf Is Nothing Then dbs.QueryDefs.Delete ( QueryName ) End Sub ' Drop relation '=============== Sub DropRelation(RelName As String)

Set rel = Nothing On Error Resume Next Set rel = dbs.Relations(RelName) On Error GoTo 0 If Not rel Is Nothing Then dbs.Relations.Delete ( RelName ) End Sub ' Drop table '============ Sub DropTable(TableName As String) Set tdf = Nothing On Error Resume Next Set tdf = dbs.TableDefs(TableName) On Error GoTo 0 If Not tdf Is Nothing Then dbs.TableDefs.Delete ( TableName ) End Sub ' Drop index '============ Sub DropIndex(TableName As String, IndexName As String) Set tdf = Nothing Set idx = Nothing On Error Resume Next Set tdf = dbs.TableDefs(TableName) Set idx = tdf.Indexes(IndexName) On Error GoTo 0 If (Not tdf Is Nothing) And (Not idx Is Nothing) Then tdf.Indexes.Delete ( Index Name ) End Sub ' Add fields to table '===================== Sub AddFieldToTable(FieldName As String, DataType As String, SizeCol As Integer, Attributes As Long, DefaultValue As Variant, ValText As String, ValRule As Stri ng, NotN As Boolean, ZeroLength As Boolean) Dim fld As DAO.Field Set fld = tdf.CreateField( FieldName, DataType ) If SizeCol If Attributes <> 0 Then fld.Size <> 0 Then fld.Attributes = SizeCol = Attributes

fld.Required = NotN If DataType = dbText or DataType = dbMemo Then fld.AllowZeroLength = ZeroLength fld.DefaultValue = DefaultValue fld.ValidationRule = ValRule

fld.ValidationText tdf.Fields.Append fld End Sub

= ValText

' Add properties to table '========================= Sub AddPropertyToTable( PropertyName As String, Value As Variant, DataType As St ring) Dim prp As DAO.Property Set prp = tdf.CreateProperty(PropertyName, DataType, Value) tdf.Properties.Append prp End Sub ' Add properties to field '========================= Sub AddPropertyToField( FieldName As String, PropertyName As String, Value As Va riant, DataType As String) Dim prp As DAO.Property Dim fld As DAO.Field Set fld = tdf.Fields( FieldName ) Set prp = fld.CreateProperty(PropertyName, DataType, Value) fld.Properties.Append prp End Sub ' Add fields to index '===================== Sub AddFieldToIndex( FieldName As String, Descending As Boolean ) Dim fld As DAO.Field Set fld = idx.CreateField( FieldName ) If Descending = True Then fld.Attributes = dbDescending idx.Fields.Append fld End Sub ' Add fields to relation '======================== Sub AddFieldToRelation( PKField As String, FKField As String ) Dim fld As DAO.Field

Set fld = rel.CreateField( PKField ) fld.ForeignName = FKField rel.Fields.Append fld End Sub ' Create before script '===================== Sub BeforeScript()

End Sub ' Create after script '==================== Sub AfterScript()

End Sub

You might also like