Solved

Run time Error

Posted on 2011-03-06
2
293 Views
Last Modified: 2012-05-11
Hi,

While using the below code in am getting a run time error 3075 because of a special character '(Apostrophe) in the FileItem.Name and FileItem.Path.

Your help on this is highly appreciated.

Sub List_Of_Files_In_Folder()
    ListFilesInFolder "I:\", True
End Sub

Sub ListFilesInFolder(SourceFolderName As String, IncludeSubfolders As Boolean)
Dim FSO As Scripting.FileSystemObject
Dim SourceFolder As Scripting.Folder, SubFolder As Scripting.Folder
Dim FileItem As Scripting.File
Dim r As Long
    Set FSO = New Scripting.FileSystemObject
    Set SourceFolder = FSO.GetFolder(SourceFolderName)
    For Each FileItem In SourceFolder.Files
          CurrentDb.Execute "INSERT INTO MyTable (CurrentFilePath,DestinationFilePath,ParentFolder,FileName,FileSize,FileType,DateCreated,DateLastAccessed,DateLastModified,Attributes,ShortFileName) " & _
                "VALUES (" & _
                "'" & FileItem.Path & "'," & _
                "'" & "D:\Archiving " & Left(FileItem.Path, 1) & " Drive" & Mid(FileItem.Path, 3, (Len(FileItem.Path))) & "'," & _
                "'" & Mid(FileItem.Path, 4, (InStr(4, FileItem.Path, "\") - 4)) & "'," & _
                "'" & FileItem.Name & "'," & _
                "'" & FileItem.Size & "'," & _
                "'" & FileItem.Type & "'," & _
                "'" & FileItem.DateCreated & "'," & _
                "'" & FileItem.DateLastAccessed & "'," & _
                "'" & FileItem.DateLastModified & "'," & _
                "'" & FileItem.Attributes & "'," & _
                "'" & FileItem.ShortPath & FileItem.ShortName & "'" & _
                ")"
    Next FileItem
    If IncludeSubfolders Then
        For Each SubFolder In SourceFolder.SubFolders
            ListFilesInFolder SubFolder.Path, True
        Next SubFolder
    End If
    Set FileItem = Nothing
    Set SourceFolder = Nothing
    Set FSO = Nothing
End Sub
0
Comment
Question by:Saichand
2 Comments
 
LVL 1

Assisted Solution

by:lalitgada
lalitgada earned 100 total points
ID: 35053440
use replace function and replace ' by "
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 400 total points
ID: 35054874
lalitgada is right, change this section:
                "VALUES (" & _
                "'" & FileItem.Path & "'," & _
                "'" & "D:\Archiving " & Left(FileItem.Path, 1) & " Drive" & Mid(FileItem.Path, 3, (Len(FileItem.Path))) & "'," & _
                "'" & Mid(FileItem.Path, 4, (InStr(4, FileItem.Path, "\") - 4)) & "'," & _
                "'" & FileItem.Name & "'," & _
                "'" & FileItem.Size & "'," & _
                "'" & FileItem.Type & "'," & _
                "'" & FileItem.DateCreated & "'," & _
                "'" & FileItem.DateLastAccessed & "'," & _
                "'" & FileItem.DateLastModified & "'," & _
                "'" & FileItem.Attributes & "'," & _
                "'" & FileItem.ShortPath & FileItem.ShortName & "'" & _
                ")"

Open in new window


to this:

                "VALUES (" & _
                "'" & Replace(FileItem.Path, "'", "''") & "'," & _
                "'" & Replace("D:\Archiving " & Left(FileItem.Path, 1) & " Drive" & Mid(FileItem.Path, 3, (Len(FileItem.Path))), "'", "''") & "'," & _
                "'" & Replace(Mid(FileItem.Path, 4, (InStr(4, FileItem.Path, "\") - 4)), "'", "''") & "'," & _
                "'" & Replace(FileItem.Name, "'", "''") & "'," & _
                "'" & FileItem.Size & "'," & _
                "'" & FileItem.Type & "'," & _
                "'" & FileItem.DateCreated & "'," & _
                "'" & FileItem.DateLastAccessed & "'," & _
                "'" & FileItem.DateLastModified & "'," & _
                "'" & FileItem.Attributes & "'," & _
                "'" & Replace(FileItem.ShortPath & FileItem.ShortName, "'", "''") & "'" & _
                ")"

Open in new window


Regards,

Rob.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Welcome back!  My apologies for taking so long to write part two of this series; it's been a long time coming!  As I promised in Part 1, this article will focus on how to locate those elusive AD properties that you are searching for.  Why is this us…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now