Access Query - Union of TEXT files that are selected.

S

Sinner

Hi,

I was wondering if this can be done.

I will place the link name of delimited text file in a table1 in filed
name like "c:\abc\test1.txt", "c:\abc\test2.txt", "c:\abc\test3.txt"
and so on.
The delimiting character is "|".
The next column is Select yes/no in which I have option to tick the
file that I want to select.

Now I want a query to include the file/files that I select in table1.
It can be single file or multiple file.

Since the text files change frequently, I wish not to import them or
link them.
Instead I want to have a selection query of my choice.

I hope I have explained it neatly.

Looking forward for a solution.
 
L

Larry Linson

Access SELECT Queries only operate on Access Tables or other Access Queries.

You can write VBA code to read through and search contents of text files,
but not a SELECT query unless the Text file can be treated as a Table by
linking it (which some, but not all, text files can).

If you would clarify your question, perhaps someone will be able to offer a
useful suggestion. Since you did not say what you wanted to do with any
data that you found, or whether you wanted to search only in a particular
position or in every comma-delimited field for the search value, etc., it's
difficult to do more than answer your initial question -- that is, with code
it is very likely, but not assuredly, possible.

Larry Linson
Microsoft Office Access MVP
 
A

Angela

Access SELECT Queries only operate on Access Tables or other Access Queries.

You can write VBA code to read through and search contents of text files,
but not a SELECT query unless the Text file can be treated as a Table by
linking it (which some, but not all, text files can).

If you would clarify your question, perhaps someone will be able to offera
useful suggestion.  Since you did not say what you wanted to do with any
data that you found, or whether you wanted to search only in a particular
position or in every comma-delimited field for the search value, etc., it's
difficult to do more than answer your initial question -- that is, with code
it is very likely, but not assuredly, possible.

 Larry Linson
 Microsoft Office Access MVP












- Show quoted text -

Hi,

The files are "|" delimited text files.

I will put the path & text file name in a table like "c:\test
\test1.txt" and next to it a column with check box.
If I check it, I want to return a query result for that. In case no
file is selected, it should return blank query.
For multiple text files that I select, I want to return union of those
selected text files.

Hope that clears what is required.

Thanks
 
L

Larry Linson

Hi,
The files are "|" delimited text files.
I will put the path & text file name in a table
like "c:\test\test1.txt" and next to it a column
with check box. If I check it, I want to return
a query result for that. In case no file is selected,
it should return blank query. For multiple text
files that I select, I want to return union of
those selected text files.
Hope that clears what is required.


Please explain what you mean by "return a query result for that".

To where, to whom, or to what do you want to return "a union of those text
files."

You said you did not want to import or link the text files, but the _only
way_ you can query a text file is if you have linked it as a table or
imported the data into a table.

You essentially re-stated what you'd said initially, but, yes, a bit more
clearly. Unfortunately, I don't understand what you mean -- so I can'f offer
an answer or a suggestion.

Larry Linson
Microsoft Office Access MVP
 
J

John W. Vinson

I need to return union of selected *.txt files as a union query.

You could construct a SQL string in VBA code using the IN(<filename>)
operator. See the Help topic for "FROM", and look at the specific help for IN
under that topic.

I haven't tested it, and this may not work, because there is a warning that
the IN clause can refer to only one external database. Is this the problem
that you're raising (without actually stating what the problem is)???
 
J

James A. Fortune

Sinner said:
Hi,

I was wondering if this can be done.

I will place the link name of delimited text file in a table1 in filed
name like "c:\abc\test1.txt", "c:\abc\test2.txt", "c:\abc\test3.txt"
and so on.
The delimiting character is "|".
The next column is Select yes/no in which I have option to tick the
file that I want to select.

Now I want a query to include the file/files that I select in table1.
It can be single file or multiple file.

Since the text files change frequently, I wish not to import them or
link them.
Instead I want to have a selection query of my choice.

I hope I have explained it neatly.

Looking forward for a solution.

That sounds like a comprehensive end-to-end task that might be more
suitable for a final exam in an Access course. Without getting bogged
down in details, here's one possible (air) plan.

Do it using VBA code with some SQL thrown in.

Perhaps have a listbox to select the names of the text files. You can
use the Dir() function to get all the files from a directory or use a
file picker to add files one by one from different directories to a
table used to populate the listbox. Extra credit is available for
having an option to pick the directory.

For each checked table name in the listbox, use VBA code to ensure that
a link to the table exists. Perhaps include extra information in the
connection string, such as an IMEX value, so that the information gets
read correctly. It might be wise to change characters in the path, such
as : or \, to something friendlier for an Access table name when
assigning a name to the linked file in VBA code.

I assume that the delimited text files have the field names on the first
line so that you don't end up with something like Field1, Field2, etc.
Next you have to get a list of all the distinct field names from all the
linked tables chosen in the listbox. Use the TableDef for each file
name chosen in the checkbox (using the same character substitutions) to
get the names of all the fields in all the tables. Come up with a list
of distinct field names. That can be done by running a SELECT DISTINCT
query on a table containing all the field names you gathered or by
adding only field names that aren't in a string array to that string array.

Make sure that the number of distinct field names is <= 255. Create an
output table with those field names, checking to make sure that they are
all valid field names. That table can be created entirely in VBA or by
creating a DDL query and executing it. Be sure to run a DDL query to
DROP the table if it already exists. Note that the table must be
created each time instead of being emptied and refilled because, in
general, it is not known in advance which fields are being used. Maybe
make all the fields text fields.

Then loop through all the checked file names again to get the data in
each table. Perhaps create dynamic append queries that use the input
table name, the output table name and the field names in the current
table to create the SQL string. It might be wise to surround each field
name with square brackets when forming the SQL string to give the query
a better chance of running if the field names can contain spaces,
reserved words or special characters. Then you can run a .Execute on a
DAO.Database object to run each append query, making sure that the
append has completed before moving on to the next table in the list.
That technique assumes that if any two tables contain the same field
name, then that information goes under the same field name in the output
table.

Note: I haven't tried linking to a delimited text file lately, but doing
so manually might produce a connection string that gives you a hint
about what connection strings to create in code. If you need any
details, ask, and perhaps someone here can supply more information.

James A. Fortune
(e-mail address removed)
 
L

Larry Linson

Angela said:
No luck with this post : (

It must be that our psychic energy is low, so we are having trouble
mindlinking to what you might want.

It couldn't possibly be that you aren't answering our questions about what
you really want, because you are asking for something that is not possible,
and we are desperately trying to figure out what it is you really want/need
to accomplish, could it?

Larry Linson
Microsoft Office Access MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top