424,279 Members | 1,905 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,279 IT Pros & Developers. It's quick & easy.

Runtime Error 31519

P: 4
Hello All,

I found some code online (from ADezii -- thanks) to import a .txt file into Access using a Dialog box. When I try and run the code, I am getting the runtime error '31519': You cannot import this file. (**Mod Edit Error occurs on line #22 **)

How can I get rid of this error? Any help is appreciated. Thanks!

The following is what I have:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command43_Click()
  2. Dim fDialog As Office.FileDialog
  3. Dim varFile As Variant
  4.  
  5. DoCmd.SetWarnings False
  6.  
  7. 'Set up the File Dialog
  8. Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
  9. With fDialog
  10.   .AllowMultiSelect = False
  11.   .Title = "Select a Text File to Import"
  12.   .InitialFileName = "C:\*.txt"
  13.  
  14.   'Clear out any Filters, then Add you own (1)
  15.   .Filters.Add "Text Files", "*.TXT"
  16.  
  17.   'Show the Dialog. If the Show Method returns True, the User picked
  18.   'at least 1 File, otherwise the User clicked Cancel
  19.   If .Show Then
  20.     For Each varFile In .SelectedItems
  21.       'There will be only 1 File selected, so place the proper Arguments into the TransferText Method
  22.       DoCmd.TransferText acImportFixed, "ImportB", "B", 0
  23.     Next
  24.   Else      'Nothing selected in File Dialog
  25.   End If
  26. End With
  27.  
  28. DoCmd.SetWarnings True
  29. End Sub
Nov 22 '11 #1
Share this Question
Share on Google+
7 Replies


NeoPa
Expert Mod 15k+
P: 31,186
You provided a FileName parameter of 0. You can't expect it to work with that information ;-)
Nov 22 '11 #2

P: 4
Thank you! I am new at VBA. How do I enter a file name argument that still allows the user to select the text file?
Nov 23 '11 #3

NeoPa
Expert Mod 15k+
P: 31,186
Scruffy, you need to consider your posts more carefully before submitting them. If your question makes sense to you I expect it's because you already know what you mean. Without any context or reference point that last bit makes no sense to me at all. 'Allowing the user to select the text file' is not any part of the question I've seen so far.

Nought for two isn't a good average ;-)
Nov 23 '11 #4

P: 4
My goal is to be able to import files using the Dialog box. The filename parameter is the path to the exact file, right? So what should the filename parameter be so that I can still choose the file I want to import?
Nov 23 '11 #5

NeoPa
Expert Mod 15k+
P: 31,186
So, you're really asking here how to use the return value of the FileDialog object you're using.

Try :
Expand|Select|Wrap|Line Numbers
  1. .SelectedItems(1)
But you probably want to ensure :
Expand|Select|Wrap|Line Numbers
  1. .AllowMultiSelect = False
Alternatively, you can use the code from Select a File or Folder using the FileDialog Object.

If you're still not sure which parameter is which in the DoCmd.TransferText() then it would be the fourth parameter and it's called FileName. Context-Sensitive Help can give you all the details you could want for that though.
Nov 24 '11 #6

P: 4
Thank you so much, I finally have this portion working.
Nov 28 '11 #7

NeoPa
Expert Mod 15k+
P: 31,186
You're welcome :-)
Nov 28 '11 #8

Post your reply

Sign in to post your reply or Sign up for a free account.