I've been assigned a project that involves the following steps...
Step 1 - Create recordset returning unique Store Numbers from SQL server database.
Step 2 - For each store number, create a new database and name it the same as the new database.
Step 3 - Create two local tables in the new database and populate it with specific data, based on the store number.
Step 4 - Create a linked table in the new database, pointing to a database located on a fixed drive.
Step 5 - Create a query in the new database that appends data from the newly created local tables(Step 3) to the linked tables(Step 4).
So far I've accomplished Steps 1, 2 and 3. Here is what I've done...
What I'm lost on now is the creation of the Linked tables.(Step 4) I've got a database located on the Stores local drive C:\Operation\Data.mdb and I need to create the links within my code for each new database created in the process. The same would be true for the remaining steps as well. Any ideas or suggestions as to how I might best resolve this problem would greatly be appreciated.
Note: I'm using Access2K, I could use Access2002, and I'd really like to have the new databases Access97 complient so I may have to convert them through code as well. The purpose of all this it to push each new db to the appropriate store via ALTERIS and have the db update the local data on their machines.
Thanks for any help...
Step 1 - Create recordset returning unique Store Numbers from SQL server database.
Step 2 - For each store number, create a new database and name it the same as the new database.
Step 3 - Create two local tables in the new database and populate it with specific data, based on the store number.
Step 4 - Create a linked table in the new database, pointing to a database located on a fixed drive.
Step 5 - Create a query in the new database that appends data from the newly created local tables(Step 3) to the linked tables(Step 4).
So far I've accomplished Steps 1, 2 and 3. Here is what I've done...
Code:
Option Explicit
Dim appAccess As Access.Application
Public Function NewAccessDatabase()
Dim dbs As Object, tdf As Object, fld As Variant
Dim strDB As String, strDBPath As String
Dim rst As Recordset, qdf As QueryDef
' Loop through each of the Branches in the database
Set qdf = CurrentDb.QueryDefs("qryUniqueBranches")
Set rst = qdf.OpenRecordset(dbOpenSnapshot)
strDBPath = CurrentDBDir
With rst
Do While Not .EOF
' For Each of the Branches in the database, create a NEW ACCESS DB in it's own Name
CreateAccessDatabase strDBPath, !COMPANY
ExportTableDataHistHDR strDBPath, !COMPANY
ExportTableDataHistLINE strDBPath, !COMPANY
.MoveNext
Loop
.Close
End With
Set rst = Nothing
Set qdf = Nothing
MsgBox "Done!"
End Function
Public Sub ExportTableDataHistHDR(strDBPath As String, strCompany As String)
Dim sql As String
Dim qdf As QueryDef
sql = " SELECT dbo_HISTHDR.*"
sql = sql & "FROM dbo_HISTHDR "
sql = sql & "WHERE dbo_HISTHDR.COMPANY = '" & strCompany & "'"
With CurrentDb
Set qdf = .CreateQueryDef("qryHistHDR", sql)
' This exports the local table called tblLocal to the new db and calls it tblExported
DoCmd.TransferDatabase acExport, "Microsoft Access", _
strDBPath & "\" & strCompany & ".mdb", acTable, qdf.Name, "tblHistHDR"
.QueryDefs.Delete qdf.Name
End With
Set qdf = Nothing
sql = vbNullString
End Sub
Public Sub ExportTableDataHistLINE(strDBPath As String, strCompany As String)
Dim sql As String
Dim qdf As QueryDef
sql = " SELECT dbo_HISTLINE.*"
sql = sql & "FROM dbo_HISTLINE "
sql = sql & "WHERE dbo_HISTLINE.COMPANY = '" & strCompany & "'"
With CurrentDb
Set qdf = .CreateQueryDef("qryHistLINE", sql)
' This exports the local table called tblLocal to the new db and calls it tblExported
DoCmd.TransferDatabase acExport, "Microsoft Access", _
strDBPath & "\" & strCompany & ".mdb", acTable, qdf.Name, "tblHistLINE"
.QueryDefs.Delete qdf.Name
End With
Set qdf = Nothing
sql = vbNullString
End Sub
Public Sub CreateAccessDatabase(strDBPath As String, strCompany As String)
Dim appAccess As New Access.Application
Dim tdf As Object, fld As Variant
Dim strDB As String
' Initialize string to database path.
strDB = strDBPath & "\" & strCompany & ".mdb"
' Create new instance of Microsoft Access.
Set appAccess = CreateObject("Access.Application.9")
' Open database in Microsoft Access window.
appAccess.NewCurrentDatabase strDB
' Close the database and empty it's object reference to clear memory
appAccess.CloseCurrentDatabase
Set appAccess = Nothing
strDB = vbNullString
End Sub
What I'm lost on now is the creation of the Linked tables.(Step 4) I've got a database located on the Stores local drive C:\Operation\Data.mdb and I need to create the links within my code for each new database created in the process. The same would be true for the remaining steps as well. Any ideas or suggestions as to how I might best resolve this problem would greatly be appreciated.
Note: I'm using Access2K, I could use Access2002, and I'd really like to have the new databases Access97 complient so I may have to convert them through code as well. The purpose of all this it to push each new db to the appropriate store via ALTERIS and have the db update the local data on their machines.
Thanks for any help...