Q. Error when trying to do Dir, as stated from Access Help File.

J

Jim

Hi,

Having read the many posts here about how to open and manipulate Text
Files from within a Microsoft Access 2000 mdb file, I tried one from
the Access Help file, regarding "Dir" to get started, and as I
expected, I got an error, right off the bat.

I get this error, when I click on the single form, in hopes of getting
some kind of directory listing:

"Run-time Error "5" "
Invalid Procedure Call or Argument

Then, if I click "End" the message goes away, and nothing else
happens.

If I click on "Debug", it brings up the code window, and highlights
the "MyFile = Dir" statement (without any of the 'arguments'), and
that's almost as helpful as when I clicked "End", the first time.

I am clueless, Please help. Thanks, Jim

Here is the code, I placed in my test datbase's "Load" event, in the
only form (the code is straight from my Access 2000 help file example)
on the "Dir" topic:



Private Sub Form_Load()
Dim MyFile, MyPath, MyName

' Returns "WIN.INI" (on Microsoft Windows) if it exists.
MyFile = Dir("C:\WINDOWS\WIN.INI")

' Returns filename with specified extension. If more than one *.ini
' file exists, the first file found is returned.
MyFile = Dir("C:\WINDOWS\*.INI")

' Call Dir again without arguments to return the next *.INI file in
the
' same directory.
MyFile = Dir

' Return first *.TXT file with a set hidden attribute.
MyFile = Dir("*.TXT", vbHidden)

' Display the names in C:\ that represent directories.
MyPath = "c:\" ' Set the path.
MyName = Dir(MyPath, vbDirectory) ' Retrieve the first entry.
Do While MyName <> "" ' Start the loop.
' Ignore the current directory and the encompassing directory.
If MyName <> "." And MyName <> ".." Then
' Use bitwise comparison to make sure MyName is a directory.
If (GetAttr(MyPath & MyName) And vbDirectory) = vbDirectory
Then
Debug.Print MyName ' Display entry only if it
End If ' it represents a directory.
End If
MyName = Dir ' Get next entry.
Loop


End Sub
 
B

Brendan Reynolds

I believe you need to pass an empty string as the first argument ...

MyFile = Dir("")

--
Brendan Reynolds (MVP)
http://brenreyn.blogspot.com

The spammers and script-kiddies have succeeded in making it impossible for
me to use a real e-mail address in public newsgroups. E-mail replies to
this post will be deleted without being read. Any e-mail claiming to be
from brenreyn at indigo dot ie that is not digitally signed by me with a
GlobalSign digital certificate is a forgery and should be deleted without
being read. Follow-up questions should in general be posted to the
newsgroup, but if you have a good reason to send me e-mail, you'll find
a useable e-mail address at the URL above.
 
J

Jim

I believe you need to pass an empty string as the first argument ...

MyFile = Dir("")


Brendan,

That certainly took away the error message.

But I thought it would give me my directory listing, which I still
dont get.

Jim
 
D

Dirk Goldgar

Jim said:
Brendan,

That certainly took away the error message.

But I thought it would give me my directory listing, which I still
dont get.

Jim

Try

MyFile = Dir()

and see if it makes a difference.
 
G

George

Try

MyFile = Dir()

and see if it makes a difference.

No, it just gives me the same exact error as before.
Thanks though.

I'm going to look for a shareware solution because the solution I need
to the problem I've presented here is moronically simple, compared to
what I am looking to do, down the road.

In reality, I should be looking for a program that I can stick in any
folder and it will seek out all .txt files. Then, given my form
input, will be able to copy, delete, edit or rename the .txt files, as
I command it to.

But having this much trouble with this 'baby step', just tells me it's
far beyond the scope of this newsgroup (and I know for a fact, it's
really not!).

Thanks for the effort, in trying to help me,

Jim
 
A

Alphonse Giambrone

Check out FileSearch in Help.
It might give you what you are looking for.
 
D

Dirk Goldgar

George said:
No, it just gives me the same exact error as before.
Thanks though.

I'm going to look for a shareware solution because the solution I need
to the problem I've presented here is moronically simple, compared to
what I am looking to do, down the road.

In reality, I should be looking for a program that I can stick in any
folder and it will seek out all .txt files. Then, given my form
input, will be able to copy, delete, edit or rename the .txt files, as
I command it to.

But having this much trouble with this 'baby step', just tells me it's
far beyond the scope of this newsgroup (and I know for a fact, it's
really not!).

Thanks for the effort, in trying to help me,

Jim

Oh, don't give up so easily. I believe I know what's going on. I
believe that your problem is really that the directory "C:\WINDOWS\"
that the sample code is trying to read doesn't exist. On my Windows
2000 system it doesn't -- the corresponding directory is "C:\WINNT\".
So what this amounts to is, you try to read a directory that doesn't
exist, so no files are returned. Then you try to read "the next file in
that directory". As the help entry for the Dir function says: "When no
more file names match, Dir returns a zero-length string (""). Once a
zero-length string is returned, you must specify pathname in subsequent
calls or an error occurs."

