Access VBA Convert Linked Table to Local - vba

I am running into a problem and can use some advice. I am linking to an excel spreadsheet and then i am trying to convert that link into a table.
my very basic function is as follows
dim mypath as string
docmd.transferspreadsheet aclink, acspreadsheettypeexcel112xml, "importeddata", mypath, true
runcommand acCmdConvertLinkedTableToLocal
this gives me a runtime error 2046
The command or action 'ComvertLinkedTableToLocal' isn't available now.
So, the reason for doing the link and then the conversion is when done manually, it will get rid of all the conversion errors that a normal import will do and then I have my basic VBA scripts which seem to run far quicker in Access than Excel.
Again, any help is duly duly appreciated
Groundhog.

I believe you need to have a specific linked table selected in order to run the acCmdConvertLinkedTableToLocal command. Try something like this after linking:
DoCmd.SelectObject acTable, "importeddata", True
DoCmd.RunCommand acCmdConvertLinkedTableToLocal
Alternatively, have you tried creating an empty table that matches the spreadsheet structure (i.e. using text fields instead of numeric fields) so that you don't get the import errors when you import instead of link?

Thank you for sharing this code example. I had to make one addition to get this to work correctly for me. I was getting a runtime message that the command was not available before adding a DoEvents after the SelectObject and prior to acCmdConvertLinkedTableToLocal.

Related

Why do I get error 3073 in VBA when using "TransferSpreadsheet acImport" but not when using "TransferSpreadsheet acLink"

I just want to import a number of Excel sheets into MS Access to combine data from several months (each in one .xlsx file). Got it to work as long as I link the .xlsx files using
DoCmd.TransferSpreadsheet acLink, acSpreadsheetTypeExcel12, tableName, fileName, True, "A4:L23"
but not when using acImport option
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, tableName, fileName, True, "A4:L23"
I get an 'error 3073 - Operation must use an updatable query' when I change the option from Link to Import.
As I want to add a column containing the month and year for which the data applies, I need to be able to add a variable which i cannot do using linked tables.
Thanks for your suggestions or an explanation of what I am doing wrong.
KR,
Martin
You probably need to use acSpreadsheetTypeExcel12Xml:
AcSpreadSheetType enumeration (Access)
Andre and Gustav,
thanks to both of you for your help!! Really appreciate it!
I thought I should test the code in a new Access DB and the code does work there. The issue seems to be caused when you switch between linking and importing in the same database.
In the new DB I can switch between importing and linking (acLink/acimport in the DoCmd.TransferSpreadsheet statement), but imported or linked tables must be deleted in Access BEFORE running the vba code. Maybe that is obvious to most of you, but was not to me :-)

Save a Form as a Report using VBA

In MS Access it's possible to convert a Form to a Report using the "Save Object As" functionality (Office Button -> Save As -> Save Object As "Save the current database object as a new object").
I'd like to have the same functionality using VBA but it seems to me that none of the following relevant DoCmd methods are suitable CopyObject, OutputTo, Save, TransferDatabase.
I was unable to find any useful information, except a blog post in which a code sample is provided using the SendKeys statement, but it seems not working.
Any advice?
It is not possible to convert a form to a report using VBA. Even using Sendkeys (which really should be avoided as there is a potential for the wrong application to get the keypresses instead) is not an effective method, as some controls don't get converted correctly.
Instead, you should be designing a report from scratch, with the required headers/sorting/grouping - it will look far better.
Regards,
Thanks to some tips from MajP on an access-programmers forum post, I found a way that is suitable for my needs, although it uses the sendkey function:
strFormName = "formname"
strReportName = "reportname"
DoCmd.SelectObject acForm, strFormName, True
SendKeys strReportName & "~"
RunCommand acCmdSaveAsReport
strFormName must contain the name of the Form to be saved as a Report whose name must be contained in strReportName.

VBA Dynamic Save As, Microsoft Project to Excel

