ACC: Sample Function to Generate a Random Temporary File Name

Last reviewed: August 29, 1997
Article 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 demonstrates how to use a sample function to generate 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 it is necessary for an application to create a file that is used temporarily and then deleted. 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 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 Windows uses to write its temporary files. This is handy for those who perform periodic maintenance on their hard drive 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):

          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.

Keywords          : kbprg PgmHowTo PgmFilM
Version           : 1.0 1.1 2.0 7.0 97
Platform          : WINDOWS
Hardware          : x86
Issue type        : kbhowto


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: August 29, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.