I have a DoCmd to export a query into an Excel file but when I go to open the Excel file I get an error message stating, "Excel cannot open because the file format and extension is not valid." What can be causing this to happen when I export the file and attempt to open it?
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "qryComplete", "C:\Users\Downloads\Reports\Report_Name " & Format(Date, "mmddyy") & ".xlsx", True
If you want acSpreadsheetTypeExcel9 for the SpreadsheetType option, use .xls as the file extension.
If you want .xlsx as the file extension, use acSpreadsheetTypeExcel12Xml for SpreadsheetType.
The error happens because acSpreadsheetTypeExcel9 and .xlsx is not a valid combination.
Related
Is it possible without VBA? I can send the file the first time with no problem. I can manually go in and delete the file then run Access - no problems. I want Access to run and then overwrite the existing Excel file without asking me if I want it overwritten.
I do not think is possible without VBA.
Delete the file before exporting.
Dim strFile As String
strFile = "your filepath here"
If Dir(strFile) <> "" Then Kill(strFile)
'export code here
I've created a multi-user Access Form that will essentially be open for most of the day on several peoples computers. On this form, I've created a button that will import an excel spreadsheet via VBA. The code is:
Option Compare Database
Private Sub Command0_Click()
DoCmd.SetWarnings False
DoCmd.RunSQL "DELETE ExcelTable.* FROM ExcelTable;"
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12Xml, "ExcelTable", "C:\Users\JohnDoe\Documents\MyTable.xlsx", True, "Data!A6:DB" & numberofrows
DoCmd.SetWarnings True
End Sub
This code imports the data fine. However, users will frequently need to update the spreadsheet throughout the day. There are 2 problems that I am frequently running into:
If a user attempts to overwrite the original spreadsheet with an updated version, they receive an error message "Cannot access read-only document".
If the user attempts to open the spreadsheet, they receive the error message that the excel file is locked for editing.
I was under the impression that by running the above VBA code, the user is importing a static table (not a linked table), meaning that once the table is imported into Access, it's no longer referencing the original Excel file.
How do I get around these error messages?
The users will need to update the excel file several times throughout the day and right now, everyone has to completely exit Access in order to access the original excel file.
I want users to be able to put the Project folder on any location of their choice. If I use:
Application.FollowHyperlink ("C:\Program Files(x86)\Project\reference.pdf")
The pdf document launches only if the user put the Project folder in the Program Files(x86) folder. Is there a way for access to refer to the current path instead? I have tried the below code with no luck.
Private Sub referencefile_Click()
Application.FollowHyperlink (".\reference.pdf")
End Sub
I have tried:
Application.FollowHyperlink (CurrentProject.Path & "\reference.pdf"),
NewWindow:=True
This works well with txt files but not with pdf files. Any idea?
After a Quick Check and ensuring that my pdf file was not corrupted.
Application.FollowHyperlink (CurrentProject.Path & "\reference.pdf"), NewWindow:=True
The above code works fine as long as the pdf file exits in the same location as the front end. If you have ever thought of eliminating the security warning that access gives when you manually enter a hyperlink to a control button's properties, this code may be a good solution.
I was trying to export A report from Access to Excel using the code below.
pat = CurrentProject.Path
Set xlo = CreateObject("Excel.Application")
Nname = "MonthlyData" & Left(Now(), 5)
DoCmd.OutputTo acOutputReport, "MonthlyAll", "Excel 97 - Excel 2003 Workbook (*.xls)", pat & "\" & Nname & ".xls", True
It works fine in my system but isn't working on any other I have tried since.
I am not able to export to any other excel formats in my system as well.
I have the following references selected in my system,
Visual Basics for Applications
Microsoft Access 15.0 object Library
OLE Automation
Microsoft Office 15.0 Access database engine object Library
Microsoft Excel 15.0 object Library
Microsoft Office 15.0 object Library
I haven't checked for these references yet.
Not sure if that is the reason. I am using windows 10 which is not the problem. I have checked for compatibility.
You will probably need to use late binding instead of early binding to ensure that whatever version of excel / office is in use your references are created on the fly for the version in use.
This thread will proabably help Convert Early Binding VBA to Late Binding VBA : Excel to Outlook Contacts
You provide the DoCmd.OutputTo parameter OutputFormat as string, this is language-specific.
Use the constant acFormatXLS instead, it should work on all systems.
DoCmd.OutputTo acOutputReport, "MonthlyAll", acFormatXLS, pat & "\" & Nname & ".xls", True
Actually, this won't help. I assumed the constant was a numeric value, but
Const acFormatXLS = "Microsoft Excel (*.xls)"
Note: If I try it with your format string "Excel 97 - Excel 2003 Workbook (*.xls)", it works with a German Office 2010. So this is probably not the problem.
Left(Now(), 5) might introduce illegal characters for file names, depending on the regional settings.
Use e.g. this instead:
Nname = "MonthlyData" & Format(Date(), "yy-mm")
developing an application in Access. The access file will be located on a db however when users need to use it, I want them to copy it on there desktop. If they do run it off the G:\ drive (our networked folder), it should give them a message.
So are there Win API that will help me solve this?
I am going to put this code in the Form_Load event of a form.
If you want to prevent the users opening your database from the G:\ drive, you can do a simple check with code like this in your startup form:
Dim strMsg As String
If CurrentProject.Path Like "G:*" Then
strMsg = "Please copy this database file to your " & _
"local disk and open the copy instead of this one."
MsgBox strMsg
Application.Quit
End If
If you also want to prevent them opening the database from a different drive letter mapping or a UNC path, you could add a file such as NotFromHere.txt to the folder where your database file is stored.
Dim strMsg As String
Dim strFilePath
strFilePath = CurrentProject.Path & Chr(92) & "NotFromHere.txt"
If Len(Dir(strFilePath)) > 0 Then
strMsg = "Please copy this database file to your " & _
"local disk and open the copy instead of this one."
MsgBox strMsg
Application.Quit
End If
You can use the FileSystemObject DriveType property
http://msdn.microsoft.com/en-us/library/ea5ht6ax(VS.85).aspx
If you need the desktop folder, you might like to look at:
CreateObject("WScript.Shell").SpecialFolders("Desktop")
You might want to check out the excellent access auto FE updater as an automated way of copying down updates etc
http://autofeupdater.com/
I rolled my own very similar system before I found this one and it does make updating the FE so much easier
I think there's a call that will list the (physically) attached drives on a machine, you could call that and compare the drive specifier on the path the user gave against the list.