How to find filepath of open MS Access file - vba

I have MS Access Switchboard created by my self and trying to figure it out how to find the filepath of the same one when it is open. For example if it is open in Desktop to get C:\Desktop and etc.
I really do not know if it is possible at all, but if someone have an idea how to do it in MS Access vba would be very helpfull.
Thanks.

Try use this to get current path file in vba
dim currentPath = CurrentProject.Path & "\"
dim currentFileName = CurrentProject.Name
dim fullPath = currentPath & currentFileName

Related

VBA FileExists and Sharepoint

I'm running into issues trying to pull info from files stored in Sharepoint.
Namely, FileExists isn't working and Overwrite file doesn't seem to be working either.
There was a discussion here, but few answers -> posting this question again in hopes some things have changed
My code runs like this:
strFileExists = Dir(Filepath & Filename)
And returns: File path not found -> I checked the path and even opened a file and recorded the macro to make sure it was the same file path without issue, but it appears DIR() is the issue.
The business dept I'm working with is entirely switching over to Sharepoint so hoping there's a straightforward solution without setting up network shares or doing C/personal/OneDrive things
You can navigate and look for files on OneDrive like this
Sub check_File_Exists()
Dim path As String
Dim strType As String
Dim file As Variant
Dim yourFile As String
'replace uname with your user name
path = "C:\Users\uname\OneDrive\"
strType = "*txt"
yourFile = "test.txt"
file = Dir(path & strType)
Do While (file <> "")
If file = yourFile Then
Debug.Print ("File: " & file & " found!")
Exit Do
End If
file = Dir
Loop
End Sub
Hope it helps

Trying to Open a File with a Button from Access using ShellExecute

In my Access database, I have a button on a form to open an external file.
Here is the code that I am using for that.
Private Sub btn_OpenFile_Click()
Dim a As New Shell32.Shell
Dim strPath As String
strPath = Me.Attachment
strPath = Chr(34) & strPath & Chr(34)
Call a.ShellExecute(Me.Attachment)
'Call CreateObject("Shell.Application").ShellExecute(strPath)
'MsgBox strPath
End Sub
The problem that I have is if I actually put in the value of the variable (Me.Attachment) it works fine and opens the program and the file.
For Example, If I put in Call a.ShellExecute("C:\Docs\Some File.pdf") it will open. But if I use the variable in it's place it won't open and tells me it cannot find the file. I have verified with the msgbox that it is receiving the correct information. I have tried to wrap it in quotes and have used the Chr(34) as shown above but nothing works.
How can I get that variable to work in the ShellExcute command?
I have looked through all the forums and it seems like everyone is using a string but not a variable. I don't want to use just the shell command as I don't want to track down all of the different apps people use to open different types of files. There will be different file types that will need to be opened and I thought this would be easier than it actually is.
Thank you for the help.
I'm pretty sure that ShellExecute expects a Variant parameter, not a String.
So try this:
Call a.ShellExecute(CVar(strPath))
or use a Variant variable from the start.
I had the same problem here.
Both of the following work for me:
Dim a As New Shell32.Shell
Dim strPath As String
strPath = Me.Attachment
Call a.ShellExecute(strPath)
Dim a As Shell
Dim strPath As String
strPath = Me.Attachment
Set a = CreateObject("Shell.Application")
a.ShellExecute strPath
Even referencing Attachment directly.
a.ShellExecute(Me.Attachment)

VBA open a csv file with unknow file name

I have got a rather simple question as i think but i couldnt find out it myself.
I want to open a csv File in a defined binder but with an unknow filename. I would asume that it should work with simply "path/*.csv" however it is not :( The errormessage says "Wrong Filename". Do i need to use something else in VBA.
path = ActiveWorkbook.path & "\input\"
Open path & "*.csv" For Binary As #1
The above code does not work for me :( The CSV is called xyz.csv
path = ActiveWorkbook.path & "\input\"
Open path & "xyz.csv" For Binary As #1
The code above is working however i have fix added the csv filename, which in this case is xyz.
Somebudy knows how to get that thing to work?
Cheers and thx for your time
Marc
Dim path As String
Dim csvFiles As String
path = ActiveWorkbook.path & "\input\"
csvFiles = Dir(path & "*.csv")
Do While Len(csvFiles) > 0
Debug.Print csvFiles
csvFiles = Dir
Loop
You can use the Dir() Function to check the files in your folder if you don't know the filename.

VB.net use string in Directory Path?

I apologise for my lack of basic VB.net knowledge but I'm looking to use the equivalent of %systemdrive% to find the drive containing Windows to check for an existing directory in VB.net - so I have the below.
Dim systemPath As String = Mid(Environment.GetFolderPath(Environment.SpecialFolder.System), 1, 3)
If Not Directory.Exists("'systemPath'\MyFolder") Then
Directory.CreateDirectory("'systemPath'\MyFolder")
End If
Can someone help with using the systemPath string in the directory query? Thank you.
Well you should write
Dim systemPath As String = Mid(Environment.GetFolderPath(Environment.SpecialFolder.System), 1, 3)
If Not Directory.Exists(Path.Combine(systemPath, "MyFolder")) Then
Directory.CreateDirectory(Path.Combine(systemPath, "MyFolder"))
End If
You could get the environment variable called %SYSTEMDRIVE% with Environment.GetEnvironmentVariable, but then the results obtained should be manually combined with current directory separator char ("\") because I have not found any way to convince Path.Combine to build a valid path with only the system drive (I.E. C: )
Dim sysDrive = Environment.GetEnvironmentVariable("SystemDrive")
Dim myPath = sysDrive & Path.DirectorySeparatorChar & "MyFolder"
IO.Path has methods that should be used IMHO
Dim sysDrive As String = Environment.GetEnvironmentVariable("SystemDrive") & IO.Path.DirectorySeparatorChar
Dim myPath As String = IO.Path.Combine(sysDrive, "FolderName")

Screenshot window and save with environment variables

I have code to take a screenshot of a window, seen here:
SC.CaptureWindowToFile(Me.Handle, "c:\Program Files\image_" & Now.ToString("yyyyMMddHHmmss") & ".png", Imaging.ImageFormat.Png)
I want to be able to set an environment variable so that the images will be saved in a folder created by my installer, let's call it "Screenshots" and it's in the Documents folder. I assume I'll have to use something like:
Dim fullPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
But I don't know how to combine these two together so it will save the screenshots into the folder in my documents. Any ideas?
If I understand your question, this should do it:
Dim myDocumentsPath as String = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
Dim formattedDateString as String = Date.Now.ToString("yyyyMMddHHmmss")
Dim imagePath as String = String.Format("{0}\Screenshots\image_{1}.png", myDocumentsPath, formattedDateString)
SC.CaptureWindowToFile(Me.Handle, imagePath, Imaging.ImageFormat.Png)