Open import dial. box to select csv

G

gg

I would like the user to click a button that would open the import dialog box
to CurrentProject.Path with csv already selected as the file type. The user
could then select a particular csv file. (The csv file name will change from
time to time or there could be more than one). This question was asked and
answered a couple of years ago and the MVP directed the asker to
http://www.mvps.org/access/api/api0001.htm. I tried what I thought applied,
but, after many tries, had to give up. Very likely beyond my capability to
extract relevant code. . I use Access2000 with Windows 2000. Any help will
be much appreciated. Thanks in advance
 
P

Piet Linden

I would like the user to click a button that would open the import dialogbox
to CurrentProject.Path with csv already selected as the file type.  Theuser
could then select a particular csv file.  (The csv file name will change from
time to time or there could be more than one).  This question was askedand
answered a couple of years ago and the MVP directed the asker tohttp://www.mvps.org/access/api/api0001.htm. I tried what I thought applied,
but, after many tries, had to give up.  Very likely beyond my capability to
extract relevant code. .  I use  Access2000 with Windows 2000. Any help will
be much appreciated.  Thanks in advance

Function GetOpenFile(Optional varDirectory As Variant, _
Optional varTitleForDialog As Variant) As Variant
' Here's an example that gets an Access database name.
Dim strFilter As String
Dim lngFlags As Long
Dim varFileName As Variant
' Specify that the chosen file must already exist,
' don't change directories when you're done
' Also, don't bother displaying
' the read-only box. It'll only confuse people.
lngFlags = ahtOFN_FILEMUSTEXIST Or _
ahtOFN_HIDEREADONLY Or ahtOFN_NOCHANGEDIR
If IsMissing(varDirectory) Then
varDirectory = ""
End If
If IsMissing(varTitleForDialog) Then
varTitleForDialog = ""
End If

