Question about cycling through .mdb's

greaseman

Closer to seniority!
Local time
Yesterday, 23:48
Joined
Jan 6, 2003
Messages
360
I have a project that contains about 200 very small .mdb's. For reasons that make no sense, I have to keep all these little .mdb's separate.

What I'm interested in doing is to run a query against a table in each of the .mdb's and if I find a certain record in the table within each .mdb, have a record written that contains the name of the .mdb in which the record was found . In other words, cycle through all my .mdb's, check my specified table, write my notification record, and then repeat the process for the next .mdb.

Can this be done? Does anyone have an idea or suggestion? I know I can do this separately, but that would be a pain.

If anyone can offer assistance, I would be most appreciative. Thanks!
 
This will require extensive modification

But it does loop through db's in a directory and grab the name and data from each db and dump into a master db

Sub MergeLCP_DBs()
On Error GoTo HandleErr

'Move through the directory structure reading each database name into a table.
'Retrieve information from each table to merge into one database.

Dim FileNum As Integer
Dim sFile, sSourcePath, sDestPath As String

Dim db As DAO.Database
Dim rsImportedDatabases As DAO.Recordset

Set db = CurrentDb
Set rsImportedDatabases = db.OpenRecordset("SELECT * FROM tblLCP_GetDatabaseNames WHERE [Imported]= False", dbOpenDynaset)


'Location on Local Drive
sFile = Dir("C:\Databases\YEProcessing\LCP_Processing\DBsRecvd\*.mdb")
sSourcePath = "C:\Databases\YEProcessing\LCP_Processing\DBsRecvd\"
sDestPath = "C:\Databases\YEProcessing\LCP_Processing\DBsImported\"


Do While Not IsEmpty(sFile) And sFile <> ""

'Insert the database name into the table
DoCmd.SetWarnings False
DoCmd.RunSQL "INSERT INTO tblLCP_GetDatabaseNames (DBName) VALUES ('" _
& sFile & "')"
DoCmd.SetWarnings True

'Copy tables from external database

DoCmd.SetWarnings False

'-------------MgdRev-------
'Make temp table based on external database
DoCmd.RunSQL "SELECT tblAccount.SAPID AS LCP_SAPID, tblpd.[LNAME]& ', ' & tblpd.[FNAME] AS LCPNAME," _
& " tblLeadClients.ClientNumber, tblLeadClients.ClientName, " _
& " tblMgdRev.MgdRevId,tblMgdRevCredit.PartnerDirector, tblMgdRevCredit.PDAmount INTO tblLCP_MgdRevTemp" _
& " FROM (tblMgdRev INNER JOIN tblMgdRevCredit ON tblMgdRev.MgdRevID = tblMgdRevCredit.MgdRevID) INNER JOIN " _
& " ((tblAccount INNER JOIN tblPD ON tblAccount.SAPID = tblPD.SAPID) " _
& " INNER JOIN tblLeadClients ON tblAccount.ClientNumber = tblLeadClients.ClientNumber) ON tblMgdRev.AcctID = tblAccount.AcctID " _
& " IN '" & sSourcePath & sFile & "'" _
& " GROUP BY tblAccount.SAPID, tblpd.[LNAME] & ', ' & tblpd.[FNAME], tblLeadClients.ClientNumber, " _
& " tblLeadClients.ClientName, tblMgdRev.MgdRevId,tblMgdRevCredit.PartnerDirector, tblMgdRevCredit.PDAmount"

'Append records from temp table to table merging records from each database
DoCmd.OpenQuery "qryLCP_Append_MgdRev"

'Add the database name to the records
DoCmd.RunSQL "UPDATE tblLCP_Merged_MgdRev SET DBName= '" & sFile _
& "' WHERE DBName IS NULL"

