Source code for Issue Number 20

Copyright 1997-2000 by C&D Programming Corp. All Rights Reserved. Source code may not be reproduced except for use in a compiled executable. All rights reserved. If you would like to reprint any or all of this code please email us at info@codeoftheweek.com

Code of the Week Home


Source Code

Just paste this code into any module (this is the desired way) or form. To do this, open up your project and insert a new Module. Change the name of the module to basDatabase and paste this code into the module.

'----------------------------------------------------------------------
'
'   Module Name:    basDatabase
'   Written By:     C&D Programming Corp.
'   Create Date:    1/7/98
'   Copyright:      Copyright 1997-98 by C&D Programming Corp.  Source
'                   code may not be reproduced except for use in a
'                   compiled executable.  All rights reserved.  If
'                   you would like to reprint any or all of this
'                   code please email us at info@codeoftheweek.com
'
'   Purpose:        Determine the decimal equivalent of a fraction
'                   or whole number.
'
'
'----------------------------------------------------------------------
'
'   sDBType can be any database type, such as dBASE III
'       if you want to attach an Access Table, just
'       leave sDBType blank.
'
Function AttachTable(DestDB As String, SrcDB As String, sTable As String, Optional sDBType As String) As Integer
    Dim dbJet As Database
    Dim tdfAttachedTable As New TableDef
    Dim X As Integer

    On Error GoTo Handler

    ' Open Database
    Set dbJet = OpenDatabase(DestDB)

    ' Make sure link does not already exist.  If it does,
    ' this routine will delete it.  Make sure that you
    ' are not attaching a table that already exists.
    For X = 0 To dbJet.TableDefs.Count - 1
        If dbJet.TableDefs(X).Name = sTable Then
            dbJet.TableDefs.Delete sTable
            Exit For
        End If
    Next

    ' Set connection information.
    tdfAttachedTable.SourceTableName = sTable
    tdfAttachedTable.Connect = sDBType & ";DATABASE=" & SrcDB
    tdfAttachedTable.Name = sTable

    ' Append TableDef object to create link.
    dbJet.TableDefs.Append tdfAttachedTable
    dbJet.Close
    Exit Function

Handler:
    Err.Raise Err.Number, "AttachTable", Err.Description
End Function