I am on a restricted corporate network and I have some trouble programmatically exporting a CSV file from a query. The following command:
DoCmd.TransferText acExportDelim, , "query_name", strPath
tries to export a CSV with a comma [ , ] as delimeter. This results in an error since it equals the decimal separator in my region. Somehow the windows localization settings are not applied correctly in this case. The Excel "save as..." command does give a proper CSV file with [ ; ] delimiter.
Following from this post, I tried to create a specification (SpecName) to apply the semicolon. The dialog box prevented me from unchecking all boxes and saving a specification was not an option. My guess is that this is due to restrictions on my account.
So, the local settings are not applied properly and a custom specification is out of the question. What are my options here?
Two approaches are possible here:
Roll your own csv-Export. It's not too difficult. Open your Query to obtain a Recordset, then iterate over rows with Do While Not rs.EOF and over Fields with For Each myField In rs.Fields and concatenate the data into a String. With this, you can specify your own separators and quotes around the values, if you have the separator inside the values.
Create an Excel-Instance and copy the recordset into a sheet using Worksheet.QueryTables.Add(). Then you can use Worksheet.SaveAs sCSVName, xlCSVWindows to write that to a csv-file.
Related
I have a query that pulls information I need to use in a mail merge document to email to people for verification of information. There are 8 fields they need to verify, preferably with a check box control, but some of the fields contain no information. I would like to make the check box next to merge fields that contain no data (or whatever I may need to write into the query to make this work) invisible. If this could be accomplished easier in a completely different way, that would be fine too. Thank you.
As Cindy said, this kind of thing is handled via field coding in the mailmerge main document, not via VB code. Such a field might be coded as:
{IF{MERGEFIELD myCheck}<> "" "[ ]"}
or:
{IF«myCheck»<> "" "[ ]"}
where 'myCheck' is the field name and '[ ]' is the checkbox content control.
Note: The field brace pairs (i.e. '{ }') for the above examples are all created in the document itself, via Ctrl-F9 (Cmd-F9 on a Mac or, if you’re using a laptop, you might need to use Ctrl-Fn-F9); you can't simply type them or copy & paste them from this message. Nor is it practical to add them via any of the standard Word dialogues. Likewise, the chevrons (i.e. '« »') are part of the actual mergefields - which you can insert from the 'Insert Merge Field' dropdown (i.e. you can't type or copy & paste them from this message, either). The spaces represented in the field constructions are all required.
I'm trying to export a crosstab query from Access 2010 to a csv without a text qualifier. I'm able to use the Transfer Text method with my other exports; the trick with this one is that the number of fields (and their names) change depending on what the user selects on the form, but the setup in the SpecificationName is static. If I don't indicate a SpecificationName, I can get whatever fields are run in the query as appropriate, but I get quotes around my text fields, which I don't want. If I set up a specification to set the text qualifier as None, I'm stuck with a set list of fields. I tried sticking an asterisk in the FieldName area when setting up the specification, but got
The number of fields in your export specification does not match the number in the table you have chosen to export.
I'd really like some kind of "SELECT *" in there, but it doesn't look like it's possible?
Poking around, it looks like I might need to set up a schema.ini? Of course, this would need to be dynamic as well. I found a resource that was written for Access 97: https://support2.microsoft.com/default.aspx?scid=kb;en-us;155512, but it doesn't seem to work in Access 2010. Even after updating db As Database to db As DAO.Database, I get
Expected variable or procedure, not module
When running the following in the Immediate window (with appropriate text subbed in):
?CreateSchemaFile(True,"C:\MyFilepath","ExportFileName.txt","qryCrosstabs")
I've reached the limits of my (limited) VBA chops to figure this out. Writing a script for a dynamic schema.ini seems like overkill for just wanting to get rid of quotes, but if that's what has to happen, any pointers in the right direction would be excellent.
Thanks!
Ah, ok, I figured it out. I thought I had to specify the columns in Schema.ini, but I was mistaken. I just needed to set the header to true; that way it'll read whatever happens to end up in the query. Setting the text delimiter to none was also a key piece.
Here's all I needed in Schema.ini (just needs to be in the same directory as the exported file):
[ExportFileName.txt]
Format=CSVDelimited
ColNameHeader=True
TextDelimiter="none"
And the code:
Private Sub cmdExport_Click()
Dim dbs As DAO.Database
Set dbs = CurrentDb
dbs.Execute "SELECT * INTO [text;database=C:\filepath].[ExportFileName.txt] FROM qryCrosstabs"
While this ultimately works, having an external Schema.ini file is clunkier than I'd like. I've seen a few places where adding extended properties to the dbs.Execute line will indicate csv (FMT=Delimited) and column headers (HDR=Yes). Having one for text delimiters would be the most parsimonious solution, but from what I can tell, that doesn't exist. I'm happy to be corrected on that if it does, though!
When reading a csv file containing ID numbers, excel is reading strings as numbers. This also occurs when reading the same style of ID's in an excel vba array.
Under locals, the elements of the array are displayed as datatype "String", but the format is still a number.
I have tried changing the style to text as well as using CStr() on individual elements of an array. Is there a way to have excel read the ID's as a string instead of a number?
Thanks.
You need to bypass the automatic conversion when you open the .csv file.
Use the Import Wizard to open the file and tell the Wizard that the field is text.
To convert back this might suit:
=SUBSTITUTE(LEFT(A1,3),".","")&"E"&TEXT(RIGHT(A1,3)-1,"0000")
I am working on developing an expert system using CLIPS. For the case at hand I need to read data from a excel file. How do I do that? Or what solutions do you propose? Thank you in advance.
You can use the open command to open a file for reading in either text or binary mode. If you opened a xlsx file in binary mode, you could use the get-char function to retrieve individual characters from the file. There's no built-in functionality for parsing a xlsx file, so you'd have to add code to do the parsing and create appropriate CLIPS values from the data. If possible, it would be easier to save your excel file as tab-delimited text. If each cell is a valid CLIPS token, then you can use the read function to retrieve the cell values. If each cell is not a valid CLIPS token (for example, a cell representing a string that has spaces but lacks quotation marks at the beginning and end), then you need to use the readline function to grab an entire row of data and then use some of the string functions to locate the tabs and split the string into valid tokens.
I want to export the access query into csv file. For that I am using the following format which was given in the previous discussion in stack overflow.
Function ExportQuery()
DoCmd.TransferText acExportDelim, , "[your query]", "[output file].csv"
End Function
I get the output as CSV file but the data is separated in the columns of the sheet. I want the data separated by the semi-colon in the first column itself of all the columns.
Please revert back asap.
I got the answer for my question.
If you want to put any delimiter like comma, semicolon,etc..
Double click on the Query or table whichever you want to export. Then click on Export in the File Tab. Give the file name and select text files (Access 2003). Then click on Export All. Export Text Wizard will open. Select radio Button delimited and click on Advanced. Then select Field Delimiter of what you want. And select Text Qualifier as none. Then select "Save As"
and give the name to the Specification. And complete the whole process as you do in the manual process. After saving it close both the files CSV and access.
The VBA Code will be as given below after saving the specification name:
Function ExportQuery()
DoCmd.TransferText acExportDelim,"Specification Name" , "[your query]", "[output file].csv"
End Function
So only Add "Specification Name" in the code.
Exporting Access database to csv files can easily be done using the Java code here:
https://github.com/NACHC-CAD/access-to-csv-tool
This code includes complete examples in the test code and does a complete export of the nortwinds data base.
See the class AccessToCsvUtil for the code to convert a single table to a .csv file.
See the class WriteToCsvIntegration test to see code that exports all of the tables in the database.
This utility is based on the ucanaccess jdbc tool and the Apache commons-csv tool.
<dependency>
<groupId>net.sf.ucanaccess</groupId>
<artifactId>ucanaccess</artifactId>
<version>4.0.4</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-csv</artifactId>
<version>1.8</version>
</dependency>