Access VBA to import dynamic excel spreadsheets as text - excel

I am looking for a way to use access vba to import an entire excel spreadsheet as text. The hard part with this one is that the import needs to be dynamic. I know that normally an import can easily be accomplished via a saved import or using a DoCmd.TransferSpreadsheet method, but unfortunately those do not suit my needs as the number of columns and certain headernames may change between files (these are files from clients and therefore I have no control over how they come in). Obviously performance is not a priority for this method.
Sudo-code:
Dim db AS DAO.Database
Dim file_to_import AS String
Set db = CurrentDb
Set file_to_import = "C:/Foo/bar/Access/Imports/FORIMPORT.xlsx"
'Read headers of spreadsheet into array
Loop through header row (A1:ZZ1) and create an array
'Create table creation string based on array
Creation_String = "CREATE TABLE TBL_Import ([Array_Element1] TEXT(255), [Array_Element2] TEXT(255), [Array_Element3] TEXT(255), ...etc, etc"
'Create TBL_Import
CurrentDb.Execute Creation_String
'Import data
'(Imports data from file_to_import into TBL_Import)
Somewhat similar to this question, may be able to use this as a starting point:
[MS Access VBA script to interface with Excel
Thanks in advance for the help with this. I know some code but obviously I am still learning the ins and outs of VBA.

The easy workaround is not to import the sheet but link it.
Then you have an attached (linked) table you can use as source in a simple select query where you modify and/or convert values.
Now, use this query for your further processing.

Related

Identify the number of a Word table (Selected or by Title)

I'm trying to program a macro in excel to export data to a word report. This data will go into several Word tables. All data transfer commands are now ready and working. However, in order to make this transfers more dynamic, I am inserting Titles and creating bookmarks for all tables into word report.
The problem is:
Via excel macro, I am able to select the Word tables through the following code:
oDoc.Bookmarks("Table_name").Range.Select
However, I cannot transfer the data to a specific cell of this table.
I know the by Word there is this command, but I can't use with Excel.
ActiveDocument.Range(0,Selection.Paragraphs(1).Range.End).Tables.Count)
Would anyone know how to help me?
Before that I was transferring the data referencing the table number but the problem is that whenever we revise the report to include new tables my reference ends up getting lost and I have to do table count again to transfer the data in the right table .
Well, that's it! Thanks in advance if anyone can help me.
It entirely depends on how your document is constructed. If the bookmarks enclose content, then you can use:
oDoc.Bookmarks("Table_name").Range.Tables(1)
MS Word does not give each one a name, so you have to come up with your own system.
When you create the document with all the tables, you can give each one a name... using the Table.Descr property.
ActiveDocument.Tables(1).Descr = "myTable"
Then when you look for them, loop thru them and find that Descr
For Each t In ActiveDocument.Tables
If t.Descr = "myTable" Then
' do something
End If
Next
Or you can set up some constants for the Index if that helps.
Or you can create one routine that sets up variables by scanning the document that you would need to run only once.

Import certain fields in Excel into Access based on field name -

I will be obtaining a few dozen MS Excel spreadsheets with 500+ columns and need to import just selected few columns into MS Access based on their column name. I have no control over this and yes, there doesn't need to be that many columns. I want to import a few selected fields only. In particular, I only want to import columns that have "c_" in the column name. The problem is I can't figure out how to do this. The other problem is the number of columns will vary each time, but I will always need the columns that start with "c_".
Normally when I use VBA code to import Excel data, it's not too difficult for me if I have to select a range. For example:
DoCmd.TransferSpreadsheet acLink, acSpreadsheetTypeExcel9, "ards_data_new", "H:\Misc. Projects\ARDS project\ards_data.xlsx", True, "ards_data!A1:A50"
The problem is that the range will vary for each file, so I can't do it this way. I wish there was a way I could use the command Like "c_" in obtaining the columns I need only, but I just don't know how. Any help would be appreciated.

Access VBA - Import problems

