vba 当用户单击表单上的按钮时,从存储在访问数据库中的路径打开文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3303136/
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
open file from path stored in access db when user clicks button on form
提问by LFurness
I'm new to Access VBA development and being asked to debug and add features to an Access 2007 application that two previous developers worked on.
我是 Access VBA 开发的新手,被要求调试和添加功能到两个以前的开发人员工作的 Access 2007 应用程序。
A form displays records from a database and shows a button for each record. The button is supposed to open a file using the appropriate path. But when the user clicks the button, it always uses the filepath from the first record that the form displays, instead of the filepath from the correct record.
表单显示数据库中的记录,并为每条记录显示一个按钮。该按钮应该使用适当的路径打开文件。但是当用户单击按钮时,它总是使用表单显示的第一条记录的文件路径,而不是正确记录的文件路径。
The code looks like it is trying to use a bookmark to open the correct file, but as stated above, that isn't working. Here is the relevant code from the button click event. When I try to Debug.Print form.Bookmark to the immediate window, it just displays a question mark.
代码看起来像是在尝试使用书签打开正确的文件,但如上所述,这是行不通的。这是按钮单击事件的相关代码。当我尝试 Debug.Print form.Bookmark 到即时窗口时,它只显示一个问号。
Dim rs As Recordset
Set rs = form.RecordsetClone
rs.Bookmark = form.Bookmark
Edit: adding more code per @Remou's request. When button is clicked:
编辑:根据@Remou 的请求添加更多代码。单击按钮时:
Private Sub OpenFile_Click()
Form_FilingProcess.Subform_cmdOpenFile_Click Me
End Sub
Which calls:
其中调用:
Public Sub Subform_cmdOpenFile_Click(frm As Form)
Set rs = frm.RecordsetClone
rs.Bookmark = frm.Bookmark
And then it goes on to open the file.
然后它继续打开文件。
回答by Fionnuala
If the button is for each record, there is no need for any messing around with the recordset. You can use the name of the control to get the file:
如果按钮是针对每条记录的,则无需对记录集进行任何处理。您可以使用控件的名称来获取文件:
TheFile=Me.MyControl
It seems that you have both a form and subform. I am guessing from your answers that the set-up is something like this:
似乎您同时拥有表单和子表单。我从你的回答中猜测设置是这样的:
|------------------------------|
| Main Form |
--------------------------------
Sub form
--------------------------------
Row Button
--------------------------------
Row Button
--------------------------------
If the name of the button is OpenFile, try:
如果按钮的名称是 OpenFile,请尝试:
Private Sub OpenFile_Click()
MsgBox Me.NameOfAContolHere & ""
'Form_FilingProcess.Subform_cmdOpenFile_Click Me
End Sub
This can then be used to ope a file like so:
然后可以使用它来操作文件,如下所示:
Private Sub OpenFile_Click()
FollowHyperlink Me.NameOfControlWithPathToFile
'Form_FilingProcess.Subform_cmdOpenFile_Click Me
End Sub
回答by JeffO
Here is what your sub should look like:
这是您的子程序的外观:
Public Sub Subform_cmdOpenFile_Click(frm As Form)
Dim CurrentBookmark as String
Set rs = frm.RecordsetClone
CurrentBookmark = frm.Bookmark
rs.Bookmark = CurrentBookmark
End Sub
Set a string variable to the value of the form's bookmark. Then set the recordset's bookmark to the string variable value.
将字符串变量设置为表单书签的值。然后将记录集的书签设置为字符串变量值。
Dim rs As Recordset
dim CurrentBookmark as String
Set rs = Me.RecordsetClone
CurrentBookmark = Me.Bookmark
rs.Bookmark = CurrentBookmark
http://msdn.microsoft.com/en-us/library/aa223967(office.11).aspx
http://msdn.microsoft.com/en-us/library/aa223967(office.11).aspx