Microsoft KB Archive/210602

= ACC2000: Sample Function to Generate a Random Temporary File Name =

Article ID: 210602

Article Last Modified on 10/11/2006

-

APPLIES TO


 * Microsoft Access 2000 Standard Edition

-



This article was previously published under Q210602



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. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers 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 requirements.



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:   Create a module and type the following line in the Declarations section if it is not already there: Option Explicit   Type the following procedure: 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   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. 

Keywords: kbinfo kbprogramming KB210602

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.