'---------------PrimarySales--------------
'Make temp table based on external database
DoCmd.RunSQL "SELECT tblAccount.SAPID AS LCP_SAPID, tblpd.[LNAME] & ', ' & tblpd.[FNAME] AS LCPNAME, " _
& " tblsales.salesid,tblLeadClients.ClientNumber, tblLeadClients.ClientName, tblPrimarySalesCredit.PrimaryPD, " _
& "tblSales.Amount INTO tblLCP_PrimarySalesTemp" _
& " FROM (tblPrimarySalesCredit INNER JOIN tblSales ON tblPrimarySalesCredit.SalesID = tblSales.SalesID)" _
& " INNER JOIN ((tblAccount INNER JOIN tblPD ON tblAccount.SAPID = tblPD.SAPID) " _
& " INNER JOIN tblLeadClients ON tblAccount.ClientNumber = tblLeadClients.ClientNumber) " _
& " ON tblSales.AcctID = tblAccount.AcctID " _
& " IN '" & sSourcePath & sFile & "'" _
& " GROUP BY tblAccount.SAPID, tblpd.[LNAME] & ', ' & tblpd.[FNAME], tblSales.SalesId,tblLeadClients.ClientNumber," _
& " tblLeadClients.ClientName, tblPrimarySalesCredit.PrimaryPD, tblSales.Amount"

'Append records from temp table to table merging records from each database
DoCmd.OpenQuery "qryLCP_Append_PrimarySales"

'Add the database name to the records
DoCmd.RunSQL "UPDATE tblLCP_Merged_PrimarySales SET DBName= '" & sFile _
& "' WHERE DBName IS NULL"

'--------------SupportSales------------
'Make temp table based on external database
DoCmd.RunSQL "SELECT tblAccount.SAPID AS LCP_SAPID, tblpd.[LNAME] & ', ' & tblpd.[FNAME] AS LCPNAME," _
& " tblSales.SalesId,tblLeadClients.ClientNumber, tblLeadClients.ClientName, tblSupportSalesCredit.SupportPD, " _
& "tblSales.Amount INTO tblLCP_SupportSalesTemp" _
& " FROM (((tblAccount INNER JOIN tblPD ON tblAccount.SAPID = tblPD.SAPID) " _
& " INNER JOIN tblLeadClients ON tblAccount.ClientNumber = tblLeadClients.ClientNumber)" _
& " INNER JOIN tblSales ON tblAccount.AcctID = tblSales.AcctID) " _
& " INNER JOIN tblSupportSalesCredit ON tblSales.SalesID = tblSupportSalesCredit.SalesID " _
& " IN '" & sSourcePath & sFile & "'" _
& " GROUP BY tblAccount.SAPID, tblpd.[LNAME] & ', '& tblpd.[FNAME], " _
& " tblSales.SalesId,tblLeadClients.ClientNumber, tblLeadClients.ClientName, " _
& " tblSupportSalesCredit.SupportPD, tblSales.Amount"

'Append records from temp table to table merging records from each database
DoCmd.OpenQuery "qryLCP_Append_SupportSales"

'Add the database name to the records
DoCmd.RunSQL "UPDATE tblLCP_Merged_SupportSales SET DBName= '" & sFile _
& "' WHERE DBName IS NULL"
DoCmd.SetWarnings False

'Delete temp tables
DoCmd.DeleteObject acTable, "tblLCP_MgdRevTemp"
DoCmd.DeleteObject acTable, "tblLCP_PrimarySalesTemp"
DoCmd.DeleteObject acTable, "tblLCP_SupportSalesTemp"

'Record in the table that the database has been imported
DoCmd.SetWarnings False
DoCmd.RunSQL "UPDATE tblLCP_GetDatabaseNames SET Imported=True,Importdate= #" & Now() _
& "# WHERE DBName= '" & sFile & "'"
DoCmd.SetWarnings True

'Place a copy of the file in the completed folder and remove the copy from the original location
FileCopy sSourcePath & sFile, sDestPath & sFile
Kill sSourcePath & sFile

'Move to next file
sFile = Dir
Loop

rsImportedDatabases.Close
Set rsImportedDatabases = Nothing

MsgBox "Process Completed", vbInformation

Exit Sub




ExitHere:
Exit Sub

' Error handling block added by Error Handler Add-In. DO NOT EDIT this block of code.
' Automatic error handler last updated at 04-27-2005 11:39:03 'ErrorHandler:$$D=04-27-2005 'ErrorHandler:$$T=11:39:03
HandleErr:
Select Case Err.Number
Case 3078
MsgBox "Import of table in " & sFile & " failed."
Resume Next
Exit Sub
Case Else
Resume Next
'MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical, "General.GetDatabaseName" 'ErrorHandler:$$N=General.GetDatabaseName
End Select
' End Error handling block.
End Sub
 
