Solved problem with Dir function

poppajim713

New member
Local time
Today, 08:11
Joined
Dec 1, 2022
Messages
20
I've tried using all text within the Dir function like sFile = "C:\Users\file.txt" which works, but trying
Dim sTxt As String
sTxt = "file.txt"
sFile = "C:\Users\" & sTxt doesn't work. The file is not found.
 
Welcome to the forums! We are the most active Microsoft Access community on the internet by far, with posts going back over 20 years!

To get started, I highly recommend you read the post below. It contains important information for all new users to this forum.

https://www.access-programmers.co.uk/forums/threads/new-member-read-me-first.223250/

We look forward to having you around here, learning stuff and having fun!
 
maybe use:

what if you use:

sFile =Environ("Userprofile") & "\" & sTxt
Debug.Print Dir$(sFile)
 
And what do you get if you debug.print sFile?
 
Well that cannot be right?, not if it starts with "C:\Users\" ?
This is exactly what I tested.
Code:
sTxt = "12-1-2022.mdb"
sFile = Environ("Userprofile") & "\Backup\" & sTxt
Debug.Print sFile
nothing happens. Thanks anyway.
 
You were looking at the Immediate window I hope? :(
 
Just tested in immediate window (using A365/64 bit)
sTxt = "12-1-2022.mdb"
sFile = Environ("Userprofile") & "\Backup\" & sTxt
Debug.Print sFile

Result:
C:\Users\JP\Backup\12-1-2022.mdb
 
Just tested in immediate window (using A365/64 bit)
sTxt = "12-1-2022.mdb"
sFile = Environ("Userprofile") & "\Backup\" & sTxt
Debug.Print sFile

Result:
C:\Users\JP\Backup\12-1-2022.mdb
I did a bit of research. I think Environ started with Access 2007. I'm still using 2003.
 
I tried some code that found a folder, but not a file in that folder
this is the code I tried. the first one the folder exist, but the second one the file doesn't exist.
Code:
   Dim FolderPath As String

    FolderPath = "C:\Users\smltu\Backup"
    If Right(FolderPath, 1) <> "\" Then
        FolderPath = FolderPath & "\"
    End If

    If Dir(FolderPath, vbDirectory) <> vbNullString Then
        MsgBox "Folder exist"

    Else
        MsgBox "Folder doesn't exist"
    End If
    
    Dim FilePath As String
    Dim TestStr As String

    FilePath = "C:\Users\smltu\Backup\12-1-2022.mdb"

    TestStr = ""
    On Error Resume Next
    TestStr = Dir(FilePath)
    On Error GoTo 0
    If TestStr = "" Then
        MsgBox "File doesn't exist"
    Else
        MsgBox "File exist"
    End If
 
this is the code I tried. the first one the folder exist, but the second one the file doesn't exist.
Code:
   Dim FolderPath As String

    FolderPath = "C:\Users\smltu\Backup"
    If Right(FolderPath, 1) <> "\" Then
        FolderPath = FolderPath & "\"
    End If

    If Dir(FolderPath, vbDirectory) <> vbNullString Then
        MsgBox "Folder exist"

    Else
        MsgBox "Folder doesn't exist"
    End If
   
    Dim FilePath As String
    Dim TestStr As String

    FilePath = "C:\Users\smltu\Backup\12-1-2022.mdb"

    TestStr = ""
    On Error Resume Next
    TestStr = Dir(FilePath)
    On Error GoTo 0
    If TestStr = "" Then
        MsgBox "File doesn't exist"
    Else
        MsgBox "File exist"
    End If
this is the code I tried. the first one the folder exist, but the second one the file doesn't exist.
Code:
   Dim FolderPath As String

    FolderPath = "C:\Users\smltu\Backup"
    If Right(FolderPath, 1) <> "\" Then
        FolderPath = FolderPath & "\"
    End If

    If Dir(FolderPath, vbDirectory) <> vbNullString Then
        MsgBox "Folder exist"

    Else
        MsgBox "Folder doesn't exist"
    End If
   
    Dim FilePath As String
    Dim TestStr As String

    FilePath = "C:\Users\smltu\Backup\12-1-2022.mdb"

    TestStr = ""
    On Error Resume Next
    TestStr = Dir(FilePath)
    On Error GoTo 0
    If TestStr = "" Then
        MsgBox "File doesn't exist"
    Else
        MsgBox "File exist"
    End If
I changed one line to this and it worked.
Code:
FilePath = "C:\Users\smltu\Backup\*.mdb"
 
Are you sure there weren't any stray spaces in any of the strings causing the issues?
 

Users who are viewing this thread

Back
Top Bottom