' instead of bloating the FE with temporary tables,
' make another BE and link to its tables.
' use the IN clause to specify an external database for make-table queries.
' If running SQL in code, to run in an external database:
' With db ' where db is your database object
' .Execute sSql 'where sSQL is the SQL statement to run
' nNumRecs = .RecordsAffected 'where nNumRecs is a long integer
' End With
Function CreateADatabase(psDatabaseName As String) As String
's4p
'RETURN
' path and filename of created database
'CALLS
' GetDatabaseName
CreateADatabase = ""
Dim sPathFileDatabase As String
sPathFileDatabase = GetDatabaseName(psDatabaseName)
'---------------------- create a new database
'make a blank database
DBEngine.CreateDatabase sPathFileDatabase, dbLangGeneral
CreateADatabase = sPathFileDatabase
End Function
Function GetDatabaseName(psDatabaseName As String) As String
's4p
'RETURN
' path\file of a specified database name
Dim sPathFileDatabase As String
If InStr(psDatabaseName, "\") > 0 Then
'if path was specified, use it
sPathFileDatabase = psDatabaseName
Else
'if no path specified, put database in current path
sPathFileDatabase = CurrentProject.Path & "\" & psDatabaseName
End If
'add extension if not specified
If Right(sPathFileDatabase, 6) <> ".accdb" Then
sPathFileDatabase = sPathFileDatabase & ".accdb"
End If
GetDatabaseName = sPathFileDatabase
End Function
Function Link2TableOtherDatabase(psDatabaseName As String _
, psTablename As String)
's4p
'When you are using Make Table and Append queries,
'use the optional IN clause to specify the path and filename of an external database.
'Then use this procedure to link to the table
'CALLS
' GetDatabaseName
' DropTheTable
Dim sPathFileDatabase As String
Dim db As DAO.Database _
, tdf As DAO.TableDef
sPathFileDatabase = GetDatabaseName(psDatabaseName)
'set db to be the current database
Set db = CurrentDb
'if table is already in the current database, delete it
'might want to check the tabledef Connect property first
'to make sure it is a linked table!
Call DropTheTable(psTablename)
'link to table
With db
Set tdf = .CreateTableDef(psTablename)
tdf.Connect = ";Database=" & sPathFileDatabase
tdf.SourceTableName = psTablename
.TableDefs.Append tdf
.TableDefs.Refresh
End With
'release object variables
Set tdf = Nothing
Set db = Nothing
End Function
Private Sub DropTheTable( _
sTablename As String _
, Optional pdb As DAO.Database _
)
's4p
'Delete a table
'if the table is not there to delete, no error is returned
'another database may be passed
Dim sName As String
Dim db As DAO.Database
On Error GoTo Proc_Err
If pdb Is Nothing Then
Set db = CurrentDb
Else
Set db = pdb
End If
'See if the table is there
sName = db.TableDefs(sTablename).Name
'If no error then table is there -- delete it
With db
.Execute "DROP TABLE [" & sTablename & "];"
.TableDefs.Refresh
End With
DoEvents
Proc_Exit:
On Error Resume Next
Exit Sub
Proc_Err:
Select Case Err.Number
Case 3265 'Table does not exist
Case Else
MsgBox Err.Description, , _
"ERROR " & Err.Number _
& " DropTheTable"
End Select
Resume Proc_Exit
Resume
End Sub