ACC2000: Sample Function to Generate a Random Temporary File Name

ID: Q210602


The information in this article applies to:
  • Microsoft Access 2000

Moderate: Requires basic macro, coding, and interoperability skills.


SUMMARY

This article shows you how to create a sample function that generates a random file name that can be used for a temporary file.

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact a Microsoft Certified Solution Provider or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Solution Providers, please see the following page on the World Wide Web:

http://www.microsoft.com/mcsp/
For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

http://www.microsoft.com/support/supportnet/overview/overview.asp


MORE INFORMATION

Sometimes, an application has to create a file that it uses temporarily and then deletes. The names of temporary files are important and you do not want your application to inadvertently overwrite an existing file.

In such cases, you can create and use the user-defined function called MakeTempFileName(). This function accepts a file extension and returns a string representing a non-existent, random file name with the file extension that you specify. The file name includes a path to your Windows Temp folder (directory) so that the temporary file is placed in the same folder that Windows uses to write its temporary files. This is handy for those who perform periodic maintenance on their hard disks and delete any leftover temporary files in the Windows Temp folder.

To create the sample function MakeTempFileName(), follow these steps:

  1. Create a module and type the following line in the Declarations section if it is not already there:


  2. 
    Option Explicit 
  3. Type the following procedure:


  4. 
    Function MakeTempFileName(Extension As String) As String
    
       Dim Isfile As Integer, FHandle As Integer, Cntr As Integer
       Dim WinTemp As String, TF As String
    
       Isfile = False
       FHandle = FreeFile
    
       WinTemp = Environ("TEMP") & "\"
       For Cntr = 1 To 8
          WinTemp = WinTemp & Mid(LTrim(Str(CInt(Rnd * 10))), 1, 1)
       Next Cntr
    
       TF = Trim(WinTemp$) & "." & Extension
    
       Open TF For Output As #Fhandle
       Debug.Print TF
       Print #FHandle, "This is a Temp file"
         
       Close #FHandle
       MakeTempFileName = TF
    
    End Function 
  5. Type the following line in the Immediate window and press ENTER:
    
    TempFileName$ = MakeTempFileName("TMP") 
    Note that the name of the temporary file is displayed in the Immediate window. The file contains the line "This is a Temp file." After you have verified that the file exists in your Temp folder, you can delete it.


Additional query words:

Keywords : kbprg kbdta AccCon KbVBA
Version : WINDOWS:2000
Platform : WINDOWS
Issue type : kbinfo


Last Reviewed: July 6, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.