MS Access Auto Link Excel Spreadsheets - ms-access

I have a directory structure where I am managing the requirements of a system with each component of that system having its own directory. the requirements of each component are stored in a excel workbook that has multiple worksheets(# of worksheets are static). I am currently using access as a central location to view the information in these sheets and perform queries on them. I hate having to manually link new excel files every time a new component documentation is added to the directory. Is there a way that when everytime I start access it will search the directory tree of stored excel files and automatically link them to access if they're not linked and update my save queries to include the new files. I was thinking that I could save the sub directory names in a table and all the filenames in those sub directory in another table that references the other table, so as it searches the filesystem it compares names to the table. Is this possible if so could someone point me in the right direction.

You can use Dir or the FileSystemObject recursively to get files from a directory tree. Access stores the link information of files in the connect property of the TableDef or you can get it from:
SELECT msysobjects.Database
FROM msysobjects
WHERE (((msysobjects.Database) Is Not Null));
You can get worksheets like so:
''Requires reference to the Microsoft Excel x.x Object Library
Dim strFileName As String
Dim objXL As New Excel.Application
Dim wkb As Excel.Workbook
Dim wks As Object
''objXL.Visible = True
strFileName = "C:\Docs\LTD.xls"
Set wkb = objXL.Workbooks.Open(strFileName)
For Each wks In wkb.Worksheets
DoCmd.TransferSpreadsheet acLink, acSpreadsheetTypeExcel9, _
wks.Name, strFileName, True, wks.Name & "$"
Next
''Tidy up
wkb.Close
Set wkb = Nothing
objXL.Quit
Set objXL = Nothing
Or using an ADOX.Catalogue: http://forum.lessthandot.com/viewtopic.php?f=95&t=3712

Related

Need to Change Linked Tables in MS Access DB with Username, Password and System DB

I have a front end DB, which needs to link to different back end DBs. To give you perspective, it relates to stand alone MDB files. The software in question builds a DB per company.
At the moment I am writing code within one of these MDB files.
For scalability I am now creating a new DB which will link to each MDB via code, and therefore my questions are as follows
How do I change the linked table location via code / VB so that the user can select the company / DB they want to work on
How do I do this with passing a username and password which is the same for all of the companies / DBs
And as per below we need to verify the username and password via the systemDB for it to open successfully.
As an FYI, this is how we open the DB on a standalone basis-
"C:\Program Files (x86)\Microsoft Office\root\Office16\MSACCESS.EXE" "C:\temp\SAMPLE.mdb" /WRKGRP "C:\ProgramData\SOFTWARE\SYSTEM.mdw" /user:username /pwd:password
This is not a problem at all and is absolutely able to be accomplished given the clarification that you are using a single MDW file.
To clarify Microsoft Access Workgroup Security is essentially a "session" security model that applies directly to the front-end MDB file as you open it.
Your example command line means that Microsoft Access will open the SAMPLE.MDB front-end file using the workgroup file you specified.
Once Microsoft Access has opened SAMPLE.MDB under that workgroup file, you cannot change to another workgroup file within that "session" without closing Microsoft Access and re-opening under the new workgroup file.
FYI - it IS possible to open via code, a table in another MDB using another workgroup file within that connection, but in this manner, the table is only usable in code as a RecordSet (for example), you can't make it a linked table.
Anyway, back to your real issue. How to link a different back-end set of tables for each Company.
My recommendation would be to add a few fields to your Company table that defines the filename and location of each back-end file. For example:
Notice that the location can be a UNC path, or a mapped drive path. Or maybe you don't need to define a location in the table explicitly. Maybe all of the back-ends are in the same folder, or in a definable dynamic location like \Dallas\Dallas.mdb, \NewYork\NewYork.mdb, etc. As long as you can determine the location of each back-end in some manner, then you are fine.
Now, since you will likely have "global" front-end tables, maybe also some "global" linked back-end tables i.e. Common.mdb, and your company-specific back-end tables, I would recommend having a front-end table that defines the name of each of the tables that is involved only in the company-specific files. That way, we can easily loop through just those table names and make the link changes.
For the linking code, let's say that you have prompted the User for which Company they want, and you pass the CompanyID to a re-linking function:
Public Function ChangeCompanyLinks(CompanyID As Long) As Boolean
Dim db As DAO.Database
Dim ldb As DAO.Database
Dim tdf As DAO.TableDef
Dim rstCompany As DAO.Recordset
Dim rstTables As DAO.Recordset
Dim mssql As String
Dim dbFullPath As String
Dim retVal As Boolean
Set db = CurrentDb()
retVal = False
mssql = "SELECT * FROM [tblCompany] WHERE [CompanyID] = " & CompanyID
Set rstCompany = db.OpenRecordset(mssql, dbOpenSnapshot)
If Not rstCompany.BOF Then
dbFullPath = rstCompany("DBLocation") & "\" & rstCompany("DBName")
If Dir(dbFullPath) = rstCompany("DBName") Then
'NOTE: By opening a temporary constant link to the back-end during
' relinking, the relinking runs faster
Set ldb = OpenDatabase(dbFullPath)
mssql = "SELECT * FROM [tblLinkedTables] WHERE [FileType] = ""Company"""
Set rstTables = db.OpenRecordset(mssql, dbOpenSnapshot)
Do While Not rstTables.EOF
Set tdf = db.TableDefs(rstTables("TableName"))
tdf.Connect = ";DATABASE=" & dbFullPath
tdf.RefreshLink
rstTables.MoveNext
Loop
rstTables.Close
ldb.Close
retVal = True
Else
MsgBox "Unable to Locate Company File"
End If
End If
rstCompany.Close
ChangeCompanyLinks = retVal
Set rstCompany = Nothing
Set rstTables = Nothing
Set ldb = Nothing
Set tdf = Nothing
db.Close
Set db = Nothing
End Function
Obviously you will want to add error handling, and customize this a bit to fit your situation, but this code will re-link the specified tables to a new back-end.
Please note that if you eventually change to have your back-end tables in SQL Server (which I highly recommend), the re-linking code would need to be modified a bit. See this answer for more details on that.

Powerquery opening files with bad extensions

One of my datasources comes as Excel "xls" files. I put that in quotes because, under the hood, the authoring system has generated an html file and named it myFile.xls.
When manually opening this, Windows gives me a warning, but Excel will open it. From Excel VBA, I can inhibit-warning, and it opens silently and correctly. But trying to open a folder full of such files, PowerQuery just hangs. I've been manually opening each "xls" file and saving as .xlsx. Any suggestions for getting rid of this manual step?
[Running Excel 2013 under Windows 7.]
Add a reference to Microsoft Scripting Runtime in order to use the Scripting.FileSystemObject object.
Dim app As Excel.Application
'If macro is running within an Excel instance, and you want to use that instance uncomment the next line
'Alternatively, use Application directly
'Set app = Application
'If you want to create a new instance, uncomment the next line
'Set app = New Excel.Application
Dim fso As New Scripting.FileSystemObject
Dim sourceFolder As String
sourceFolder = "C:\path\to\folder\of\pseudo-excel-files"
Dim destFolder As String
destFolder = fso.BuildPath(sourceFolder, "_converted")
If Not fso.FolderExists(destFolder) Then fso.CreateFolder(destFolder)
Dim fle As Scripting.File
For Each fle In fso.GetFolder(sourceFolder).Files
Dim destPath As String
destPath = fso.BuildPath(destFolder, fle.Name & ".xlsx")
Dim book As Workbook
Set book = app.Workbooks.Open fle.Path
book.SaveAs destPath, xlWorkbookDefault
book.Close
Next

MSAccess - TransferDatabase - File locked open?

I am attempting the following in an AccessDb via VBA:
Export a single table from current DB into a new DB in the same directory, via a Query/TransferDatabase. This seems to work as expected.
Dim ws As Workspace
Dim db_new as Database
strPath = CurrentProject.Path & "\Backend_Database\"
strDBFilename = strPath & Raw_Count_File.accdb"
Set ws = DBEngine.Workspaces(0)
Set db_new = ws.CreateDatabase(strDBFilename, dbLangGeneral)
DoCmd.TransferDatabase acExport, "Microsoft Access", _
strDBFilename, acTable, "tmp_RawCountFile", "Raw_TblMatchedTB"
Within the same function used above (to create the new file), I am attempting next to ZIP the new file into the same directory. The result is 1K Byte ZIP file (it's an empty ZIP ).
If I breakout the code segment that creates the ZIP file into separate function (i.e., under another button), the function works as expected and the proper ZIP file is created.
My Question:
I am guessing the new DB file and subsequent TransferDatabase is leaving the new_db file hanging open and inaccessible to the ZIP function. I attempted to set the various objects = nothing prior to the ZIP function, but same result. Only if I exit the first function and call a second function will it work as desired.
Can I add something to the end of the TransferDatabase function to ensure the resulting file will be available for the ZIP task?
My preference is not to add a secondary button press to this task...
Any suggestions to get me going?
Thanks!
Try to Set db_new = Nothing before zipping, in order to close the newly created db.

Data extracted from Acces to Excel is automatically getting over to extra sheet than the defined sheet

I am trying to get some data extracted from MS Access to Excel sheet which is a kind of already defined template. For example , i use an excel file with say one sheet named Result (with some pre-defined data) as a source file and then i copy the same to an output file. Then some OutputTable from MS Access is extracted in output file using DoCmd.TransferSpreadsheet with explicitly mentioned to get extracted in Result sheet . The data is coming fine but the sheet Result is not used though an extra sheet named Result1 gets created with same data as OutputTable of Ms Access.
Code i am using is mentioned below:
SourceFile = CurrentProject.Path & "\Template\" & "Input_Template.xlsx"
DestinFile = CurrentProject.Path & "\Output\" & "Output_" & sDateTimeStamp & ".xlsx"
FileCopy SourceFile, DestinFile
DoCmd.TransferSpreadsheet acExport, , "OutputTable", DestinFile, False, "Result"
I have these folders Template (having Input_Template.xlsx file) and Output folder created on my system under same path where the Database is placed.
Could anyone tell if i'm doing it in wrong way or is there any configuration required or i might be missing something. Any help would be highly appreciated.
Thank You!!!
Honey
TransferSpreadsheet is not suitable for exporting to pre-formatted template. Use CopyFromRecordset method from Excel library instead. Example below will dump content of OutputTable to "Result" spreadsheet of destination template file starting "B2" cell.
Dim xlApp As Object
Dim xlWork As Object
Dim xlSheet As Object
Dim rsExportResults As Recordset
Set rsExportResults = CurrentDb.OpenRecordset("OutputTable")
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
Set xlWork = xlApp.Workbooks.Open(DestinFile)
Set xlSheet = xlWork.Sheets("Result")
xlSheet.Range("B2").CopyFromRecordset rsExportResults

Access query to excel 2010 to create a graph via vba

After an extensive search I have been unable to find any information on this that I could understand. there are numerous examples, but these are all for access 2003, and these do not work in access 2010.
I need to run a vba code that will export the results of a query (QryTotalSale) to excel 2010 and automatically create a bar chart of the data and show this over the database that is running.
If anyone could give me some advise then I would greatly appreciate it, or even a link to a valid resource that will work in 2010.
So far I can get excel to open, and display the results of the query in question.
To make it more difficult I really need the query to open a specific excel file, which will be on a folder on the desktop and every time the button is pressed to run the VBA, a new page should be added to the excel workbook, and the new graph be shown, and saved into the spreadsheet, so that at a later date the entire excel file can be viewed.
I have the below code, but it does not work. the bit about ranges would work in excel, but access does not seem to recognise range ( which does not really suprise me as it does not really work with ranges to my knowledge.)
My second thought was to have the first two doCmd's run, then have the next bit be forced to auto run in the excel file.
Private Sub SalesImage_Click()
DoCmd.OpenQuery "QryTotalSale"
DoCmd.RunCommand acCmdOutputToExcel
Dim myRange as range
Set myRange = B2 * C30
Charts.Add
ActiveChart.ChartType = xlColumnClustered
ActiveChart.SetSourceData Source:=myRange, _
PlotBy:=xlColumns
ActiveChart.Location Where:=xlLocationAsNewSheet
End Sub
I found a adodb code type thing for the 2003 versions of access and excel, but could not get this to work. half of the options no longer seem to be recognised by access...
I am a long way off and would really appreciate any help.
Thanks
Sam
Here are some notes. I have used late binding, so you do not need to set a reference to the Excel library, however, I have included notes on the types.
Dim xl As Object ''Excel.Application
Dim wb As Object ''Excel.Workbook
Dim ws As Object ''Excel.Worksheet
Dim ch As Object ''Excel.Chart
Dim myRange As Object
Set xl = CreateObject("Excel.Application")
sExcelWB = "z:\docs\testchart.xls"
''This will overwrite any previous run of this query to this workbook
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "Query1", _
sExcelWB, True
Set wb = xl.Workbooks.Open(sExcelWB)
''Sheets are named with the Access query name
Set ws = wb.Sheets("Query1")
Set ch = xl.Charts.Add
ch.ChartType = xlColumnClustered
xl.Visible = True
xl.UserControl = True
''Still not saved