Microsoft KB Archive/88929

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

Article ID: 88929

Article Last Modified on 1/18/2007

-

APPLIES TO


 * Microsoft Access 1.0 Standard Edition
 * Microsoft Access 1.1 Standard Edition
 * Microsoft Access 2.0 Standard Edition
 * Microsoft Access 95 Standard Edition
 * Microsoft Access 97 Standard Edition

-



This article was previously published under Q88929



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.

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 &quot;Building Applications with Microsoft Access&quot; 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 &quot;Introduction to Programming&quot; manual in Microsoft Access version 1.x or the &quot;Building Applications&quot; 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:

  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(&quot;TEMP&quot;) & &quot;\&quot; For Cntr = 1 To 8 WinTemp = WinTemp & Mid(LTrim(Str(CInt(Rnd * 10))), 1, 1) Next TF = Trim(WinTemp$) & &quot;.&quot; & Extension

Open TF For Output As #Fhandle Debug.Print TF        Print #FHandle, &quot;This is a Temp file&quot; Loop While Err > 0 Close #FHandle MakeTempFileName = TF

End Function  Type the following line in the Debug window (or Immediate window in versions 1.x and 2.0), and then presss ENTER:

TempFileName$ = MakeTempFileName(&quot;TMP&quot;)

Note that the name of the temporary file is displayed in the Debug window. The file contains the line &quot;This is a Temp file.&quot; After you have verified that the file exists in your Temp folder, you can delete it.

Keywords: kbhowto kbprogramming KB88929

-

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

© Microsoft Corporation. All rights reserved.