ACC: Sample Function to Generate a Random Temporary File Name

ID: Q88929


The information in this article applies to:
  • Microsoft Access versions 1.0, 1.1, 2.0, 7.0, 97


SUMMARY

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

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

This article assumes that you are familiar with Visual Basic for Applications and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information about Visual Basic for Applications, please refer to your version of the "Building Applications with Microsoft Access" manual.

NOTE: Visual Basic for Applications is called Access Basic in Microsoft Access versions 1.x and 2.0. For more information about Access Basic, please refer to the "Introduction to Programming" manual in Microsoft Access version 1.x or the "Building Applications" manual in Microsoft Access version 2.0.


MORE INFORMATION

Sometimes, an application has to create a file that it uses temporarily and then deletes. The names of temporary files are important because 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 specified. 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 procedure:
    
          Function MakeTempFileName(Extension As String) As String
          On Error Resume Next
          Dim Isfile As Integer, FHandle As Integer, Cntr As Integer
          Dim WinTemp As String, TF As String
             Isfile = False
             FHandle = FreeFile
    
          Do
             WinTemp = Environ("TEMP") & "\"
             For Cntr = 1 To 8
             WinTemp = WinTemp & Mid(LTrim(Str(CInt(Rnd * 10))), 1, 1)
             Next
                TF = Trim(WinTemp$) & "." & Extension
    
             Open TF For Output As #Fhandle
          Debug.Print TF
             Print #FHandle, "This is a Temp file"
          Loop While Err > 0
          Close #FHandle
          MakeTempFileName = TF
    
       End Function 


  2. Type the following line in the Debug window (or Immediate window in versions 1.x and 2.0), and then presss ENTER:
    TempFileName$ = MakeTempFileName("TMP")
    Note that the name of the temporary file is displayed in the Debug 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 PgmFilm
Version : WINDOWS:1.0,1.1,2.0,7.0,97
Platform : WINDOWS
Issue type : kbhowto


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