我的 MS Access 主窗体上有一个标签,需要显示导出文件的保存位置。我有一个 [编辑] 按钮,单击该按钮会显示一个文件对话框,并允许用户选择要导出到的文件夹。选择文件夹后,标签标题将更改为用户选择的文件夹的位置。这非常有效。我唯一的问题是,当数据库关闭并重新打开时,标签标题会恢复到原始标题是什么(在这种情况下,可以说它只是说 TEST)。我想要它,以便在更改标签标题时保持这种状态,除非用户单击 [编辑] 按钮并再次更改位置。下面是我正在使用的 VBA 代码。
预先感谢您的帮助!
Sub SetFileLocation()
Dim Ret
strUserName = Environ("UserName")
strPath = "C:\documents and settings\" & strUserName & "\Desktop"
'~~> Specify your start folder here
Ret = BrowseForFolder(strPath)
Forms.frmmainform.lblFolderLocation.Caption = strFolderLocation
End Sub
Function BrowseForFolder(Optional OpenAt As Variant) As Variant
'Function purpose: To Browser for a user selected folder.
'If the "OpenAt" path is provided, open the browser at that directory
'NOTE: If invalid, it will open at the Desktop level
Dim ShellApp As Object
'Create a file browser window at the default folder
Set ShellApp = CreateObject("Shell.Application"). _
BrowseForFolder(0, "Please choose a folder", 0, OpenAt)
'Set the folder to that selected. (On error in case cancelled)
On Error Resume Next
BrowseForFolder = ShellApp.self.Path
On Error GoTo 0
Debug.Print BrowseForFolder
strFolderLocation = BrowseForFolder
Debug.Print strFolderLocation
'Destroy the Shell Application
Set ShellApp = Nothing
'Check for invalid or non-entries and send to the Invalid error
'handler if found
'Valid selections can begin L: (where L is a letter) or
'\\ (as in \\servername\sharename. All others are invalid
Select Case Mid(BrowseForFolder, 2, 1)
Case Is = ":"
If Left(BrowseForFolder, 1) = ":" Then GoTo Invalid
Case Is = "\"
If Not Left(BrowseForFolder, 1) = "\" Then GoTo Invalid
Case Else
GoTo Invalid
End Select
Exit Function
Invalid:
'If it was determined that the selection was invalid, set to False
BrowseForFolder = False
End Function
虽然最好的方法是在某个表中存储值,但您可以在自定义表单属性中保存以前的值。首先创建一个表单属性(在即时窗口中):
CurrentProject.AllForms ("Your form name").Properties.Add "LastFolder", ""
然后像这样将其保存在您的子文件中
...
Me.lblFolderLocation.Caption = strFolderLocation
CurrentProject.AllForms("Your form name").Properties("LastFolder").Value = strFolderLocation
然后恢复 Load 事件中的最后一个值:
Private Sub Form_Load()
Me.lblFolderLocation.Caption = CurrentProject.AllForms("Your form name").Properties("LastFolder")
End Sub
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句