在MS Access中,我有一个表“文件”与i.a.字段“title”和“fileLocation” 我想要的是一个打开所选记录所引用文件的按钮 换句话说:
当然,我也可以将整个路径放在“fileLocation”中,但驱动器号会不时变化......
按钮后面的VBA代码应该是这样的:
Sub buttonClickHandler()
Dim fLocation As String
fLocation = files."current selected record".fileLocation
If (Dir("D\fileLocation") <> "")
Then (Application.FollowHyperlink "D\fileLocation")
Elseif (Dir("E\fileLocation") <> "")
Then (Application.FollowHyperlink "E\fileLocation")
...
Else (MsgBox "File not found")
End Sub
我在这里找到了类似的问题:
How do I access the selected rows in Access?
但是我还没有找到如何将这些代码用于表或查询而不是表单
非常感谢提前
答案 0 :(得分:0)
来自Erik von Asmuth的评论和How do I access the selected rows in Access?的回答确实完成了这项工作。
Dim previousSelection As Integer
Private Sub Form_Open(Cancel As Integer)
previousSelection = 0
End Sub
Private Sub Form_Timer()
If Me.SelHeight <> 1 Or Me.SelTop = previousSelection Then Exit Sub
previousSelection = Me.SelTop
With Me.RecordsetClone
.MoveFirst
.Move Me.SelTop - 1
openFile ("path/to/file")
End With
End Sub
Sub openFile(fileName As String)
If (Dir("D:\" & fileName) <> "") Then
Application.FollowHyperlink ("D:\" & fileName)
ElseIf (Dir("E:\" & fileName) <> "") Then
Application.FollowHyperlink ("E:\" & fileName)
Else
MsgBox ("File not found")
End If
End Sub
每次更改所选行时,都会打开相关文件。