Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with Access VBA Script 2

Status
Not open for further replies.

TSch

Technical User
Jul 12, 2001
557
DE
Hi folks,

a Colleague of mine integrated some VBA Scripts into one of his Access Databases. There's one part where the script's supposed to create a txt file containg certain information.

Everytime the script runs it first deletes the old file using "kill" command.

Problem is, if there's no txt file existing, the script aborts and doesn't continue with the creation of the txt file. How would I have to change the "kill" command, so that in case there's no txt file existing the script won't bother and goes on instead of aborting ...

I'm afraid I have no idea about VBA ...

Regards
Thomas
 
You could use Dir() to see if the file exists before you try and kill it.

Hope this helps

HarleyQuinn
---------------------------------
Black coat, white shoes, black hat, cadillac. The boy's a timebomb!

You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before post
 
I would not really suggest On error resume next, since You never know which error might pop up, and with that command, you will never know.

Probably something that catches the error:

Code:
 On error Goto ErrorHandler

At the end of your sub:

Code:
Exit sub

ErrorHandler:

If Err = [The specific error number]

End sub

But I sincerely doubt you need to trap an error in this process, and I am certain that the abortion you mention is part of the code, to prevent errors.

could you maybe post your code, in order to help people help you.

Cheers,

Julien



"Knowing that you know is the greatest sign of stupidity, knowing that you are ignorant is the best proof of intelligence.
 
Hi folks,

there's currently not much to post. It's only one line:

[/code]
Kill "filename.txt"
[\code]

And if there's no file existing that can be deleted I get the following:

[/code]
Runtime Error 53
File not found
[\code]

My problem is that I have absolutely no idea what code I'd have to wrap around this one line in order to prevent this from happen because I'm extremely unfamiliar with VB :-(

Regards
Thomas
 

How about something like:
Code:
Private Sub KillFile()
    On Error GoTo Err59
    Kill "filename.txt"
FileNotFound:
    [COLOR=green][i]'more code here if necessary[/i][/color]
    Exit Sub
Err59:
    If Err.Number = 59 Then
        MsgBox "filename.txt not found", vbOKOnly
        GoTo FileNotFound
    End If
End Sub


Randy
 
How about
Code:
Dim Filename As String
Filename = "Path\file.ext"
If dir(Filename)>"" Then
   kill Filename
End if
 
Hi Randy and Pwise,

thanks a lot, that's exactly what I need !

Regards,
Thomas
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top