I'm importing some data form Excel into Access and I'm facing some strange issues.
Problem:
I'm using DoCmd.TransferSpreadsheet Method to import Excel data into Access like this :
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12Xml, "Excel_Data", "Filename", True
This "Excel_Data" table is not pre-created so Access creates It on It's own. Why ? If I pre-create It, then User has to import data to Destination table from Excel in exactly same order (column A in Excel is row 1 in Access etc..).
But If you don't pre-create It, then Access creates whatever table there is in Excel and you can Import only data that you wish - based on column names. Now here is where It get's stucked....
I don't know why, but on every other Imports I do like this, Access creates only Text fields - and then my Import to destination table works.
But in one of the Imports Access creates a Number field, and then Import into destination table doesn't work anymore. All Excel data are formatted as general.
Does anybody know how to avoid this ???
Basically I want just to Import excel data into Access, based on column names, in whatever column order there is in Excel.
thanks for help !
I would suggest to use the query like this instead of TransferSpreadsheet
SELECT * INTO Table1
FROM [Sheet1$D3:E24]
IN "C:\Temp\Test.xls" [Excel 12.0;HDR=YES;IMEX=1];
Note, that IMEX=1 allows you to suppress data type guessing and Access will create always text fields.
Also this will allow you to import data from specified ranges of spreadsheet and use WHERE clause for filtering unnecessary data
Thanks for all answers, but I have solved this on my own. Best thing to avoid any problems when doing Import is simply using ALTER Table command after DoCmd.TransferSpreadsheet method is done. I just altered all my columns to Text format, changed all my other tables to Text fields and now every manipulating with data works just fine.

Inserting SQL Server Results into Excel

I have an Excel spreadsheet like this:
Where the Student Name, Student ID and Classification are, I would like to fill that with the results from a SQL Server view. The columns to the right (Capstone, Milestone 2, Milestone 1, Benchmark, Semester Grade, Notes) will remain blank until the instructor fills them in later. These columns will not be written back to the database but will be saved, with the data that is loaded from the database, into the first three columns in it's Excel spreadsheet format.
Question 1: Is there a way to simply "embed" the data that is coming from the view?
Question 2: If not, can you provide a link to an example using a macro to read records and insert them (moving lower rows down with each new record)?
TIA
On the Data tab in Excel you can select "From Other Sources" icon from the "Get External Data" group to pull your student data from a SQL Server view.
That will give you the "embedded" data, until you decide to Refresh your connection and retrieve updated data from the view.
EDIT:
Use the CopyFromRecordset method for Range objects. Here is the link that provides working examples for what you are trying to accopmlish, without the range being being pushed to the side. Entries #3 and #11 provide the VBA examples.
http://www.xtremevbtalk.com/showthread.php?t=217783
You will need to use VBA to create an ADO connection, recordset, command, and parameter.
Dim adostudent as ADODB. (...) ^ use the above
Then, assign a named range to the areas that you would like to drop the information
StudentRow
Then, use a Do Until and an iterator and do until.eof and .movenext to drop the values from the recordset into the range.
irow = 10 'insert the header row # + 1here
Do Until adostudent.EOF = True
with adostudent
StudentRow(irow,1).value =.Fields("Student Name").value
...
...
.movenext
irow = irow + 1
loop
You can use the Data->Get External Data->From Other sources->From SQL feature in Excel.
Or use my Add-In:
http://blog.tkacprow.pl/excel-sql-add-in-free/

Import additional excel data into Access Table

We need to upload a small amount of additional records to a table from an Excel sheet. Is there a way to use the Access Import function to add the additional data to the table (truncate it). The table was created by uploading the same Excel sheet. But now, when records are added, we need to add them to the table. The tables are linked to SQL but I do not want to use an SSIS because there are only a few records and there must be a way to use Access functions. Suggestions please.
It may be easiest to link the excel sheet and run an append query to add data from Excel to existing table. Once linked, this can be done in the query design window.
You did not specify versions of Excel or Access.
I did this with a test 2003 Excel sheet with cells containing 1000+ characters. An import in Access 2003 detects the data type as a memo field, which is correct, when there are that many characters, so it should work for you. It may be your Excel data has other ingredients causing an import issue. How is the excel data derived?
Have you tried importing to Access? It should work fine. If your ultimate target is another database why use Access as an intermediary?
I agree a linked table seems like a really simple method to update a table if you are using Access, but that is your choice.

Resources