File checking

  • Thread starter Thread starter BrettsNEWS
  • Start date Start date
B

BrettsNEWS

Hello,

I would like to check if certain files exist on the computer on which I wish
my project to run. I can find no vba code anywhere for a simple check on
whether for instance "myfile.txt" exists on the present computer, with the
obvious true or false response.
I wish to do this as an extra security feature to make sure the project
isn't run on another computer.

Anyone any ideas?
Thanks
Brett.
 
You can simply use Len() & Dir()

?Len(Dir("D:\Thien\db2.mdb"))
7

i.e. returns the length of the file name if it exists

?Len(Dir("D:\NonExistent.File"))
0

non-existent file will returns zero.
 
Hi There,
Thanks for the lead, tried it by pasting it in but when it ran it came up
with

..FileType = msoFileTypeAllFiles
Run time error 5
Invalid procedure call or argument

Brett.
 
Having a bit of trouble using this, I'm using access 2002 and it keeps
converting the "?" at the start to a PRINT instruction. and I can't
understand the help on the Lan instruction/function

Here is what VBA Help came up with
***************************************************
Len Function Example
The first example uses Len to return the number of characters in a string or
the number of bytes required to store a variable. The Type...End Type block
defining CustomerRecord must be preceded by the keyword Private if it
appears in a class module. In a standard module, a Type statement can be
Public.

Type CustomerRecord ' Define user-defined type.
ID As Integer ' Place this definition in a
Name As String * 10 ' standard module.
Address As String * 30
End Type

Dim Customer As CustomerRecord ' Declare variables.
Dim MyInt As Integer, MyCur As Currency
Dim MyString, MyLen
MyString = "Hello World" ' Initialize variable.
MyLen = Len(MyInt) ' Returns 2.
MyLen = Len(Customer) ' Returns 42.
MyLen = Len(MyString) ' Returns 11.
MyLen = Len(MyCur) ' Returns 8.

The second example uses LenB and a user-defined function (LenMbcs) to return
the number of byte characters in a string if ANSI is used to represent the
string.

Function LenMbcs (ByVal str as String)
LenMbcs = LenB(StrConv(str, vbFromUnicode))
End Function

Dim MyString, MyLen
MyString = "ABc"
' Where "A" and "B" are DBCS and "c" is SBCS.
MyLen = Len(MyString)
' Returns 3 - 3 characters in the string.
MyLen = LenB(MyString)
' Returns 6 - 6 bytes used for Unicode.
MyLen = LenMbcs(MyString)
' Returns 5 - 5 bytes used for ANSI.

*******************************************************

What do you think of that lot??

Regards
Brett.
 
Use the FileSystem object.

Add a reference to Microsoft Scripting Runtime

Then you can use the DoesFileExist, as shown below

Dim FSO As FileSystemObject
Dim sFileSpec As String

' Create a FleSystemObject
Set FSO = New FileSystemObject
If (FSO.FileExists(FileSpec:=sFileSpec)) Then
' do whatever
End If

sFileSpec is a string containing the fully qualified file spec, eg.
"C:\SomeFolder\SomeFile.txt"

Ragnar
 
Thanks for that, I'll take it to work with me and have a go, (I'm not a
computer wiz with all this, but a ships Engineer trying to build a database
for the ships stores and maintenance. I,ll let you know how I get on.
Regards
Brett.
 
? means PRINT in the Debug window.

The example in Help is much too complicated. Just try:

?Dir("FullPathToYourDatabase")

which will return the non-zero length of the name. If the file doesn't
exist, Dir() will return zero.
 
Back
Top