Saving to a folder

davesmith202

Employee of Access World
Local time
Today, 22:51
Joined
Jul 20, 2001
Messages
522
I have this code:



Code:
strMyFolderName=Me.PageID
fnum = FreeFile()
Open strFileName For Output As fnum
Print #fnum, strwebpage
Close #fnum

It prints strwebpage to the current folder.

How can I get it to print to a folder (that may or may not yet exist) who's name is stored in strMyFolderName?

Thanks,

Dave
 
Code:
Private Sub SomeSub()

   Dim mypath As String: mypath = "C:\MyFakeFolder\MyFile.txt"
   Dim myfolder As String: myfolder = GetFolder(mypath)
   Dim fso:  Set fso = CreateObject("Scripting.FileSystemObject")
   
   ' Create folder if necessary.
   If fso.FolderExists(myfolder) = False Then
      fso.CreateFolder (myfolder)
   End If
   
   ' Write to file code goes here...
   '
   '

End Sub

Function GetFolder(path As String) As String

  Dim myArray: myArray = Split(path, "\")
  Dim myfilename As String: myfilename = myArray(UBound(myArray))
  Dim myfolder As String: myfolder = Mid(path, 1, (Len(path) - (Len(myfilename) + 1)))

  GetFolder = myfolder

End Function

Regards,
Tim
 
Thanks for that code Tim.

How do you actually get it to write to a specific folder though? Yes, the folder is now created if it didn't exist before. But how do you tell Access to write to that folder, rather than the folder the database is in? How do you modify this line of code to specify the path?

Print #fnum, strwebpage
 
Code:
Private Sub Blah()

  Dim mypath As String: mypath = "C:\MyFakeFolder\MyFile.txt"
  Dim myfolder As String: myfolder = GetFolder(mypath)
  Dim fso:  Set fso = CreateObject("Scripting.FileSystemObject")
   
  ' Create folder if necessary.
  If fso.FolderExists(myfolder) = False Then
     fso.CreateFolder (myfolder)
  End If
   
  ' Get some fake data.
  Dim mydata As String: mydata = "XYZ"

  ' Open a file in a folder
  Dim fnum As Integer: fnum = FreeFile()
  Open mypath For Output As fnum

  ' Add data to the file.
  Print #fnum, mydata

  ' Done, so close the file.
  Close #fnum
    
End Sub

Function GetFolder(path As String) As String

  Dim myArray: myArray = Split(path, "\")
  Dim myfilename As String: myfilename = myArray(UBound(myArray))
  Dim myfolder As String: myfolder = Mid(path, 1, (Len(path) - (Len(myfilename) + 1)))

  GetFolder = myfolder

End Function

Regards,
Tim
 

Users who are viewing this thread

Back
Top Bottom