Import Text file via browse box - import specs?

D

Diane, St. Louis

I know how to import a text file using a browse box, but I want to be able to
import my text file without having to manually select with import
specification I want to use. I want it to import my file smoothly and
automatically. Is that possible? Does someone have an example code?

This is the one I am currently using:

Private Sub Command13_Click()
On Error GoTo ErrHandler
DoCmd.RunCommand acCmdImport
ExitHandle:
Exit Sub
ErrHandler:
Select Case Err.Number
Case 2501
'Do nothing as cancel selected by user
Resume ExitHandle
Case Else
MsgBox Err.Number & vbCrLf & Err.Description
Resume ExitHandle
End Select

DoCmd.RunMacro "mcrQuantumImportFileExport"
End Sub

Thanks!
Diane
 
G

Graham Mandeno

Hi Diane

First you need some code to browse for and select your text file. You will
find the code you need here:
http://www.mvps.org/access/api/api0001.htm

Next, having the full path to your text file in a variable (say
strTextFile), you need to use the TransferText method - for example:
DoCmd.TransferText acLinkDelim, "Import Spec", "MyTable", strTextFile,
True

You can check out more about TransferText in the online help.
 
D

Diane, St. Louis

Thanks, Graham - I have seen that code & tried using it, but it always errors
out. Am I to insert the entire thing from code start to code end? Are there
names in there that I need to change according to my database & files? Sorry
for being so dense, but I just can't get this to work right.

Thanks for your response!
Diane


Graham Mandeno said:
Hi Diane

First you need some code to browse for and select your text file. You will
find the code you need here:
http://www.mvps.org/access/api/api0001.htm

Next, having the full path to your text file in a variable (say
strTextFile), you need to use the TransferText method - for example:
DoCmd.TransferText acLinkDelim, "Import Spec", "MyTable", strTextFile,
True

You can check out more about TransferText in the online help.
--
Good Luck :)

Graham Mandeno [Access MVP]
Auckland, New Zealand

Diane said:
I know how to import a text file using a browse box, but I want to be able
to
import my text file without having to manually select with import
specification I want to use. I want it to import my file smoothly and
automatically. Is that possible? Does someone have an example code?

This is the one I am currently using:

Private Sub Command13_Click()
On Error GoTo ErrHandler
DoCmd.RunCommand acCmdImport
ExitHandle:
Exit Sub
ErrHandler:
Select Case Err.Number
Case 2501
'Do nothing as cancel selected by user
Resume ExitHandle
Case Else
MsgBox Err.Number & vbCrLf & Err.Description
Resume ExitHandle
End Select

DoCmd.RunMacro "mcrQuantumImportFileExport"
End Sub

Thanks!
Diane
 
G

Graham Mandeno

Hi Diane

Do you mean it's the file browse dialog code that you can't get working?

Just copy everything between *** Code Start*** and *** Code End ***.

Create a new module, type Option Explicit at the top (if it's not already
there) and paste the code you have copied.

Now, at the place where you want to call the file browse, insert some code
like this:

Dim strFilter As String
Dim strTextFile as string

strFilter = ahtAddFilterItem(strFilter, "Text Files", "*.txt;*.csv")
strFilter = ahtAddFilterItem(strFilter, "All Files", "*.*")
strTextFile = ahtCommonFileOpenSave( _
Filter:=strFilter, OpenFile:=True, _
DialogTitle:="Please select an input file...", _
Flags:=ahtOFN_HIDEREADONLY)
If Len(strTextFile) <> 0 Then
DoCmd.TransferText ...
End If

--
Good Luck :)

Graham Mandeno [Access MVP]
Auckland, New Zealand

Diane said:
Thanks, Graham - I have seen that code & tried using it, but it always
errors
out. Am I to insert the entire thing from code start to code end? Are
there
names in there that I need to change according to my database & files?
Sorry
for being so dense, but I just can't get this to work right.

Thanks for your response!
Diane


Graham Mandeno said:
Hi Diane

First you need some code to browse for and select your text file. You
will
find the code you need here:
http://www.mvps.org/access/api/api0001.htm

Next, having the full path to your text file in a variable (say
strTextFile), you need to use the TransferText method - for example:
DoCmd.TransferText acLinkDelim, "Import Spec", "MyTable", strTextFile,
True

You can check out more about TransferText in the online help.
--
Good Luck :)

Graham Mandeno [Access MVP]
Auckland, New Zealand

message
I know how to import a text file using a browse box, but I want to be
able
to
import my text file without having to manually select with import
specification I want to use. I want it to import my file smoothly and
automatically. Is that possible? Does someone have an example code?

This is the one I am currently using:

Private Sub Command13_Click()
On Error GoTo ErrHandler
DoCmd.RunCommand acCmdImport
ExitHandle:
Exit Sub
ErrHandler:
Select Case Err.Number
Case 2501
'Do nothing as cancel selected by user
Resume ExitHandle
Case Else
MsgBox Err.Number & vbCrLf & Err.Description
Resume ExitHandle
End Select

DoCmd.RunMacro "mcrQuantumImportFileExport"
End Sub

Thanks!
Diane
 

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