I am currently working on a database for a small Company. They already have a first Version with several tables already implemented. What I would like to do is to create a master table in which data are going to be imported from Excel.
Everytime this table is updated, all the other tables should get updated aswell.
I am not sure how to implement this in Access, so this is whz I am asking for help.
Thanks in advance!
Maurizio
Following sub will import data from excel to access table. Table structure and excel data structure must be same (Column name, data type etc).
Private Sub cmdSpecificSheetOnly_Click()
Dim strExcelPath As String
strExcelPath = "C:\Users\HARUN\Documents\tblEmpInfo.xlsx"
'For Specific Range
'Call DoCmd.TransferSpreadsheet(acImport, acSpreadsheetTypeExcel5, "tblEmpInfo", strExcelPath, True, "Sheet1!A1:E31")
Call DoCmd.TransferSpreadsheet(acImport, acSpreadsheetTypeExcel5, "tblEmpInfo", strExcelPath, True, "Sheet1!")
End Sub
Related
Backstory:
I am currently working on a "make-shift" database using excel. I will have one central excel workbook which will house the tables on separate sheets storing data. Other workbooks will interface with this database excel workbook and will input new lines or replace existing lines in the tables by each user interface.
The user-interface workbooks will have query tables pulling from the database workbook tables as needed to provide the users with information.
I am sure there must be a better way of doing this. Any tips tricks or pointers are much appreciated.
Problem:
I have written in my VBA Database Input code to Refresh All queries however it does not update the queries.
I have made a separate sub to update queries but when it is called by the Databse Input code it does not.
Sub Refresh_Queries
Dim ActWB as Object
Set ActWB = ThisWorkbook
ActWB.RefreshAll
End Sub
Are these actual query objects, or something else, perhaps? Make sure you are acting on the correct type of object. Maybe these are workbook connections. Just making a guess here...
Sub Something()
Dim Connection As Variant
For Each Connection In ActiveWorkbook.Connections
Connection.OLEDBConnection.BackgroundQuery = False
Connection.Refresh
Next Connection
End Sub
Or...
Sub Workbook_RefreshAll()
ActiveWorkbook.RefreshAll
End Sub
I hav seen some weird things in the past, like doing a single refresh doesn't work, but doing it a second time works perfectly fine. See the link below for some additional ideas of what you can do to refresh queries, connections, and the like.
https://analysistabs.com/vba-code/workbook/m/refreshall/
I'm trying to call part information from SQL to Excel. When a user enters a part number, Excel queries the SQL database and retrieves details about the part (price, description, etc).
I was able to import the data, and use a vlookup to pull in the values, however the import is quite large.
There were some VB scripts out in the webs that would pull data directly from the SQL database into the cells, but those are out of date for Excel 2013.
I'm using the following statement for SQL to pull all the data on the table:
SELECT
PartNum,
PartDescription,
UnitPrice
FROM erp.Part
Ideally would like a script that only asks for a specific part, using WHERE PartNum = part and returns the requested data in a row.
Thanks in advance for any info/help.
your question is vague at best, and you didnt even provide code youve tried. Here is a basic sql from excel vba script
Sub TrackerInput()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Set db = DBEngine.OpenDatabase("pathtoyourfilewhichmightneedotincludecredentialsGoogelitforyourself")
Dim vAr$
vAr = "your input of choice" '.ie part number you were suggesting
Set rs = db.OpenRecordSet("SELECT PartNum, PartDescription, UnitPrice FROM erp.Part WHERE PartNum=" & vAr & ";")
TheLocationWhereYouWantTheDataReturned.CopyFromRecordset rs
End Sub
Your references will need to be set OBV
Solved by handing in my resignation, apparently im too stupid or don't know how to git good enough to do this job anymore. Going to follow my passion and sell umbrellas on the beach.
I have an Excel sheet with powerpivot tables and charts. I want to show when the data was updated.
I use VBA code and a button to update the data, and I use the function now() in a cell which is updated when the code is running.
I have two problems.
When I open the Excel file the date is updated even if I do not update the data on start. (I do not want the data to update on start)
When the update fails the date is still updated.
My VBA code look like this:
Range("table name").Select
ActiveSheet.PivotTables("PivotTable2").PivotCache.refresh
Range("date cell").Select
How to avoid update when the data is not updated?
Instead of solve it as user3819867 suggested, I solved it by adding the information from the database, which is a SQL server database so I added a view with one colum and one row (with a getdate()) and send it along with the other tables so I get one extra table named "date".
How about its event?
Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
Range("date cell").Select
End Sub
I'm debugging some code and need to find out where a
DoCmd.TransferSpreadsheet acImport, , ".....
fails so I've decided to import it 'manually' line-by-line to see where it falls over.
I suppose something like this is what I'm looking for:
mySpreadSheet = ConnectTo(Spreadsheet.xlsx)
while(!mySpreadSheet.EOF)
get(mySpreadSheet.nextLine)
SQL("UPDATE MyTable with mySpreadSheet.nextLine")
I've tried Googling to no avail. Any help is much appreciated!
Additional info:
The column names of the spreadsheet and the Access table are identical.
Every data type is nvarchar(MAX) (Or "Memo" as Access calls it)
The table is a linked table to SQL Server 2008
ADO works well if you have a well defined sheet layout of your data (HansUp answer). If you need added control before loading the objects, you can hook into the excel workbook, and then pull out whatever data you like. It really depends on the level of control you need.
Public Sub LoadExcelToAccess(xlPath As String)
'uses late binding to open excel workbook and open it line by line
'make reference to Microsoft Excel xx.x Object Model to use native functions, requires early binding however
Dim xlApp As Object 'Excel.Application
Dim xlWrk As Object 'Excel.Workbook
Dim xlSheet As Object 'Excel.Worksheet
Dim i As Long
Dim sql As String
Set xlApp = VBA.CreateObject("Excel.Application")
'toggle visibility for debugging
xlApp.Visible = False
Set xlWrk = xlApp.Workbooks.Open(xlPath)
Set xlSheet = xlWrk.Sheets("Sheet1") 'modify to your perticular sheet
'depends on what your trying to do with the sheet
For i = 1 To 10
'quick and dirty: best to load items into custom class collection, then do processing there
sql = "Insert Into [Temp] (Col1) VALUES (" & xlSheet.Cells(i, 1).Value & ")"
DoCmd.RunSQL sql
Next i
'make sure to dispose of objects
xlWrk.Close
xlApp.Quit
Set xlSheet = Nothing
Set xlWrk = Nothing
Set xlApp = Nothing
End Sub
You can create an ADO connection to your spreadsheet (see Connection strings for Excel 2007), then open an ADO recordset with that connection (see StackOverflow: ADODB recordset in VBA says excel field is empty when it's not for example).
Then move through the recordset rows, and create a SQL INSERT statement using the row's values.
strInsert = "INSERT INTO MyTable (first_field, second_field) VALUES ('" & -
rs2.Field(0).Value & "', '" & rs2.Field(1).Value & "');"
Debug.Print strInsert
CurrentDb.Execute strInsert, dbFailonerror
That code snipped assumes first_field and second_field are text data types. If they are numeric, lose the single quotes.
I think that does roughly what you asked. However, before resorting to code I would check whether the spreadsheet imports cleanly into a new native Access table. (Also, check whether the data types and constraints on that new table are compatible with those of the linked SQL Server table.) If that works, maybe try importing the spreadsheet directly into SQL Server from the Management Studio, or whatever tool is appropriate.
For troubleshooting purposes, try linking to the spreadsheet and see what problems you encounter, including data displaying wrong when you view it in datasheet view.
You can also try just copying your Excel data to the clipboard and pasting it into your Access table. Whatever fails will get written into a 'Paste Errors' table by Access.
Two additional options:
Link the spreadsheet in Access like a table. In Access 2007, go to "external data" pane and select "Import Excel Spreadsheet". You should import to an existing datatable, a new datatable or just link to Excel file. Then, you would work with this new "Excel" table like an Access table (regarded the performance issues, in last case).
Try to fix the Docmd.TransferSpreadsheet problem. I've been using this method for some years, and it works fine, despite it ought to be a little tricky in some cases (I belive its your case). Please, its worthy if you give more information about your problem with this method, including your Access and Excel version.
I hope I've helped. Good luck.
I've got an Access 2007 database on which I have created around 15 SQL queries to process specific data, I have created a main frame navigation menu using menus in Access, I now need to extract all th queries to Excel using VBA code, I have managed to do this with the code below by creating a button and specifying this code to it.
Private Sub query1_Click()
DoCmd.TransferSpreadsheet acExport, _
acSpreadsheetTypeExcel9, "Total Users and Sessions", _
"C:\UsersandSessions.xls", , "Total Users & Sessions"
End Sub
Now my problem at the moment is that fine the query is exported to Excel, but it is done so without any formatting applied at all, I would like to add some formatting at least to the headers and maybe a title inside the spreadsheet, and one thing I dont really like is that all records are being started from the first cell. Also I would prefer that if I hit that button again in Access and the Excel spreadsheet has already exists with that query output then when clicked again it will write again to a the next available sheet.
Any suggestions or ideas a very welcome.
The short story, is you can't. You might be able to do some scripting on the Excel side to format the resulting file. If you want something pretty, you probably want to create a report.
You could, instead mount the excel sheet as a table, and then on a separated sheet in the excel file, reference the first sheet, and format the second sheet for viewing.
if you use DoCmd.TransferSpreadsheet and create an original and then edit it so that the formatting is correct, you can then run DoCmd.TransferSpreadsheet again and it will update the file with the values but keep the formatting.
However, if a human then changes the file by adding new tabs, or adding calculations, etc, then the DoCmd.TransferSpreadsheet will no longer work and will fail with an ugly error message. So what we do in our enviroment is DoCmd.TransferSpreadsheet to an original file with formatting, and follow that up in the VBA by copying the file to the users desktop, and then opening that copy so the user doesn't mess up the original source excel file.
This approach is a minimum code, clean, and easy to maintain solution. But it does require a extra "source" or original file to be hanging around. Works in Access 2007.
You also would like the results to end up on a new tab. Unfortunately, I think it will take some excel automation to do that. The VBA inside Acccess can call a function inside the VBA in Excel. That VBA could then copy the tabs as needed.
My idea would be a hybrid of Excel automation from Access and creating a template in Excel as well that would have a data table linked to your query.
To start create your data table in Excel. You can start three rows down and two columns to the right if you want or wherever. Go to your data tab and click access, find your db, choose your query you want to link to, choose table as the radio button but click properties next instead of ok, uncheck the enable background refresh, this part is critical ... under the definition tab in the connection string you will see a part that says Mode=Share Deny Write change that to Mode=Read, this will make sure that the query refreshes without errors from an MS Access VBA while the db is open and will keep your users from writing back to the db in case your query is a writeable query. Once you set that up you can adjust the table formatting however you choose from the table design tab and it will keep that formatting.
For the purposes of this we are going to assume you started the table in cell B4 ,and your named the worksheet CurrentDay, for purpose of the following VBA example be sure to replace that reference with your actual placement.
Next go back to Access and write your VBA first ensure that in your VBA window you have the reference to Microsoft Excel 12.0 Object Library is selected by going to Tools > References and selecting it from the alphabetical listing.
Create your sub as follows:
Sub query1_click()
Dim xl as Excel.Application
Dim wbk as Excel.Workbook
Dim wks as Excel.Worksheet
Dim RC as Integer
Dim CC as Integer
Set xl = New Excel.Application
Set wbk = xl.wbk.Open "X:\Filelocation\FileName.xlsx" 'name and path you saved the file you previously created
xl.Visible = True
'The above is not necessary but you may want to see your process work the first few times and it will be easier than going to task manager to end Excel if something fails.
RC = xl.Application.CountA(xl.wbk.Worksheets("CurrentDay").Range("B:B")) + 3 'This will count the rows of data in your table including your header so you can copy the data to another tab dynamically as the size of your table expands and shrinks we add 3 to it because we started at row 4 and we need the location of the last row of the record set.
CC = xl.Application.CountA(xl.wbk.Worksheets("CurrentDay").Range("4:4")) + 1 'This counts the header row and adds one space because we will use this as a location holder for our copy / paste function
Set wks = xl.wbk.Worksheets.Add
wks.Name = format(date(),"MM_dd_yy") 'this will name the tab with today's date... you can eliminate this step if you just want the sheets to be the generic Sheet1, Sheet2, etc.
With xl.wbk
.Worksheets("CurrentDay").Range(Cells(4,2),Cells(RC,CC)).Copy
.wks.PasteSpecial xlPasteValues 'This pastes the values so that the table links do not paste otherwise every tab would just refresh everyday.
.wks.PasteSpecial xlPasteFormats 'This gets your formatting.
.RefreshAll 'This will refresh your table
Wend
With xl
.Save
.Close False
.Quit
Wend
Set xl = Nothing
Set wbk = Nothing
Set wks = Nothing
End Sub
That should get you to have your data to not start on A1 of your sheets, save your old data each time, and automate the steps from access.