AOB
Registered User.
- Local time
- Today, 16:41
- Joined
- Sep 26, 2012
- Messages
- 633
Hi guys,
I want to replicate some of the tables in my BE in a separate database. At this stage, I just want to copy the table structure only (no data). However, I also want to change any autonumber fields to non-incremental long integer fields, before any records get copied across.
Does anybody know how I can change the field type in the new table from AutoNumber to LongInteger using TableDef / Field objects? I know I can do it with DoCmd.RunSQL "ALTER TABLE..." but I'm wary of doing this as the table names are the same in the 'live' BE as they are in the newly created copy and I don't want to accidentally change the attributes of the live
(Although I don't think it's actually possible to change an autonumber field to long integer if there are already records present - however, not willing to test that out!)
I want to replicate some of the tables in my BE in a separate database. At this stage, I just want to copy the table structure only (no data). However, I also want to change any autonumber fields to non-incremental long integer fields, before any records get copied across.
Does anybody know how I can change the field type in the new table from AutoNumber to LongInteger using TableDef / Field objects? I know I can do it with DoCmd.RunSQL "ALTER TABLE..." but I'm wary of doing this as the table names are the same in the 'live' BE as they are in the newly created copy and I don't want to accidentally change the attributes of the live
(Although I don't think it's actually possible to change an autonumber field to long integer if there are already records present - however, not willing to test that out!)
Code:
Private Function CreateArchiveBE(lngYear) As String
Dim wrk As Workspace
Dim dbs As Database
Dim tdf As TableDef
Dim fld As Field
Dim objFSO As Object
Dim arrArchiveTables() As String
Dim strPath As String
Dim i As Long
arrArchiveTables = Split(strArchiveTables, ":")
' Get default Workspace
Set wrk = DBEngine.Workspaces(0)
' Define Path
strPath = strArchiveFolder & "Archive" & lngYear & ".accdb"
' Confirm archive does not already exist (exit if found)
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(strPath) Then
CreateArchiveBE = objFSO.GetFile(strPath).Path
Exit Function
End If
' Create a new .accdb file
Set dbs = wrk.CreateDatabase(strPath, dbLangGeneral)
' Copy the tables in scope from the live to the archive (structure only, no records)
For i = LBound(arrArchiveTables) To UBound(arrArchiveTables)
DoCmd.TransferDatabase acExport, "Microsoft Access", strPath, acTable, arrArchiveTables(i), arrArchiveTables(i), True
Next i
' Important! Need to change autonumber fields to long integers
' (to prevent incrementation - retain the identifiers from the live version so they can be related back and forth later)
For Each tdf In dbs.TableDefs
For Each fld In tdf.Fields
If fld.Attributes And dbAutoIncrField Then
' ?????
End If
Next fld
Next tdf
Set objFSO = Nothing
Set fld = Nothing
Set tdf = Nothing
Set dbs = Nothing
Set wrk = Nothing
End Function