VBA 在特定文件夹中打开 MyComputer

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3619200/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 12:03:18  来源:igfitidea点击:

VBA to open MyComputer at a specific folder

ms-accessvba

提问by DarkW1nter

Ive just inherited an MS Access 2003 system and need a bit of VBA to put behind a command button to open MyComputer at a specific folder to view the files there. The folder name will come from a field on a form. I did have

我刚刚继承了一个 MS Access 2003 系统,需要一点 VBA 来放置一个命令按钮,以在特定文件夹中打开 MyComputer 以查看那里的文件。文件夹名称将来自表单上的字段。我确实有

Application.FollowHyperLink "C:\" & Me![ref] (ref is in the format abcd-1234)

Application.FollowHyperLink "C:\" & Me![ref] (ref 格式为 abcd-1234)

when its hard-coded it works fine, but i cant seem to get it to open when picking the foldername up from the form.

当它硬编码它工作正常时,但我似乎无法从表单中选择文件夹名称时打开它。

any hints? (other than binning access!) thanks

任何提示?(除了分级访问!)谢谢

回答by HansUp

See what the text looks like as you're submitting it to FollowHyperlink. You can insert a line like this in your code:

在您将文本提交到 FollowHyperlink 时,查看文本的外观。您可以在代码中插入这样的一行:

MsgBox "C:\" & Me![ref]

Perhaps it's not what you expect. It's always good to check.

也许这不是您所期望的。检查一下总是好的。

What happens when it doesn't work. Do you see any error messages or any other symptoms which could help us nail this down?

当它不起作用时会发生什么。您是否看到任何错误消息或任何其他可以帮助我们确定问题的症状?

My first thought was spaces in a folder name might create problems. But I don't think that's the answer because FollowHyperlink works fine for me in this example:

我的第一个想法是文件夹名称中的空格可能会产生问题。但我认为这不是答案,因为在这个例子中 FollowHyperlink 对我来说很好用:

Application.FollowHyperlink "C:\Access\spaces in name\"

So the best I can offer is to see what you're asking FollowHyperlink to use. If that effort doesn't lead you to the answer, add a specific example which fails to your question.

所以我能提供的最好的就是看看你要求 FollowHyperlink 使用什么。如果这种努力没有引导您找到答案,请添加一个无法解决您的问题的具体示例。

回答by user3133334

This code always work for me:

这段代码总是对我有用:

Dim filePath = <"Insert the path of the directory to open inside of opening and closing parenthesis">

Application.FollowHyperlink filePath, vbNormalFocus

Normally, I store a few directories in a table inside the DBMS, which helps when linking hundreds of images to a database instead of embedding. For instance, I have a table called, "dbLocations." Inside this table, there are only two fields: 1) picLocation 2) Description.

通常,我在 DBMS 内的表中存储一些目录,这有助于将数百张图像链接到数据库而不是嵌入。例如,我有一个名为“dbLocations”的表。在这个表中,只有两个字段:1) picLocation 2) 描述。

The field picLocation has the value of the network path, i.e, C:\My Documents or G:\Whatever Directory or \\groups1\for UNC paths.

字段 picLocation 具有网络路径的值,即 C:\My Documents 或 G:\Whatever Directory 或 \\groups1\(对于 UNC 路径)。

The field Description is what it implies, a description of the picLocation.

字段描述是它所暗示的,是对 picLocation 的描述。

I use tables to store directory locations because they linking to files (.jpg, .png) stored on a network drive. As time evolves, directories can get changed (I move a folder to another location, or if the UNC changes, etc.).

我使用表来存储目录位置,因为它们链接到存储在网络驱动器上的文件(.jpg、.png)。随着时间的推移,目录可能会发生变化(我将文件夹移动到另一个位置,或者 UNC 发生变化等)。

If you hard code the location(s) over several subs or modules, you will need to change each one; which is very inefficient. So, in order to save time and a lot of headaches, I use the Domain Lookup function which allows me to only change the file location just once and in an easy to find place, namely, the dbLocations table.

如果您对多个子项或模块的位置进行硬编码,则需要更改每一个;这是非常低效的。所以,为了节省时间和很多麻烦,我使用了域查找功能,它允许我只更改一次文件位置并且在一个容易找到的地方,即 dbLocations 表。

In essence, I am looking up the value of the location inside the table where the picLocation matches the description of, Alert Pics. (I am creating a database that will be used to track Trespassers and other vagrant persons for work)

实质上,我正在查找表中 picLocation 与 Alert Pics 的描述相匹配的位置的值。(我正在创建一个数据库,用于跟踪侵入者和其他流浪者的工作)

Dim filePath as String

filePath = DLookup("picLocation", "dbLocations", "[Description] = 'Alert Pics'")
Application.FollowHyperlink filePath, vbNormalFocus    

With these three lines of simple code, you can navigate to a specific directory.

通过这三行简单的代码,您可以导航到特定目录。