我在MS Access主窗体上有一个标签,需要显示导出文件的保存位置。我有一个[编辑]按钮,单击该按钮会弹出一个文件对话框,并允许用户选择要导出到的文件夹。选择文件夹后,标签标题将更改为用户选择的文件夹的位置。这非常好用。我唯一的问题是,当关闭数据库并重新打开数据库时,标签标题会恢复为原始标题(在这种情况下,可以说只是测试)。我想要这样,以便在更改标签标题时,除非用户单击[编辑]按钮并再次更改位置,否则它将保持这种状态。下面是我正在使用的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
答案 0 :(得分:0)
尽管最好的方法是将值存储在某个表中,但是您可以将以前的值保存在自定义表单属性中。 首先创建一个表单属性(在立即窗口中):
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