Thanks for your reply! I'll modify it and give it a shot. That was fast!

And appreciated..........
 
Jerry,

If it's not asking too much, would you mind sending me one of your append queries, perhaps "qryLCP_Append_SupportSales?" I'm havng a bit of a quandary on making the connection between the code you sent and the append query.

Thank you so much! Looking forward to your reply:)
 
send me an e-mail and ill forward a zip file with sample dbs
 
Jerry,

Just wanted to say thank you so much for your help! I was able to modifiy your code and queries successfully. You saved me several hours of headache and heartache.

Thanks again! :)
 
Jerry,

Sorry to bother you again, but..... I've got another "fun" headache and I'm hoping you can help me out once more.....

I've got a bunch of Access .mdb's and here's what I would love to be able to do automatically for all of them:

Do while I've got a bunch of .mdb's

Add a field to a table in the current .mdb
Import a table from another database into the current .mdb
Import a query from another database into the current .mdb
Import a module from another database into the current .mdb
Run the query just imported
Run the module just imported

Get the next .mdb

Loop

Any ideas or suggestions would be greatly appreciated. I'm trying to learn the concepts of cycling through a bunch of .mdb's, but it gives me headaches.

Thanks so very much in advance!!!
 
Well you allready know how to loop through db's. Use DoCmd.TransferDatabase method to import tables, queries and modules. Just loop through dbs and pass in variable to the Transfer database method for each db you are importing from.
DoCmd.TransferDatabase acImport, "MicrosoftAccess", strDatabaseNameYouAreTransferingFrom, acTable, "strSourceTableName", "strDestinationTableName"
 
Hi, Jerry.....

I got the thing I've been working on to loop through my databases, pull in queries, modules, run all the queries, modules and stuff I needed to run.

However...... when I tried doing the Transfer Database Export function to export out my newly altered table back to the database it originally came from, I got a message saying I couldn't do the export, since the table in question was participating in relationships in the original database.

How might I go about circumventing the relationship thingy?

By the way, I really appreciate your assistance.... you saved me a ton of time and aggravation! :) :)
 
Do a make table query in the database the table originally came from pointing to the db the table you want resides in. Open a blank query, in properties you'll see Source Database is set to (Current). Put the String Path to the db you need to access there like this:
Oberon:\StonerJ\Public\Projects\Ad Effectiveness\C3 Adapted\NonContent\AddEff.mbd

Name the query and close. Then reopen and you will see the tables in the Source db instead of the db you are in.

Probably best to do the query in code. Just write one in the query window, copy the SQL into your module so you get the connection string right and pass in variables as you've been doing.
 
Jerry,

Sorry I didn't get back to you on Friday. Question.... Will what you suggested take care of the transfer database issue I encountered? The reason I ask is because you advised to make a Make Table query, and my problem was with the transfer command.

And again, thanks so much.... I appreciate your help!
 
greaseman said:
Jerry,

Sorry I didn't get back to you on Friday. Question.... Will what you suggested take care of the transfer database issue I encountered? The reason I ask is because you advised to make a Make Table query, and my problem was with the transfer command.

And again, thanks so much.... I appreciate your help!

What I'm saying is you can use a make table pointing to the appropriate databases in lieu of transfer command to get around the relationship issue.
 
Jerry,

I don't quite grasp what that'll do, but I'll play around with it and see what gives. Thanks so much for all your willingness to share knowledge and to offer help!
 
OK I see what you are saying. Youre right make table is not going to help. You need to either work out a way to update the tables or break the relations, add new table then restore relations. Updating would be MUCH safer. You can do that by making an update query in the"Master" db that points to the original tables in the original databases the same way I described doing the make table deal.
 
Hi, Jerry.....

What I ended up doing was putting my queries, related tables and modules into one .mdb, and then manually opening up each of the other .mdb's and copying in a module I had made from that one .mdb. In turn, I would run the module that would copy in my needed items from that one .mdb, run the steps I needed and then erase the copied items.

While this was not the best nor my desired approach, since it involved touching every single .mdb, it worked.

It took a couple of days, which is why I didn't get back to you sooner.

Thank you so much for your help and advice. You saved me a lot of time and a lot of coding headache!

Have a great weekend. :)
 

Users who are viewing this thread

Back
Top Bottom