I’m developing a database program that deals with many auxiliary files at same time. Everyone codingdatabase programs must create some temporary files to produce an output from SQL or a temporarydatabase to manipulate those records efficiently. I decided to create a FileAux function that returns onetemporary file name. If I need to create more than one file into the same process, I simply store those namesinto variables I dimensioned before:
Function FileAux(Ext As String) _ As String Dim i As Long, X As String If InStr(Ext, ".") = 0 Then Ext = "." + Ext End If 'Look for the previous files in the 'HardDisk i = 0 Do X = "Aux" + Format$(i, "0000") _ + Ext If FileExists(X) Then i = i + 1 Else Exit Do End If Loop FileAux = XEnd Function
This function uses the “FileExists” function:
Function FileExist(filename As String) _ As Boolean FileExist = Dir$(filename) <> ""End Function
Here’s an example of its usage:
Sub Test() Dim File1 As String, File2 As _ String, File3 As String Dim DB1 As database, DB2 As DataBase Dim FileNum As Integer File1 = FileAux("MDB") Set DB1 = CreateDataBase(File1) File2 = FileAux("MDB") Set DB2 = CreateDataBase(File2) File3 = FileAux("TXT") FileNum = FreeFile Open File3 For OutPut As FileNum 'Your code ' ... Close FileNumEnd Sub
File1, File2, and File3 should be “Aux0001.MDB,” “Aux0002.MDB,” and “Aux0001.TXT,” respectively.