Problem Importing Excel File into Access via VBA - ms-access

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.

Related

I want to export from Access to Excel a Table I created in Access without Access asking me if I want to overwrite the existing file

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

How to copy a report or form from one Access database to another?

I made a form in a database that I wanted to copy to another MS Access database. I can copy and paste the form design but seems like even though all the fields are copied the form width and spacing is messed up. Is there another way to copy the form design from one database to another?
Also you can use Application.DoCmd.TransferDatabase :
DoCmd.TransferDatabase acImport, "Microsoft access", "C:\sourceDB.mdb", acForm ,"SourceFormName", "FormNameInDestinationDB"
Or less handy two steps:
Export in source DB
Application.saveAsText acForm ,"SourceFormName", "C:\fileWithForm.txt"
Import in destination DB
Application.LoadFromText acForm ,"FormNameInDestinationDB", "C:\fileWithForm.txt"
Yes, opfen your new Access file and go to External Data > External Datasource > From Access File, specify your file path (the database path where you want to copy forms etc...) and click the option Import tables, queries, forms, reports, macros and modueles into the current database. Next chose what you want to import and hit OK.

How to call an Excel report from an other Excel report?

I have 12 Excel reports in my Application Lifecycle Management.
Each of them execute a Query and some VBA code when I click on generate.
I was wondering if it is possible to create a single Excel report that can call each of the previously mentioned reports so that I don’t have to manually generate each of them.
Is there a way?
Otherwise I think I’ll have to regroup and recode them in one big Excel report, which would not be practical for future modifications.
You can call macro from "starting file".
You need open file, start makro like bellow, close file and do the same with another
Application.Run "filename.xlsm!macroname"
EDIT:
sub test()
Workbooks.Open filepath & "\filename.xlsm"
Application.Run "filename.xlsm!macroname"
Windows(ActiveWorkbook.Name).Close
end sub

Excel files not closing from MS Access

I have an Excel Dashboard where i have buttons to perform some data cleansing in MS Access. The Access database fetches data from other two Excel files. When i run the MS Access separately i am not facing any issue. My user wanted all the operations to be done from the final Excel dashboard.So when i call the Access module from the final Excel report, the input excel files opens as read only and Access operations not completed. Every time i wam forced to kill the process in the task manager. i want the Input Excel files to close normally and Access to perform the set of operations and only the final value to be retrieved in the Excel dashboard.
Use this piece of code after uploading the file
'This will set the sheet object to nothing
set xlsht=Nothing
'This closes the workbook
xlwkb.Close
set xlwkb=Nothing
'This closes the excel application
xlapp.Quit
set xlapp=Nothing

Access 2007 - Display PDF content on a form

On Access 2007, is there a way to display the content of a PDF, even if it is just the first page, on a form? This PDF saved in a table as attachment.
Disclaimer: This answer will only work for PDF files stored outside of your database as separate file. They can be located over a network connection, but I do not know how to access them directly from your database table. This site gives a thorough guide to using the attachments, but doesn't show how to actually display them automatically. It is likely functionality not provided by Access.
You can display anything Internet Explorer can display with a Microsoft Web Browser Control.
Once you've added the control, you can navigate to whatever you want to display during the load or open event of the form.
For example, if the control is called WebBrowser0 then the following would work:
Private Sub Form_Load()
Me.WebBrowser0.Navigate2 "C:\example.pdf" 'Substitute the actual address here.
End Sub
This is an extremely versatile method for displaying other content within Access. You can find more information here.
The only two methods I know of for previewing a PDF (WebBrowswer as suggested by Daniel and the Adobe Active X control) require a file path to be passed to the control.
I recommend extracting the file from the attachment field and saving it to a temporary location such as C:\Documents and Settings\username\AppData. This can be found by using the vba Environ command.
Extracting the file is done with the SaveToFile method in the embedded DAO recordset (which is how attachments are stored in memory).
Example Code
Assume each record has a field called AttachedFile and each record has only one attached PDF. The form is using a WebBrowser control named PreviewBrowser to view the PDF
Private Sub Form_Current()
On Error GoTo ExitSub
Dim FormRS As DAO.Recordset
Set FormRS = Me.Recordset
Dim RecAtt As DAO.Recordset
If (Me.AttachedFile.AttachmentCount > 0) Then
Set RecAtt = FormRS.Fields("AttachedFile").Value
RecAtt.OpenRecordset
Dim Path As String
FilePath = Environ("APPDATA") & "\Preview.pdf"
If (Dir(FilePath) <> "") Then Kill FilePath
RecAtt.Fields("FileData").SaveToFile FilePath
Me.PreviewBrowser.Navigate2 FilePath
End If
ExitSub:
RecAtt.Close
End Sub
Of course the code needs to be a bit more complicated if there are multiple attachments to a given record, but that would be done by manipulating the RecAtt recordset.