I find that if I run your code unchanged, I get the same error you do.
But if I change the directory to "C:\WINNT\", it works fine. You've
stumbled over a very minor bump in the road, that's all. Any real code
would test MyFile to see if any files were found on the first try before
running on to get more.
 
G

George

Oh, don't give up so easily. I believe I know what's going on. I
believe that your problem is really that the directory "C:\WINDOWS\"
that the sample code is trying to read doesn't exist. On my Windows
2000 system it doesn't -- the corresponding directory is "C:\WINNT\".
So what this amounts to is, you try to read a directory that doesn't
exist, so no files are returned. Then you try to read "the next file in
that directory". As the help entry for the Dir function says: "When no
more file names match, Dir returns a zero-length string (""). Once a
zero-length string is returned, you must specify pathname in subsequent
calls or an error occurs."

OK, I made sure to replace C:\ with my actual data drive.
But, now, even tho I get no errors, I don't get a directory listing of
what's on that drive.

I have a feeling if I keep coming here, asking for help on fixing this
'problem', which I haven't a clue how to start, that my threads will
rightfully start getting ignored.

So, given that you do have an idea of how I'm trying to get my little
project going, can you point me to a tutorial, or sample database that
does pretty much what I've described?

Thanks for your time, george
 
D

Dirk Goldgar

George said:
OK, I made sure to replace C:\ with my actual data drive.
But, now, even tho I get no errors, I don't get a directory listing of
what's on that drive.

If you post your actual code, I expect we can figure out why not.
I have a feeling if I keep coming here, asking for help on fixing this
'problem', which I haven't a clue how to start, that my threads will
rightfully start getting ignored.

No, that hasn't been my experience. As long as you're putting in your
own effort and not expecting people to do all the work for you, you'll
find your questions are welcome.
So, given that you do have an idea of how I'm trying to get my little
project going, can you point me to a tutorial, or sample database that
does pretty much what I've described?

I think there's probably a sample showing how to fill a list box with a
directory listing in the _Access <version> Developer's Handbook_, by
Litwin Getz, et al., from Sybex, but I don't remember for sure. That
book is a terrific resource for developers, but I'm not sure whether it
may be a bit too advanced for you to start with.

I have this function for the same purpose that I cobbled together from
various sources -- probably the Access help files + some web searching:

'----- start of code -----
Function FillFileList( _
fld As Control, _
ID As Variant, _
row As Variant, _
Col As Variant, _
code As Variant) _
As Variant

Static avarFiles(1000) As Variant
Static intEntries As Integer
Dim ReturnVal As Variant

ReturnVal = Null
Select Case code
Case acLBInitialize ' Initialize.
intEntries = 0
avarFiles(intEntries) = _
Dir(Me.txtFolder & "\" & Nz(Me.txtPattern, "*.*"), _
CLng(Me.txtAttributes))
Do Until avarFiles(intEntries) = vbNullString _
Or intEntries >= 1000
intEntries = intEntries + 1
avarFiles(intEntries) = Dir
Loop
If intEntries > 1 Then
QSArray avarFiles, LBound(avarFiles), intEntries - 1
End If
ReturnVal = intEntries
Case acLBOpen ' Open.
' Generate unique ID for control.
ReturnVal = Timer
Case acLBGetRowCount ' Get number of rows.
ReturnVal = intEntries
Case acLBGetColumnCount ' Get number of columns.
ReturnVal = 1
Case acLBGetColumnWidth ' Column width.
' -1 forces use of default width.
ReturnVal = -1
Case acLBGetValue ' Get data.
ReturnVal = avarFiles(row)
Case acLBEnd ' End.
Erase avarFiles
End Select

FillFileList = ReturnVal

End Function


Private Sub QSArray( _
arrIn() As Variant, _
ByVal intLowBound As Integer, _
ByVal intHighBound As Integer)
Dim intX As Integer
Dim intY As Integer
Dim varMidBound As Variant
Dim varTmp As Variant
If intHighBound > intLowBound Then
varMidBound = arrIn((intLowBound + intHighBound) \ 2)
intX = intLowBound
intY = intHighBound
Do While intX <= intY
If arrIn(intX) >= varMidBound And arrIn(intY) <= varMidBound Then
varTmp = arrIn(intX)
arrIn(intX) = arrIn(intY)
arrIn(intY) = varTmp
intX = intX + 1
intY = intY - 1
Else
If arrIn(intX) < varMidBound Then
intX = intX + 1
End If
If arrIn(intY) > varMidBound Then
intY = intY - 1
End If
End If
Loop
Call QSArray(arrIn(), intLowBound, intY)
Call QSArray(arrIn(), intX, intHighBound)
End If
End Sub
'----- end of code -----
 

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