Inserir campos em tabelas que já existem são necessidades freqüentes quando efetuamos manutenções em bancos de dados. Mas qual é o código mais apropriado para isso? Segue:
Function Vai()' Author: Date: Contact:' André Bernardes 06/03/2012 16:32 bernardess@gmail.com https://sites.google.com/site/bernardescvcurriculumvitae/' Application: MedicalPanelAnalyseBrazil®' Source Code:' Listening:' Cria campo dentro de uma tabela.Call AddFieldToTable ("PMedico", "TimeStamp", dbDate, , , Now())End FunctionFunction AddFieldToTable (ByVal TblName As String, FldName As String, FldType As Integer, Optional FldPos As Integer,Optional FldSize, Optional DefaultValue, Optional FldDes, Optional IsAutoNumber) As Boolean' Author: Date: Contact:' André Bernardes 06/03/2012 16:32 bernardess@gmail.com https://sites.google.com/site/bernardescvcurriculumvitae/' Application: MedicalPanelAnalyseBrazil®' Source Code:' Listening:' Cria campo dentro de uma tabela.Dim Db As DatabaseDim DbPath As VariantDim Td As TableDefDim Fd As FieldDim p As PropertyOn Error Resume Next'get back end path of linked tableDbPath = DLookup("Database", "MSysObjects", "Name='" & TblName & "' And Type=6")If IsNull(DbPath) ThenSet Db = CurrentDb 'if local tableElseSet Db = OpenDatabase(DbPath) 'if linked tableIf Err <> 0 Then'failed to open back end databaseExit FunctionEnd If'in case back end has different table name than front endTblName = DLookup("ForeignName", "MSysObjects", "Name='" & TblName & "' And Type=6")End If'get tableSet Td = Db.TableDefs(TblName)If Err <> 0 Then'failed to get tableGoTo DoneEnd If'if IsAutoNumber, then use the correct field TypeIf Not IsMissing(IsAutoNumber) ThenIf IsAutoNumber ThenFldType = dbLongEnd IfEnd If'add field and propertiesWith Td'create fieldIf FldType = dbText And Not IsMissing(FldSize) ThenSet Fd = .CreateField(FldName, FldType, FldSize)ElseSet Fd = .CreateField(FldName, FldType)End If'position (0 is first position)If Not IsMissing(FldPos) ThenDim Num As IntegerFor Num = 0 To FldPos - 1Td.Fields(Num).OrdinalPosition = NumNextFor Num = FldPos To .Fields.Count - 1Td.Fields(Num).OrdinalPosition = Num + 1NextEnd If'if IsAutoNumberIf Not IsMissing(IsAutoNumber) ThenIf IsAutoNumber ThenFd.Attributes = 17End IfEnd If'add field to table.Fields.Append FdIf Err <> 0 Then'failed to add field - probably already existsGoTo DoneEnd If'defaultIf Not IsMissing(DefaultValue) Then.Fields(FldName).DefaultValue = DefaultValueEnd If'add description propertyIf Not IsMissing(FldDes) ThenSet p = .Fields(FldName).CreateProperty("Description", dbText, FldDes).Fields(FldName).Properties.Append pEnd If'other properties according to personal preferenceIf FldType = dbText Then.Fields(FldName).AllowZeroLength = TrueEnd IfEnd WithAddFieldToTable = True 'defaults to false if it fails to get here'clean upDone:Set Fd = NothingSet Td = NothingIf Not Db Is Nothing Then Db.CloseSet Db = NothingEnd FunctionSub CallAddField()' Author: Date: Contact:' André Bernardes 06/03/2012 16:32 bernardess@gmail.com https://sites.google.com/site/bernardescvcurriculumvitae/' Application: MedicalPanelAnalyseBrazil®' Source Code:' Listening:' Cria campo dentro de uma tabela.Dim Result As Boolean'sample call:Result = AddFieldToTable("Table1", "NewFieldName", dbText, 2, 10, , "sample description")Debug.Print Result'Possible values for FldType parameter:' dbBigInt (Decimal)' dbBinary' dbBoolean (Yes/No)' dbByte' dbCurrency' dbDate' dbDouble' dbGUID (Replication ID)' dbInteger' dbLong (Long Integer)' dbLongBinary (OLE Object)' dbMemo' dbSingle' dbText (specify size, or length of text)' dbVarBinary (OLE Object)'FldPos parameter is the ordinal position, 0 being position 1,' but it works sporadically - I don't know why.'For optional IsAutoNumber parameter, use True or False, or leave blank.End Sub
Nenhum comentário:
Postar um comentário