DAO import csv file data - csv

I have an ms Access database and using VBA in this database i and trying to open several csv files and copy data from each one to a new ms Excel workbook using DAO. I am using the connection string as below and i get a Run Tine Error 3044 - which is saying it is not a valid path.
I have tried saving the file down as a .xlsx and changing the string as below and this works fine. Clearly as this works with the .xlsx and not with the csv there is something wrong with my connection string used for the csv. The variable strSourceFile is the full name of the file; ie, path and file name.
I will be grateful if someone will give me the correct connection string.
Connection string used for the csv which fails.
OpenDatabase(strSourceFile, False, True, "Text;") This fails
Connection string for the .xlsx which works fine.
OpenDatabase(strSourceFile, False, True, "Excel 12.0;) This works fine.
Regards
Spike

Related

SSIS For Each Loop crashes the flat file connection

I created a simple SSIS package to import a flat file (.txt) into a database table. Tested that and it works perfectly. Since I have several files to import, I added a foreach loop to go through all the files, added the variables as recommended in several examples found on the net but now my flat file connection manager returns an error of "A valid file name must be selected." and the package will not run. I have so far been unsuccessful in finding the solution for this issue and would appreciate any suggestions by the SSIS gurus of this forum. Many thanks in advance!
Here is what I have in the way of variables:
SourceFileFolder which is the path to the folder that contains the files
FileName a string containing one of the names of the files I am seeking to import
SourceFilePath which is an expression driven variable that incorporates the previous two variables concatenated together. I can click "Evaluate Expression" and copy and paste it into windows explorer and open the file
ArchivePath which is an expression driven variable that creates the path to archive the file to once it is processed.
As the message says this is related to your connection manager not gathering the connection string. This can be handled using the following:
First of all clear the expression on the SourceFilePath variable.
With your Foreach Loop Container, set it up as follows:
This will use your variable SourceFileFolder as the Folder, you could also just hardcode the folder name C:\ for instance. Also make sure your folder is qualified correctly, I.E. make sure it finishes with a slash C: won't work but C:\ will work.
Next you need to map the fully qualified name to your other variable SourceFilePath
This should now store the full name of the file the loop has found into the SourceFilePath variable. For Instance C:\File.txt, you can now use this as a connection string expression on your file connection manager.
Under the properties of the connection manager make sure the expression is set to ConnectionString and then use the SourceFileName variable.
ALSO MAKE SURE DELAY VALIDATION IS SET TO TRUE
This hopefully should mean you can loop through the files.

Send mail Task with an attachment in SSIS package

In my ssis Package I have Data Flow task in sequence container. OLEDB is the Source and Flat file is Destination.
I have given a variable for the Location of the flat file (#[User::strUnknownFileLocation]). Also for the name of the flat file i have added a variable (#[User::StrUnknownFileName]). After the data flow task I have send mail task. I am attaching that Flat file as an attachment in Send mail Task.
This is the Expression I am evaluating in my Filename Variable (#[User::StrUnknownFileName]). I have set it up the Evaluate Expression is True in property.
"UnknownValue_"+ (DT_STR,4,1252)YEAR(getdate()) +(MONTH( getdate()) < 10 ? "0" : "")+ (DT_STR,4,1252)MONTH(getdate()) +(DAY( getdate()) < 10 ? "0" : "") + (DT_STR,4,1252)DAY(getdate()) + ".txt"
This is in my flat file connection property expression --Connection string
#[User::strUnknownFileLocation]+ #[User::StrUnknownFileName]
This is in my flat file connection property expression
Variable for File location I don't have any expression. I have added value of location in Variable
(#[User::strUnknownFileLocation]).
Like M:\ETL\Tmp\
In send mail task I have an attachment and expression for attachment is given like this,
#[User::strUnknownFileLocation]+ #[User::StrUnknownFileName]
I ran the package it was fine yesterday. It was attaching an file as UnknownValue_20130405.txt.
But when I open the package today, it had an error in the send mail task. When i click on error, it says it can't find the file UnknownValue_20130406.txt or I don't have permission to open that location. Its kinda weird.
So, I execute the DFT task. So file got generated. Then it works because file is there. So why its not running in the first place. So now tomorrow when I will open my package and try to run, it wont run because there is no file on that date? But if i go and run only tht DFT task it will generate a flat file and it will have a flat file in that location. But this is not how it should be.
Is there something I am doing wrong? I am unable to understand this. Any help will be very appreciated.
Thanks in advance.
I believe you are running into an issue because SSIS, as part of the pre execute phase, validates that all the resources it needs are available. On a new day, the file isn't available until after the package has run.
To get around this, you can tell you SSIS to wait until it actually needs to use a resource before validating it. On any element in SSIS, it should have a DelayValidation property which is set to False by default. Set the value to True on the "Send Mail Task"

Automatically populate an Access database from a script

I have a script that downloads data from a database into a series of CSV files. After they're downloaded, they must be loaded into an Access database for reporting (I use DoCmd.TransferText, and have a saved text import specification). Every time I run the job that generates the data and downloads into CSV, I usually need to load into a fresh copy of the unpopulated version of the Access database. Is there a way to automate this in a batch script?
In short, I need to be able to:
copy the unpopulated Access file to a new file with the timestamp in the name
load certain CSV files that match a pattern (such as "data_for_reporting_2_20111024_135142.csv") in the directory into the Access file.
I think you can use VBScript to do what you need.
copy the unpopulated Access file to a new file with the timestamp in
the name
FileSystemObject.CopyFile "c:\somefolder\template.mdb", "c:\dest\new.mdb"
See CopyFile Method.
load certain CSV files that match a pattern (such as
"data_for_reporting_2_20111024_135142.csv") in the directory into the
Access file.
You can examine the Files Collection of your CSV folder, determine which of those file names match your target pattern, then run DoCmd.TransferText with each matching file name.
You would run DoCmd.TransferText from an Access application instance:
Option Explicit
Dim appAccess
Dim strMdb
Const cstrFolder = "c:\dest\"
strMdb = "new.mdb"
Set appAccess = CreateObject("Access.Application")
appAccess.OpenCurrentDatabase cstrFolder & strMdb, False
So, do the Transfertext from that instance variable:
appAccess.DoCmd.TransferText [your options]
Edit: This would be faster for me to create and test in VBA. So I think I would use that instead of VBScript.
Create a function, SnarfCSV, in a standard module in your template MDB. Then create a macro, mcrSnarfCSV, with the SnarfCSV function as its runcode action. Then after you copy the template MDB to the new MDB, open the new one with the /x command line switch to run the macro.
"Path to MSACCESS.EXE" "Path to your db file" /x mcrSnarfCSV

SSIS Excel connection error "External table is not in the expected format."

Problem
-I have an excel spreadsheet generated a lotus app. It smells and looks like excel but Excel Source data flow source can not recognise it. When trying to select a table(tab) i get the following error message "External table is not in the expected format.”
-Opening the excel spreadsheet and save it again helps(File also reduces in size). But as dev we ara allegic to manual processes.
-I have tried to change the connection string using a variable
from
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\DataImport\Lotus.xls;Extended Properties="Excel 8.0;HDR=YES";
To
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\DataImport\Lotus.xls;Extended Properties="EXCEL 12.0;HDR=YES";
If i can avoid the script task that would be great
I have had to open and close the excel file manually saving. Those bugs that can't be fixed
I had the same error, your post led me to open the excel file but my excel file was corrupt. I'm posting in case this helps someone else.

Apache POI - Reading .xlsx and .xls files using usermodel

I have code using Apache POI written to read data from .xls worksheets using hssf. I'd like for the program to read .xlsx worksheets as well using org.apache.poi.ss.usermodel. Here is the code:
(_fileName is passed into the function)
java.io.FileInputStream fs = new java.io.FileInputStream(_fileName)
Workbook book = WorkbookFactory.create(fs);
It throws the following exception for a .xlsx file: InavlidFormatException - Can't read the content types part !
I'm doing this in Visual Studio, so the output window says "A first chance exception of type 'org.apache.poi.openxml4j.exceptions.InvalidFormatException' occurred in poi-ooxml-3.7-20101029.dll'.
And for a .xls file, the output window says "A first chance exception of type 'java.io.IOException' occurred in IKVM.OpenJDK.Core.dll"
It would be great if someone could help me solve this issue. Been working on this since yesterday.
Thank you so much!!!
Soundarya
The error message indicates that your .xlsx file isn't a valid one. I'd double check you're passing in the correct file, and that it's really an Excel one (and not something else - in your .xlsx case I suspect you just have a normal zip file)