' Define the filter string and allocate space in the "c"
' string Duplicate this line with changes as necessary for
' more file templates.
'strFilter = ahtAddFilterItem(strFilter, _
"Access (*.mdb)", "*.MDB;*.MDA")
strFilter = ahtAddFilterItem(strFilter, "Comma-separated values
(*.csv)", "*.CSV")
' Now actually call to get the file name.
varFileName = ahtCommonFileOpenSave( _
OpenFile:=True, _
InitialDir:=varDirectory, _
Filter:=strFilter, _
Flags:=lngFlags, _
DialogTitle:=varTitleForDialog)
If Not IsNull(varFileName) Then
varFileName = TrimNull(varFileName)
End If
GetOpenFile = varFileName
End Function

Call:
?GetOpenFile(currentproject.Path,"Choose a CSV file...")

or probably...
strFile = GetOpenFile(currentproject.Path,"Choose a CSV file...")
 
D

Douglas J. Steele

What Piet's given you is NOT sufficient. If you look, you'll see it's only
part of the code at http://www.mvps.org/access/api/api0001.htm

Go to the webpage you cited and copy everything in the shaded area (between
Code Starts and Code Ends) and paste it into a new code module (not a class
module nor a module associated with a form or report). Save the module,
making sure you give it a unique name (modules cannot have the same name as
functions or subs contained in the application. For this reason, many people
choose to prefix all their module names with bas or mod, and then not use
that prefix anywhere else).

To present the user with the dialog and capture his/her choice into variable
strInputFile, you'd then use the following code:

Dim strFilter As String
Dim strInputFileName as string

strFilter = ahtAddFilterItem(strFilter, "Comma-Separated Value Files
(*.csv)", "*.csv")
strInputFileName = ahtCommonFileOpenSave( _
Filter:=strFilter, OpenFile:=True, _
DialogTitle:="Please select an input file...", _
Flags:=ahtOFN_HIDEREADONLY)



--
Doug Steele, Microsoft Access MVP

(no private e-mails, please)


I would like the user to click a button that would open the import dialog
box
to CurrentProject.Path with csv already selected as the file type. The
user
could then select a particular csv file. (The csv file name will change
from
time to time or there could be more than one). This question was asked and
answered a couple of years ago and the MVP directed the asker
tohttp://www.mvps.org/access/api/api0001.htm. I tried what I thought
applied,
but, after many tries, had to give up. Very likely beyond my capability to
extract relevant code. . I use Access2000 with Windows 2000. Any help will
be much appreciated. Thanks in advance

Function GetOpenFile(Optional varDirectory As Variant, _
Optional varTitleForDialog As Variant) As Variant
' Here's an example that gets an Access database name.
Dim strFilter As String
Dim lngFlags As Long
Dim varFileName As Variant
' Specify that the chosen file must already exist,
' don't change directories when you're done
' Also, don't bother displaying
' the read-only box. It'll only confuse people.
lngFlags = ahtOFN_FILEMUSTEXIST Or _
ahtOFN_HIDEREADONLY Or ahtOFN_NOCHANGEDIR
If IsMissing(varDirectory) Then
varDirectory = ""
End If
If IsMissing(varTitleForDialog) Then
varTitleForDialog = ""
End If

' Define the filter string and allocate space in the "c"
' string Duplicate this line with changes as necessary for
' more file templates.
'strFilter = ahtAddFilterItem(strFilter, _
"Access (*.mdb)", "*.MDB;*.MDA")
strFilter = ahtAddFilterItem(strFilter, "Comma-separated values
(*.csv)", "*.CSV")
' Now actually call to get the file name.
varFileName = ahtCommonFileOpenSave( _
OpenFile:=True, _
InitialDir:=varDirectory, _
Filter:=strFilter, _
Flags:=lngFlags, _
DialogTitle:=varTitleForDialog)
If Not IsNull(varFileName) Then
varFileName = TrimNull(varFileName)
End If
GetOpenFile = varFileName
End Function

Call:
?GetOpenFile(currentproject.Path,"Choose a CSV file...")

or probably...
strFile = GetOpenFile(currentproject.Path,"Choose a CSV file...")
 
G

gg

Thanks-and sorry so late to reply-out of town. I tried code and works to a
point. I would like it to open the dialog box for csv imports in the folder
located at currentProject.path. As shown, it always goes to the My
Documents directory and i can't figure out how to change it.

An aside, I have been coding, but apparently in a very simplified way-I am
relly confused by the complexity of the referenced code.. Can you recommend
a book or website that takes me past Calahan's Fundamentals?
 
G

gg

Thanks for reply.-I am replying late, sorry, unavoidable. I have tried your
and Mr Steele's answers and have partial victory on both. With his reply, I
get a csv import dialog box, but only in the My documents folder. With
yours, I get to the desired CurrentProject.path folder, but csv files are not
chosen as file type. In order to avoid an error,(trimnull function not
defined) I had to remove the if statement code near the end that starts with
If Not isnull(varfileName). How do I modify to open same import dialog box
with csv file type chosen? thanks in advance
 
D

Douglas J. Steele

To have it start in a different folder, provide a value for the InitialDir
parameter:

strInputFileName = ahtCommonFileOpenSave( _
Filter:=strFilter, OpenFile:=True, _
DialogTitle:="Please select an input file...", _
InitialDir:="C:\SomeFolder\SomeSubfolder", _
Flags:=ahtOFN_HIDEREADONLY)

For lists of some good books on coding, see what Jeff Conrad has at
http://www.accessmvp.com/JConrad/accessjunkie/resources.html#Books
 
G

gg

Thanks so much-works great. GG

Douglas J. Steele said:
To have it start in a different folder, provide a value for the InitialDir
parameter:

strInputFileName = ahtCommonFileOpenSave( _
Filter:=strFilter, OpenFile:=True, _
DialogTitle:="Please select an input file...", _
InitialDir:="C:\SomeFolder\SomeSubfolder", _
Flags:=ahtOFN_HIDEREADONLY)

For lists of some good books on coding, see what Jeff Conrad has at
http://www.accessmvp.com/JConrad/accessjunkie/resources.html#Books
 
G

gg

I spoke a little too soon. I had assumed that what the code came up with was
a standard import dialog box. However, when I select the filename for
import, I don't get the import wizard-everything just disappears into a
void. it may be that this whole procedure is beyond me. If so, tell me and
I will quit pestering you.
 
D

Douglas J. Steele

You don't get a wizard when you're using VBA to import data. What I gave you
earlier is simply how to determine the filename. Once you know that, you
need to use the TransferText method to import the data.
 
G

gg

Thanks again for all the work-GG

Douglas J. Steele said:
You don't get a wizard when you're using VBA to import data. What I gave you
earlier is simply how to determine the filename. Once you know that, you
need to use the TransferText method to import the data.
 
M

MOSTAFA

â€â€ÙƒØªØ¨ "gg said:
Thanks-and sorry so late to reply-out of town. I tried code and works to
a
point. I would like it to open the dialog box for csv imports in the
folder
located at currentProject.path. As shown, it always goes to the My
Documents directory and i can't figure out how to change it.

An aside, I have been coding, but apparently in a very simplified way-I am
relly confused by the complexity of the referenced code.. Can you
recommend
a book or website that takes me past Calahan's Fundamentals?
 

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