Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to check if a file is in use?

Posted on 2009-06-30
2
Medium Priority
?
280 Views
Last Modified: 2013-11-27
Hi.  I have a form in MS Access that writes strings to a text file.  This text file is actually a macro script for a different application.  So, with a click of a button, here's what the form does:
1. Creates the script using the "Print" command to put lines of text in a text file.
2. opens up the application.
3. Using Sendkeys, send the command to that application to run the macro script.
This all works, but here's my problem:  I also need the text file that is created to be deleted right away.  Problem with the execution is it won't because the file is in use at that point by the other application.  I had thought the solution would be to create a while loop to try and delete the file while the file exists.  This does not work.  I am thinking that if there is a way to check if the file was in use instead in the while loop would work, but i'm not sure.  Anybody know of a way for me to code it to delete the file once it is not in use anymore?  Don't want to use a timer because i want to make sure the file is deleted as soon as it can be deleted.
Private Sub Command10_Click()
If IsNull(Me.JDA) = True Then GoTo Fieldsempty
If IsNull(Me.sku) = True Then GoTo Fieldsempty
If IsNull(Me.qty) = True Then GoTo Fieldsempty
If IsNull(Me.customer) = True Then GoTo Fieldsempty
If IsNull(Me.warehouse) = True Then GoTo Fieldsempty
'Define all the variables that are going to be used
Dim strInputVal As String
Dim strOutputVal As String
Dim strChar As String
Dim strMocha As String
Dim fso As Object, fsoFile As Object
'Check to see if there already is a macro present
If FileExists("C:\mochasoft\mtn5250.8") = True Then
'Delete the previous jda macro
Set fso = CreateObject("Scripting.FileSystemObject")
  Set fsoFile = fso.GetFile("c:\mochasoft\mtn5250.8")
  fsoFile.Delete
   End If
 'Open the macro to write to
 Open "c:\mochasoft\mtn5250.8" For Output As #1
 'write the inital line to the jda macro file
