Identifying Excel filepath in SSIS - sql

I'm using this ForEach loop tutorial in an attempt to import all my Excel files from a folder to an SQL table via SSIS. I followed the steps exactly but can't get the Excel source block to recognize the variable filename. The error I get shows that the filepath did not work at all:
Could not retrieve the table information for the connection manager ConnectionManagerName
The source file formatting, type, and folder are consistent. Has anyone else had this problem? I suspect I'm missing something between steps 2-4 in the link, but I wouldn't know what it is.

I've solved it! I used a ConnectionString in Step 3b instead of ExcelFileName. For anyone reading this answer, the exact ConnectionString format will vary (see here) but will be something like
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\myFolder\myExcel2007file.xlsx;
Extended Properties="Excel 12.0 Xml;HDR=YES";

Related

DBF file is not a valid Path - VBA

I'm trying to make a connection with a dbf file trough Visual Basic, i'm using the following connection string:
dbConn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="C:\Users\joelgjunior\Desktop\PROVISAOMAT_012020.dbf";Persist Security Info=False;
The connection string seems correct, but the code can't recognize the dbf file i don't know why, the name and the path are written correctly but the vb always say that is not a valid path, otherwise i did it to an accdb file and it worked. Can someone help me in this case? Thanks
A connection string points to the PATH where the data files are located. Then, any .dbf within that path location (or child-folder within) is accessible.
Think of it this way. A normal SQL command might be
select * from PROVISAOMAT_012020 where...
The SQLCommand object uses the CONNECTION to point to the location. So if the connection has the dbf name as well, your query will basically be interpreted as
select * from C:\Users\joelgjunior\Desktop\PROVISAOMAT_012020.dbf\PROVISAOMAT_012020 where...
Also, if using .DBF files, is it dBASE, or Visual FoxPro (VFP), if VFP, I would download those drivers and use them instead of ACE.

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.

Import online xls data into MS access via VBA

I need to import exchange rate data stored in an Excel spreadsheet online into an Access data table. However first I need to manipulate it so I would like to import it into an array and then write the array to the table. The code I used for Excel doesn't seem to work with Access...
Dim arr as variant
Workbook.Open ("http://www.rba.gov.au/statistics/tables/xls-hist/f11hist.xls")
arr=activeworkbook.worksheets("Data").Range("A12:X" & Range("A1045876").end(xldown).Row)
'data manipulation ommitted
'add to data table
Clearly this doesn't work in Access, but I've got no idea how to open the file and read the data. Any help appreciated!
Your question is overly broad, so the answer is generic. You can use Microsoft Excel object library in MS Access application by adding the reference to that library and start using its methods, similar to what you have done in Excel. More details in: https://msdn.microsoft.com/en-us/library/office/ff194944.aspx. Hope this may help.
I guess this line can help you.
Docmd.Transferspreadsheet acImport,,"name of excel table","link to find the table (ex:c:.....)",true
that will help to take a excel table and import its on access .
Excel does this by - behind the scene - first downloading the file then reading it.
Access can't do this, but you can use VBA to download the file Download file from URL and then create a link to a Worksheet or a Named Range in it. Or open the file via automation.

Using ReadLine, where did my text go?

I'm pretty new to visual basic (and coding in general) so if I've made any really simple mistakes let me know.
Right now, I'm getting a pretty weird problem with my vb.net code.
The filestream is able to correctly open the file and read from it - but what's weird is that while the code is able to read a bunch of lines from the beginning of the file, when I manually open the file in notepad I'm not. Here's the code:
Dim fs, f, s 'filesystemobject, file, stream.
fs = CreateObject("Scripting.FileSystemObject")
f = fs.GetFile(CurrDataPath) ' This change made to ensure the correct file is opened
s = f.OpenAsTextStream(1, 0) ' 1 = ForReading, 0 = as ASCII (which i think is right?)
Dim param(14) As String
Dim line As String
line = s.ReadLine()
While i <= 14
i += 1
MessageBox.Show(line)
line = s.ReadLine()
End While
(I've read that arrays are a bad idea but they've been convenient and haven't caused me any problems so I've been using them anyways.)
What's weird is that when this code is run, it will (in the message boxes) show me the information I want to see - which isn't bad at all. The information that I want looks like this:
BEGINPARAM
parameter1, 0
parameter2, 7.5
ENDPARAM
EDIT:
After using Path.GetFullPath(DFile), I found that there were two files in different directories with the same name DFile. The file I had been opening in Notepad was saved in the directory where I expected it to be saved, while the file the code was reading was saved in the VB project's folder.
Once I changed the code to rely on CurrDataPath which includes the expected path, the code read from the file exactly what I did in notepad.
I do have word wrap on in notepad, so I know that's not the issue, however, I will look into getting notepad++.
The file named DFile is created in a c++ program that I'll be digging through to find out why one part of the file is written to a different folder than the rest.
Obviously I'm missing something important, and if anyone could help, that would be great.
*Note: This is a vb6 migration project so if anyone asks I can provide the old code.
Assuming the most recent version of VB.Net, the modern way to write that is like this:
For Each line As String In File.ReadLines(CurrDataPath).Take(14)
MessageBox.Show(line)
Next
I'm not 100% clear on what you're saying. There's nothing in this code that outputs to a file, so what you have to be saying is that when you open the file referenced by "DFile" on line 3 above, that file doesn't have the lines containing "parameter1, 0" and "parameter2, 7.5" in it?
Since we know that's not technically possible, do verify the answer to the question above and make sure you're really opening the same file in notepad as the script is opening. The second thing to do is to turn on Word Wrap in Notepad or download Notepad++ (a text editor I think everyone should have anyway) and make sure that the data's actually missing, and not just not showing on your screen because it's not using Windows style line endings.

SSIS Dynamic Excel Destination File Name

How can I configure a dataflow task that takes data from a MS SQL Server 2008 datasource and puts it in an Excel file where the filename looks like 'date filename'.xls?
Excel is the biggest pain to deal with in SSIS. Usually I store a template file that just has the column headers and nothing else. I start with a task to copy the template file to the processing directory. You can use variables to create the file name in an expression at this point. Alternatively, you can create the file in the dataflow and then rename the file in a step after the data flow. With text files, I have dynamically created the connection in an expression, but Excel seems to be funny about that.
Provided that your column definition don't change.... you can go to
Right Click on Excel Connection Manager
Expression
Select connectionstring
bulid expression (for Example : (DT_WSTR, 50) GETDATE() + #[user::FileName] +".xlsx")
Select the properties for Excel Connection Manager instance, Click on the ellipsis for 'Expressions 'property and set an expression for 'ExcelFilePath' to a variable with a valid path to an excel file, this takes cares of the connection string.
You do need a variable valid excel file at the design time, otherwise connection manager does not work, you can overwrite it at run time using a script task to point to the excel file that does not exist at design time.
HLGEM's answer certainly helps. As #sql-rookie requested, I'll provide a sample about how to copy the excel file to a new file named with current date. Hope this will help others with the same question in the future.
Basically you just need to add an additional File System Task after the Data Flow Task. And use a variable for the Destination File Path: "\\\\file\\"+SUBSTRING((DT_STR,30, 1252) GETDATE(), 1, 10) +".xlsx"