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

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Importing Excel records    
 
   
bigbrownbear
post Dec 9 2006, 12:55 PM
Post #1

UtterAccess Enthusiast
Posts: 55



Hi,

I have researched importing from Excel in other posts but I am still having problems I can't solve.

I have this code in the onclick procedure of a command button;



Sub Command0_Click()


Dim strFilter As String
Dim strInputFileName As String

strFilter = ahtAddFilterItem(strFilter, "Excel Files (*.XLS)", "*.XLS")
strInputFileName = ahtCommonFileOpenSave( _
Filter:=strFilter, OpenFile:=True, _
DialogTitle:="Please select an input file...", _
Flags:=ahtOFN_HIDEREADONLY)

Dim YourPath As String

DoCmd.TransferSpreadsheet acImport, , "allaccounts", YourPath, True


End Sub



When I click on the button it opens up the dialog box for me to select the file to import, however when I select the file and click open I get;

runtime error 2522,
The action or method requires a file name argument.

The debug shows this is a problem with my doCmd line. I am trying to import into an existing table named "allaccounts". Any ideas where I am going wrong?
Go to the top of the page
 
+
Peter46
post Dec 9 2006, 12:58 PM
Post #2

UtterAccess VIP
Posts: 7,394
From: Oadby Leics, UK



Your Docmd.Transfspreadsheet command doesn't specify which file you are importing from.
Go to the top of the page
 
+
KenSnell
post Dec 9 2006, 01:01 PM
Post #3

UtterAccess VIP
Posts: 1,117
From: Michigan, USA



Your code never sets the value of YourPath variable. It does set the value of strInputFileName (I believe your code also will open the selected file, which I don't believe you want to do -- see the OpenFile argument in the ahtCommonFileOpenSave function call), so either use that variable in the TransferSpreadsheet call, or else set its value to YourPath variable.
Go to the top of the page
 
+
bigbrownbear
post Dec 9 2006, 01:14 PM
Post #4

UtterAccess Enthusiast
Posts: 55



I now remember why I have given up learning VBA so many times! This is hard work!

My code is now;

Sub Command0_Click()


Dim strFilter As String
Dim strInputFileName As String

strFilter = ahtAddFilterItem(strFilter,
strInputFileName = ahtCommonFileOpenSave( _
Filter:=strFilter, OpenFile:=True, _
DialogTitle:="Please select an input file...", _
Flags:=ahtOFN_HIDEREADONLY)

Dim YourPath As String
YourPath = strFilter

DoCmd.TransferSpreadsheet acImport, , "allaccounts", YourPath, True



Does this make sense?
When I select the file now I get the error;

Run Time error 3011
The Microsoft Jet Database Engine could not find the object 'D:\PersonalData\ExcelFiles (*.XLS)'
Make sure the object exists and that you spell it's name and path correctly

I figure this is referring to this part of code,

"Excel Files (*.XLS)", "*.XLS")

and that this code is making it look for files named "ExcelFiles *.XLS" when actually I am just asking it to display these file types. But i'm not sure how to change this part of the code. I don't mind if it shows all file types.
Go to the top of the page
 
+
KenSnell
post Dec 9 2006, 01:19 PM
Post #5

UtterAccess VIP
Posts: 1,117
From: Michigan, USA



Change this line of code:

YourPath = strFilter


to this:

YourPath = strInputFileName
Go to the top of the page
 
+
norie
post Dec 9 2006, 01:20 PM
Post #6

UtterAccess VIP
Posts: 4,295



Is that all the code?
Go to the top of the page
 
+
bigbrownbear
post Dec 9 2006, 01:25 PM
Post #7

UtterAccess Enthusiast
Posts: 55



Thanks so much.

Just so I know for the future, when I am defining YourPath as a variable why would I refer to strInputFileName and not strFilter?

And as strInputFileName was already defined as a variable could I just not have referred to this in the DoCmd as below?

DoCmd.TransferSpreadsheet acImport, , "allaccounts", strInputFileName, True

I am presuming that by defining YourPath as a variable all I am doing is basically making
YourPath = strInputFileName

These probably seem stupid questions but I am desperate to understand this better!
Go to the top of the page
 
+
norie
post Dec 9 2006, 03:30 PM
Post #8

UtterAccess VIP
Posts: 4,295



If you declare something as a variable it's empty/0 until you set it a value.

As Ken pointed out you didn't set a value for YourPath in your code.

As to why you should use strInputFileName instead of strFilter that's because this piece of code
gets a filename and puts it in strInputFileName.
CODE
strInputFileName = ahtCommonFileOpenSave( _
Filter:=strFilter, OpenFile:=True, _
DialogTitle:="Please select an input file...", _
Flags:=ahtOFN_HIDEREADONLY)

What you probably should do is replace YourPath here with strInputFileName.
CODE
DoCmd.TransferSpreadsheet acImport, , "allaccounts", YourPath, True
Go to the top of the page
 
+
KenSnell
post Dec 9 2006, 03:35 PM
Post #9

UtterAccess VIP
Posts: 1,117
From: Michigan, USA



QUOTE
And as strInputFileName was already defined as a variable could I just not have referred to this in the DoCmd as below?

DoCmd.TransferSpreadsheet acImport, , "allaccounts", strInputFileName, True


Yes, you can use strInputFileName in the TransferSpreadsheet code line.
Go to the top of the page
 
+
bigbrownbear
post Dec 9 2006, 04:12 PM
Post #10

UtterAccess Enthusiast
Posts: 55



Thanks. That makes it clearer for next time!
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: 20th May 2013 - 11:21 AM

Tag cloud: