Link to home
Start Free TrialLog in
Avatar of Murray Brown
Murray BrownFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Access Create Table in backend

Hi

In Access I am using the following VBA code to add new columns in a linked table which is in another database.
What similar code would I use to rather create a new linked table?

Thanks



Sub Main
    Call Add_Column_to_Backend("t_Arms", "RegExpiryDate", "Date")
    Call Add_Column_to_Backend("t_Licence", "DriverExpiry", "Date")
   
End Sub


'==================== Add Backend Column =========================
Sub Add_Column_to_Backend(ByVal oTable As String, ByVal oColumn As String, ByVal oType As String)
   
    On Error Resume Next
   
    Dim strDbName As String 'Database name
    strDbName = GetLinkedDBName(oTable)
    Dim dbs As DAO.Database
    Dim tdf As DAO.TableDef
    Dim fld As DAO.field
    Dim blnFieldExists As Boolean
    ''Set dbs = CurrentDb()
   
    Set dbs = OpenDatabase(strDbName)
    Set tdf = dbs.TableDefs(oTable)
   
    For Each fld In tdf.Fields
        If fld.Name = oColumn Then
            blnFieldExists = True
            Exit For
        End If
    Next
    ''https://docs.microsoft.com/en-us/office/client-developer/access/desktop-database-reference/tabledef-createfield-method-dao
    If Not blnFieldExists Then
        If oType = "Date" Then
           Set fld = tdf.CreateField(oColumn, dbDate)
           fld.DefaultValue = "=Now()"
        ElseIf oType = "Text" Then
            Set fld = tdf.CreateField(oColumn, dbText)
        ElseIf oType = "Integer" Then
            Set fld = tdf.CreateField(oColumn, dbInteger)
        Else
            Set fld = tdf.CreateField(oColumn, dbText)
        End If
       
        ''
       
        tdf.Fields.Append fld
        MsgBox "New column & '" & oColumn & "' added to table '" & oTable & "'"
    End If
   
End Sub

Public Function GetLinkedDBName(TableName As String)
    Dim db As DAO.Database, Ret
    On Error GoTo DBNameErr
    Set db = CurrentDb()
    Ret = db.TableDefs(TableName).Connect
    GetLinkedDBName = Right(Ret, Len(Ret) - (InStr(1, Ret, "DATABASE=") + 8))
    Exit Function
DBNameErr:
    GetLinkedDBName = 0
End Function
'==================== Add Backend Column (End) =========================


'+++++++++++++++++++ Rename Backend Column +++++++++++++++++++++++
Public Sub RenameColumn(ByVal TableName As String, ByVal oldName As String, ByVal newName As String)
   
    On Error Resume Next
   
    Dim dbName As String
    dbName = GetLinkedDBName(TableName)
   
    Dim db As DAO.Database
    Set db = OpenDatabase(dbName)
   
    Dim tdf As DAO.TableDef
    Set tdf = db.TableDefs(TableName)
    If (ExistInCollection(oldName, tdf.Fields)) Then
        Dim field As DAO.field
        Set field = tdf.Fields(oldName)
       
        field.Name = newName
    End If
End Sub

Public Function ExistInCollection(ByVal key As String, ByRef col As Object) As Boolean
    On Error Resume Next
    ExistInCollection = ExistInCollectionByVal(key, col) Or ExistInCollectionByRef(key, col)
End Function

Private Function ExistInCollectionByVal(ByVal key As String, ByRef col As Object) As Boolean
On Error GoTo Error
    Dim item As Variant
    item = col(key)
    ExistInCollectionByVal = True
Exit Function
Error:
    ExistInCollectionByVal = False
End Function

Private Function ExistInCollectionByRef(ByVal key As String, ByRef col As Object) As Boolean
On Error GoTo Error
    Dim item As Variant
    Set item = col(key)
    ExistInCollectionByRef = True
Exit Function
Error:
    ExistInCollectionByRef = False
End Function
Avatar of John Tsioumpris
John Tsioumpris
Flag of Greece image

Private Sub CreateTable(ByRef db As DAO.Database, nameOfTable as String)
    Dim columnName As String
    Dim tdf As DAO.TableDef
    Dim fld As DAO.Field
    Dim columns As Variant
   


    If checkTableExists(db, nameOfTable) Then
        'Nothing the table is already made
        Set tdf = db.TableDefs(nameOfTable)
    Else
        Set tdf = db.CreateTableDef(nameOfTable)
        db.TableDefs.Append tdf
        db.TableDefs.Refresh

    End If
End Sub

Open in new window

Avatar of Murray Brown

ASKER

Thanks John. How would I specify column names and data types? Also what code would I use for checkTableExists?
Creating a Linked Table implies that the table already exists. If you want to create a table in the backend and then link it, just use the methods you posted earlier, and then use John's method to create the link.

Note that you can also use DoCmd.TransferDatabase to create a linked table:

https://docs.microsoft.com/en-us/office/vba/api/Access.DoCmd.TransferDatabase
Hi Murray
I am out for now,when I am back I will fix the function
Thanks John
I would ask one question,

What is your use case for creating a new table in the back-end?  Are you doing this so that you can deploy changes in the production BE after working in the development BE?  Generally, at least when working with Access, when I need to do that, I just force users out of the application and copy the new table into the production BE.
Hi Dale. The database is being used by a police department for gun control purposes. They don't want anyone including me to have a copy of the data. So when I add new tables I want to be able to do this without going onto their system
SOLUTION
Avatar of ste5an
ste5an
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Daniel Pineault
Daniel Pineault

You may like to look over

http://www.peterssoftware.com/beu.htm
thanks for the help