Stuart McCall's Microsoft Access Pages - Tables

Home

Determine if Table exists in current Database

Note that this code requires a reference to "Microsoft DAO 3.x", where x is the highest value on your system.
Public Function IsTable(TableName As String) As Boolean
    Dim tdf As DAO.TableDef
    
    For Each tdf In DBEngine(0)(0).TableDefs
        If tdf.Name = TableName Then
            IsTable = True
            Exit For
        End If
    Next
End Function
Paste this function into a standard module.

Usage:

Debug.Print IsTable("MyTable")
Home Contents

Duplicate a table, with or without data, in the current database

Note that this code requires a reference to "Microsoft DAO 3.x", where x is the highest value on your system.
Public Sub DupeTable(TblMaster$, TblDupe$, Optional StructureOnly As Boolean = True)
    Application.Echo False
    DoCmd.TransferDatabase acExport, "Microsoft Access", CurrentDb.Name, acTable, TblMaster, TblDupe, True
    Application.Echo True
End Sub
Paste this function into a standard module.

Usage:

DupeTable "OriginalTable", "NewTableName"
Home Contents

Obtain a named section of a linked table's Connect property

Note that this code requires a reference to "Microsoft DAO 3.x", where x is the highest value on your system.
Public Function ConnectPart(TableName As String, part As String) As String
' Retrieves part of a table's connect property
' eg: ConnectPart("tblCustomers", "DATABASE")
'         would return the path to the remote file
' or: ConnectPart("tblCustomers", "DSN")
'         would return the DSN name (ODBC link)
'
' Zero-length string returned if part is missing/invalid

    Const Delimiter As String = ";"
    Dim con$, i&, j&
    
    con = CurrentDb.TableDefs(TableName).Connect
    i = InStr(1, con, Delimiter)
    If i = 0 Then Exit Function
    If Right$(con, 1) <> Delimiter Then con = con & Delimiter
    '
    If UCase$(part) = "TYPE" Then
        If i = 1 Then
            ConnectPart = "ACCESS"
        Else
            ConnectPart = Left$(con, i - 1)
        End If
    Else
        i = InStr(1, con, part, vbDatabaseCompare)
        If i Then
            j = InStr(i + 1, con, Delimiter)
            If j Then
                i = i + Len(part) + 1
                ConnectPart = Mid$(con, i, j - i)
            End If
        End If
    End If
    
End Function
Paste this function into a standard module.
Home Contents