I am trying to create a macro that will export a Microsoft Project file into an excel file. Through the use of macro recording I have got a line of code that accomplishes this using the export wizard, but I want the file path and file name to be dynamic so I can use this macro on different projects. I have been searching many other threads and the Microsoft website with no luck. Is this possible?
Here is what I have:
sub formatAndSave ()
FileSaveAs Name:="C:\Users\XXXXXX\SharePoint\Projects\ProjectType\HxH\myProject.xlsx",_
FormatID:="MSProject.ACE", map:="myMap"
end sub
One idea I tried was:
Active.Workbook.SaveAs FileName:=Title
Any help would be very much appreciated!
For the sake of simplicity, let's assume for all answers below your project is located at c:\projects\myProj.mpp
I think you're after the string replace function. Something like:
Dim excelFilePath As String
excelFilePath = Replace(ActiveProject.FullName, ".mpp", ".xlsx")
Debug.Print excelFilePath
'the output would be c:\projects\myProj.xlsx
If you're unfamiliar with string manipulation in VB/VBA, just search the web for "VBA string manipulation". Microsoft has a decent article here: https://msdn.microsoft.com/en-us/library/aa903372(v=vs.71).aspx
A few other things that may be handy for you are these variables:
ActiveProject.FullName 'shows full path & name, so you'd get "c:\projects\myProj.mpp"
ActiveProject.Path 'shows just the path, so you'd get "c:\projects\"
ActiveProject.Name 'shows just the file name, so you'd get "myProj.mpp"
Finally, one caveat I've seen is that the ActiveProject.FullName and ActiveProject.Name variables may or may not provide the file extension depending on your local windows environment settings. I've observed that if Windows Explorer is configured to hide file extensions, then these variables also withhold the extension; if Explorer is configured to show them, then they are provided in the variables. Make sure your code is robust to both cases, or make sure you have control over the environment where you code will run.

MS Access 2003 Error

I'm getting the following error when I try to run a macro that eventually uses the DoCmd.TransferText function. Here is the error below:
Cannot find object. Make sure the object exists and you spell its name and path correctly
I've used this method before to create a text file and add contents to it either from a table or a query.
Can someone correct me on this but doesn't the Transfer Text method create the file if it doesn't already exist?
Here is the code where i call it:
'Create Dispense File
DoCmd.TransferText acExportDelim, "DispenseExportSpec", "qryExport", DispenseFileName
Call Sleep(5000)
DoCmd.TransferText acExportDelim, "ExportDispenseCFSpec", "qryExportDispenseCF", ExportDispenseCFName
Call Sleep(5000)
(It fails on the first) This can't/shouldn't be a permissions issue as I have full read/write/edit abilities over the folder and its subfolders; it's not a path issue as I've verified multiple times, so what is the the problem here?
Access Version: MS Access 2003
Thanks
Try removing the spec argument and let it use the default? The documentation on that seems wonky. Just do a DoCmd.TransferText acExportDelim, , "qryExport", DispenseFileName

Conditional Saved Import in Access/VBA

I'm fairly new to this stuff, so hopefully someone our there can help me out.
The setup: I've got an Access 2007 database that imports data, then performs a set of queries and exports the results on a monthly basis.
The monthly data is saved as either Excel spreadsheets or in text files in a "Current" folder. I saved the rather tedious import steps for each file to facilitate the process of adding them as tables in Access.
There are a lot of files to import, so I wrote a simple VBA code to run all of the saved imports at once.
Public Sub DatabaseImp()
DoCmd.RunSavedImportExport "Import-Excel1"
DoCmd.RunSavedImportExport "Import-Excel2"
DoCmd.RunSavedImportExport "Import-Txt1"
DoCmd.RunSavedImportExport "Import-Txt2"
DoCmd.RunSavedImportExport "Etc....."
End Sub
Also, I created a macro to run this code and everything works fine.
BUT,
The Problem: Depending on the month, certain files will be included in the 'Current' folder and others won't be.
For example, suppose that this month there is no 'Excel2' file to import into the database.
Is there anyway to modify the code above so that it only tries to perform the import IF there is something there for it to import?
I understand that I could simply do it manually, ignoring the 'Excel2' import. However I would like to keep the process automated.
I'm looking for some kind of conditional IF statement that I could add on to the end of each line, e.g.:
DoCmd.RunSavedImportExport "Import -Excel1"
(ONLY IF there is an excel1 file in the 'current' folder to import)
Ideas, anyone?
Thanks,
You can use the Dir command. If the result is is an empty string, then it doesn't exist.
So, on a Windows machine, Dir("C:\Windows\explorer.exe") will return explorer.exe, but Dir("C:\Windows\bumblebee.exe") will return "".
So set up an If statement.
If Len(Dir(YourFile)) > 0 then
DoCmd.RunSavedImportExport "Import -Excel1"
End if