I am having some trouble exporting out to a file from VBA. Some things to be aware of are that JobDetail is a valid export SpecName and 2_JobDetail is a query that combines some info with stuff from other fields and tables. When I try to run I get the error like below on my DoCmd.TransferText method call. Advice? am I doing something wrong? code below.
Public Function exportJobDetailRecs(dateStr As String)
'Docmd.TransferText(acexport,specName,TableName, FileName,HasfieldNames,HTMLTableName)
DoCmd.TransferText acExport, _
"JobDetail", _
"2_JobDetail", _
"P:\Folder1\Folder2\Tracker\" + CStr(dateStr + "_OrderStatus_jobdets.txt")
exportJobDetailRecs = CStr(dateStr + "_OrderStatus_jobdets.txt")
End Function
Some questions people have asked:
Yes - Do you have full permission to write to the file (i.e. full permission to the folder as well as the text file if you are not creating it with code)
Yes - Check path (folder name with space/etc) for mistakes.
and I'm copying with Shift-Right click, then copy as Path, and adding a trailing \ manually
Yes - Have you try exporting manually and overwriting your existing spec and see if that works? If so, try runs the code again afterward
Yes - Have you confirmed JobDetail is an export spec instead of an import spec?
and manual exports work fine
3-50 - How many records does 2_JobDetail currently return?
Same - Try the export (temporarily) to "P:\Folder1\Folder2\Tracker\a.txt"
It doesn't care what folder (that exists) I point it to, it wont go
None - Also make sure that there are no punctuation marks in strDate
http://pastie.org/private/0kdf2wvkg1wug5physna for the function I use to make dateStr
I figured this out. It wanted me to create the destination files too, not just the directories before I could copy to it.
I ran into the same problem. Solution: I changed all the column names and it worked. Something about the column names MS Access didn't like. I named them f1, f2, f3...
I just ran into the same problem; I changed the encoding from 'Westeuropean (Windows)' to 'Unicode (UTF-8)' and that did the trick. The problem probably had to do with this invisible 'character' in one of the fieldnames: "observer".
Related
I need to skip the first three lines of a CSV file when loading into MS Access. The default CSV import does not drop the first three lines. I am thinking of writing a macro to ignore the first three lines.
My research has shown the DoCmd object but its methods do not cater to drop the first x lines.
Any thoughts?
The DoCmd.TransferText method takes a SpecificationName as one of its arguments. Follow the instructions at How to Create an Import Specification to create and save an import specification.
Now go into Access Options, Navigation Options, and show hidden/system objects. You should now be able to open a table named 'mSysIMEXSpecs'. Find the import spec you created earlier based on the SpecName column. Change the 'StartRow' column for that import spec to 3 (the StartRow is zero-based).
Now you should be able to use the DoCmd.TransferText method passing the name of the import spec you created and it will skip your first three lines.
In Access VBA I would use two TextStream objects, one to .ReadLine the original CSV file line-by-line and the other to .WriteLine to a temporary file, skipping the first three lines of the input file. Do some web searches on Scripting.FileSystemObject and I'm sure you'll find some sample code for this.
Then I would use DoCmd.TransferText acImportDelim to import the temporary file into Access.
The Docmd.TransferText method is a good option to go with but as an alternative to modifying your import spec, you could consider importing everything and doing some validation after the import.
So you could, import the entire file and then use a delete query to delete data from the table.
As mentioned in a comment above, modifying the spec via a system table could be tricky for someone else to find where-as a delete query with a nice little comment in your code could work a treat. If it's required use it, if not it could be commented out.
So I'm looking at some access code, and trying to figure out what exactly it's doing. Semi-stuck at the following line:
SpecName = "DocLineDetailImportSpec"
DoCmd.TransferText acImportDelim, SpecName, tblname, filenm, True
Ok, let's do some googling. Microsoft tells us the following:
https://msdn.microsoft.com/en-us/vba/access-vba/articles/docmd-transfertext-method-access
Acimportdelin - ok, the default. Sure.
Specname - this is where I'm stuck
Tablename - yup, obviously going to the table named earlier
Filename - yup, obviously grabbing the file we declared earlier in the code
True - Sure, not too concerned about it.
So I'm on Specname - reading the documentation, it's a type of import rule. Ok, where are import rules kept? Apparently, in saved imports (from my googling). I get there and... there are two rules, neither of them have the name, and neither of them appear to have any sort of visibility. I can't see what they're doing (if they were even correct), or anything. How can I figure out (short of brute-force uploading data and tracing it all) what the speccommand is?
Uploading Excel files btw
Thank you
Your question, essentially, boils down to the following: how can I view the import specifications?
Well, in the following way (screenshots: Access 2016)
Step 1: Try to add a new data source -> Text
Step 2: Go to advanced
Step 3: Go to specifications
You can view and adjust import and export specifications here.
Sorry to darken your day with my troubles, but SSIS has broken me! I am new to SSIS and I just seem to be misunderstanding it.
For background: I have a few versions of a basic package that includes a Foreach Loop container and a Data Flow with a few Derived Columns that imports CSV files into a SQL Server Staging table. It is very straightforward and does include an Execute SQL task and a File Move but those work fine. The issues are with the Foreach loop and the Data Flow.
I have one version of this package (let’s call it “A”) that seemed to be working fine. It would process multiple files in a folder, insert records into the staging table, properly execute the SQL Statements, and move the files to Archive. Everything seemed fine until I carefully QA’d the process. Turns out it was duplicating the data from one file, and never importing the data from a second Source File! Yet, the second/dupe round of data included the Source Filename (via a derived column) of the second file (but the data from the first). So it looked like I had successfully processed BOTH files until I looked at the actual data and saw that none of the values from the second source file were ever written to the Staging table.
Once I discovered this, I figured that the problem was in the Foreach loop and how I setup the different file path & name variables. So, I decided to try to make a new version of the package. I started by copying package A and created package B. In B, I deleted the Source Connection manager and created a new Connection Manager along with all new file & path variables. I then tried to cleanup/fix/replace various elements in my Data Flow and Foreach loop. In the process, I discovered that the Advanced Mappings from A – which DID work – were virtually all setup as String (even the Currency and Date columns). That did not seem right, so I modified each source money column by changing to data type Currency, and changed each date-related column to data type Date.
What followed has been dozens and dozens of Errors and I cannot get Package B to run. I have even changed all of the B data types back to String (mirroring the setup in Package A which DID work). But, still no joy.
This leads me to ask a few questions to those of you smarter than I:
1) Why can’t SSIS interpret Source CSV data using the proper data type? I.e. why do I need to set every Input column as a STRING when some columns are clearly & completely Numeric, Currency or Dates? (Yes, the Source CSV files are VERY clean – most don’t even have NULLS)
a. When I do change the Advanced mapping for a date-related Source column to Date, I get the ever present error message: [Flat File Source [30]] Error: Data conversion failed. The data conversion for column "Settle Date" returned status value 2 and status text "The value could not be converted because of a potential loss of data.".
2) When I reset the data types back to String in package B, I still get errors – usually Truncation errors (and Yes – I have adjusted the length to 250 in one of these columns).
a. Error Message: "The value could not be converted because of a potential loss of data.".
b. When I reset the Mappings to ignore the column (as a test), it throws a similar error at the next column.
3) Any ideas why Package A would dupe a file’s data and not process the second file, yet throw no errors and move both to Archive?
4) Why does the Data Viewer appear to have parsing errors (it shows data in the wrong columns) but when you use the Copy data feature in the data viewer and paste it into Excel, all of the data lines up perfectly?
5) Are there any tips & tricks that a rookie SSIS user needs to understand and which might not be apparent through the documentation and searching web articles as well as this site?
I can provide further details if they will help, but these packages are really very simple and should not be causing me this much frustration.
THANKS for any insights.
DGP
Wow seems like you have a lot of ssis issues... I think the reason for the same file being extracted is because of the the way your 'variable mappings' is defined.
Have you had a look and followed this guide:
https://www.simple-talk.com/sql/ssis/ssis-basics-introducing-the-foreach-loop-container/
Hope this helps.
Shaheen
Thanks Tab & Shaheen,
To all SSIS rookies - please learn from my mistakes!
It appears that my issue was actually in how I identified the TEXT QUALIFIER in the Connection Manager. I had entered "" and that was causing problems with how my columns were being parsed. The parsing issues caused unexpected values to appear in some of the columns and that was causing the errors in the package.
When I tried changing the the Text Qualifier to only ONE double quote - " - the whole thing worked!
As I mentioned - and as Shaheen suspected - my initial issues with the duplicate processing was probably due to how I setup the foreach loop. I had already fixed that, bit was still getting errors until I fixed the Text Qualifier.
I have only tested it a few times but it looks like that was the issue.
Thanks for the contributions.
DGP
I am trying to use an automated macro to export a Ms-Access table to a csv file. I want the destination file to have a unique name, and I reckoned that using now()yyyymmddhhnn would be a good way to achieve this.
I have got transfer text working ok from my macro, and I have set up an export file spec for the transfer.
I am using ="C:\batchfile_" & Format(Now(),"yyyymmddhhnn") & ".csv" in the filename argument in the macro. This bit works.
But when I try to run the macro, it tells me that the filename doesn't exist and then the export doesn't complete. I am not sure why this is, but I think it is because the export file specification is expecting the destination file to have the same filename and column structure as the source table.
Does anyone know a way around this?
Eric
This is very old thread, I am posting my solution so that it may be usefull for some one else
transfer text works fine, as long as variables are supplied properly, you can check for other options other than filename, datasource alternatively create using file open statement
by opening text file and convert recordset data into CSV format.
I've had quite a hard time with this--can anyone help?
I need code for a command button that will import a file (the file will be different each time but will always be either txt. or csv), run an import specification that converts all data types to text (the spec is called SpecsTest), then call the table "Scrubbed" (not the original file name). I've also been unable to create a macro that works--the challenge has been that each time this is run the raw file name will be different.
Haven't used Access, but have done this a lot with Excel. Try this:
Dim ds As FileDialog
Set ds = Application.FileDialog(msoFileDialogOpen)
ds.Show
Dim path as String
path = ds.SelectedItems(1)
You'll have to add some check to ensure that a file was selected, etc, but this should get you on the right track.
BTW, you can configure default directories, filters, ... in the FileDialog so you should be able to limit the tendency of your users to screw up the import, but you never can tell.