Print #1, "# Script file for Mocha W32 TN5250"
olwritejda Me.jdauser
Print #1, "9 0 0 f 1"
olwritejda Me.jdapass
Print #1, "d 0 0 1c 1"
Print #1, "d 0 0 1c 1"
Print #1, "31 0 0 0 0"
Print #1, "38 0 0 0 0"
Print #1, "31 0 0 0 0"
Print #1, "d 0 0 1c 1"
olwritejda Me.JDA
Print #1, "71 0 0 3c 1"
Print #1, "71 0 0 3c 1"
olwritejda Me.sku
Print #1, "9 0 0 f 1"
Print #1, "27000 0 0 14d 1"
Print #1, "27000 0 0 14d 1"
Print #1, "27000 0 0 14d 1"
Print #1, "27000 0 0 14d 1"
Print #1, "27000 0 0 14d 1"
Print #1, "2e 0 0 153 1"
Print #1, "2e 0 0 153 1"
Print #1, "2e 0 0 153 1"
Print #1, "25000 0 0 14b 1"
Print #1, "25000 0 0 14b 1"
Print #1, "25000 0 0 14b 1"
Print #1, "25000 0 0 14b 1"
Print #1, "25000 0 0 14b 1"
olwritejda Me.qty
Print #1, "76 0 0 41 1"
Print #1, "28000 0 0 150 1"
Print #1, "28000 0 0 150 1"
Print #1, "28000 0 0 150 1"
Print #1, "28000 0 0 150 1"
Print #1, "28000 0 0 150 1"
Print #1, "28000 0 0 150 1"
Print #1, "28000 0 0 150 1"
olwritejda Me.warehouse
Print #1, "28000 0 0 150 1"
Print #1, "39 0 0 0 0"
Print #1, "39 0 0 0 0"
Print #1, "39 0 0 0 0"
Print #1, "39 0 0 0 0"
Print #1, "39 0 0 0 0"
Print #1, "28000 0 0 150 1"
Print #1, "25000 0 0 14b 1"
Print #1, "25000 0 0 14b 1"
Print #1, "25000 0 0 14b 1"
Print #1, "25000 0 0 14b 1"
Print #1, "25000 0 0 14b 1"
Print #1, "25000 0 0 14b 1"
Print #1, "25000 0 0 14b 1"
olwritejda Me.customer
Print #1, "76 0 0 41 1"
Print #1, "76 0 0 41 1"
Print #1, "72 0 0 3d 1"
Print #1, "70 0 0 3b 1"
Print #1, "70 0 0 3b 1"
Print #1, "31 0 0 0 0"
Print #1, "33 0 0 0 0"
olwritejda Me.JDA
Print #1, "d 0 0 1c 1"
Print #1, "28000 0 0 150 1"
Print #1, "28000 0 0 150 1"
Print #1, "28000 0 0 150 1"
Print #1, "28000 0 0 150 1"
Print #1, "28000 0 0 150 1"
Print #1, "28000 0 0 150 1"
Print #1, "28000 0 0 150 1"
Print #1, "28000 0 0 150 1"
Print #1, "28000 0 0 150 1"
Print #1, "26000 0 0 148 1"
Print #1, "25000 0 0 14b 1"
Print #1, "25000 0 0 14b 1"
Print #1, "25000 0 0 14b 1"
Print #1, "25000 0 0 14b 1"
Print #1, "25000 0 0 14b 1"
Print #1, "25000 0 0 14b 1"
Print #1, "25000 0 0 14b 1"
Print #1, "25000 0 0 14b 1"
Print #1, "25000 0 0 14b 1"
Print #1, "25000 0 0 14b 1"
Print #1, "25000 0 0 14b 1"
Print #1, "25000 0 0 14b 1"
Print #1, "25000 0 0 14b 1"
Print #1, "25000 0 0 14b 1"
Print #1, "25000 0 0 14b 1"
Print #1, "25000 0 0 14b 1"
Print #1, "25000 0 0 14b 1"
Print #1, "25000 0 0 14b 1"
Print #1, "36 0 0 0 0"
Print #1, "d 0 0 1c 1"
Print #1, "75 0 0 40 1"
Print #1, "63 0 0 0 0"
Print #1, "72 0 0 0 0"
Print #1, "65 0 0 0 0"
Print #1, "64 0 0 0 0"
Print #1, "69 0 0 0 0"
Print #1, "74 0 0 0 0"
Print #1, "20 0 0 0 0"
Print #1, "69 0 0 0 0"
Print #1, "73 0 0 0 0"
Print #1, "73 0 0 0 0"
Print #1, "75 0 0 0 0"
Print #1, "65 0 0 0 0"
Print #1, "64 0 0 0 0"
Print #1, "9 0 0 f 1"
Print #1, "9 0 0 f 1"
Print #1, "9 0 0 f 1"
Print #1, "53 0 0 0 0"
Print #1, "4b 0 0 0 0"
Print #1, "55 0 0 0 0"
Print #1, "3a 0 0 0 0"
'Print #1, "41 0 0 0 0"
'Print #1, "6d 0 0 0 0"
'Print #1, "6f 0 0 0 0"
'Print #1, "75 0 0 0 0"
'Print #1, "6e 0 0 0 0"
'Print #1, "74 0 0 0 0"
'Print #1, "3a 0 0 0 0"
olwritejda Me.sku
Print #1, "9 0 0 f 1"
Print #1, "52 0 0 0 0"
Print #1, "65 0 0 0 0"
Print #1, "61 0 0 0 0"
Print #1, "73 0 0 0 0"
Print #1, "6f 0 0 0 0"
Print #1, "6e 0 0 0 0"
Print #1, "3a 0 0 0 0"
olwritejda DLookup("[Field2]", "customer", "[Field1] = '" & Me.customer & "'")
Print #1, "70 0 0 3b 1"
Print #1, "70 0 0 3b 1"
Print #1, "70 0 0 3b 1"
Print #1, "70 0 0 3b 1"
Close #1
Shell "c:\Mochasoft\mtn5250.exe"
SendKeys "%8", True
Do While FileExists("C:\mochasoft\mtn5250.8") = True
fsoFile.Delete
Loop
'Me.JDA = Null
'Me.sku = Null
'Me.qty = Null
'Me.warehouse = Null
'Me.customer = Null
Exit Sub
Fieldsempty:
MsgBox "All fields are required."
Exit Sub
End Sub

Open in new window

0
Comment
Question by:liebs
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 2

Accepted Solution

by:
JohnBobH earned 1500 total points
ID: 24750639
0
 
LVL 16

Expert Comment

by:Chuck Wood
ID: 24754354
Try this:
Public Function WaitForFile()
    '================================
    '  requires a reference to:
    '    Microsoft Scripting Runtime
    '================================
    Dim fso As New Scripting.FileSystemObject
    '
    ' ...
    '
    ' turn off error handling
    On Error Resume Next
    ' try to delete the file while it exists
    Do While fso.FileExists("C:\mochasoft\mtn5250.8")
        fso.DeleteFile "C:\mochasoft\mtn5250.8", True
    Loop
    ' turn error handling back on
    On Error GoTo 0
End Function

Open in new window

0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question