UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Pass File Name From Function To Procedure    
 
   
goss
post May 14 2012, 02:37 PM
Post #1

UtterAccess Enthusiast
Posts: 63



Hi all,

Using Access 2007.

I would like to pass the name of the file the user chose at run-time to a procedure to import the file.

My msoFileDialogFilePicker functions works fine.
But when at the DoCmd.TransferText process I receive this error
QUOTE
run-time error '2522':
The action or method requires a File Name argument


What should I be using here
CODE
FileName:=GetFileName, _

If not the name of the function?

Full code below\
thx
w

CODE
Option Compare Database
Option Explicit

Sub Validate()

    'Process:
    '1. Get OpenFileName <- Use late binding MSO 12.0 Object Library
    '2. Import the user selected file


    'Reources
    
    'Date       Developer       Action                      Comments
    '---------------------------------------------------------------------------------
    '05/07/12   ws              Created
    
    'Initialize
     DoCmd.SetWarnings False
     Dim xAeorPLCntry As AccessObject
    
    
    'Get file name
     Call GetFileName
    
    'Import the selected file
     DoCmd.TransferText Transfertype:=acImportDelim, _
     SpecificationName:="specImportCustomer", _
     TableName:="xCustomer", _
     FileName:=GetFileName, _
     HasFieldNames:=False


    'Tidy up
     DoCmd.SetWarnings True
    


End Sub


Public Function GetFileName() As String
    Dim fDialog As Object
    Const msoFileDialogFilePicker As Integer = 1
    Dim strUserFile As String

    Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
    Dim varFile As Variant

    With fDialog
        .AllowMultiSelect = False
        .Title = "Select file to import"
        .InitialFileName = "C:\tmp\"
        .Show
    End With
    
    GetFileName = varFile
    strUserFile = varFile
End Function
Go to the top of the page
 
+
theDBguy
post May 14 2012, 02:40 PM
Post #2

Access Wiki and Forums Moderator
Posts: 48,114
From: SoCal, USA



Hi,

You should be able to use:

FileName:= GetFileName()

But, you might consider checking first if the user did in fact select a file before using the TransferText method.

Just my 2 cents... (IMG:style_emoticons/default/2cents.gif)
Go to the top of the page
 
+
goss
post May 15 2012, 12:15 PM
Post #3

UtterAccess Enthusiast
Posts: 63



Hi DBguy,

I modifed my code to call the function at the import process as you suggested
CODE
'Import the selected file
     DoCmd.TransferText Transfertype:=acImportDelim, _
     SpecificationName:="specImportCustomer", _
     TableName:="xCustomer", _
     FileName:=GetFileName(), _ '<-Changed    
     HasFieldNames:=False


Now I am receiving this error
QUOTE
Run-time error '2522':
The action or method requires a File Name argument


thx
w
Go to the top of the page
 
+
theDBguy
post May 15 2012, 12:24 PM
Post #4

Access Wiki and Forums Moderator
Posts: 48,114
From: SoCal, USA



I see... I guess it wouldn't work that way. Try it this way instead:

DoCmd.TransferText acImportDelim, "specImportCustomer", "xCustomer", GetFileName(), False

But again, just want to reemphasize that doing it this way doesn't prevent the user from causing an error if they don't actually pick a file.

Just my 2 cents... (IMG:style_emoticons/default/2cents.gif)
Go to the top of the page
 
+
goss
post May 15 2012, 12:47 PM
Post #5

UtterAccess Enthusiast
Posts: 63



Thanks,

I copied your code and pasted.
I commented my code out
Ran it - same error message.
Any other ideas?

thx
w
Go to the top of the page
 
+
theDBguy
post May 15 2012, 12:51 PM
Post #6

Access Wiki and Forums Moderator
Posts: 48,114
From: SoCal, USA



QUOTE (goss @ May 15 2012, 10:47 AM) *
Any other ideas?

Well, actually, the only idea I have was what I have been trying to say earlier. That is, I wouldn't recommend you trying to use the function inside the TransferText method because you cannot prevent the user from producing an error if they don't actually pick a file. So, what I recommend is that you do something like this:

strFileName = GetFileName()
If strFileName > "" Then
DoCmd.TransferText acImportDelim, "specImportCustomer", "xCustomer", strFileName, False
End If

Just my 2 cents... (IMG:style_emoticons/default/2cents.gif)
Go to the top of the page
 
+
goss
post May 15 2012, 01:11 PM
Post #7

UtterAccess Enthusiast
Posts: 63



Perfect! (IMG:style_emoticons/default/notworthy.gif)

thx
w
Go to the top of the page
 
+
theDBguy
post May 15 2012, 01:15 PM
Post #8

Access Wiki and Forums Moderator
Posts: 48,114
From: SoCal, USA



Hi,

(IMG:style_emoticons/default/yw.gif)

Glad we could help. Good luck with your project.
Go to the top of the page
 
+

Thank you for your support! Reply to this topicStart new topic

Jump To Forum:
 



RSS Go to Top  ·  Lo-Fi Version Time is now: 26th May 2013 - 02:32 AM