【发布时间】:2019-05-16 19:43:34
【问题描述】:
我的 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
【问题讨论】:
-
这需要在设计视图中修改表单。您需要做的是在表单打开时检索“最后一个”项目并设置控件。这意味着将数据保存在某处以便可以检索。
-
我真的不希望必须设置一个表来存储数据,是否可以将它存储在像标签一样的控件中?我以为我过去在另一个项目上做过类似的事情,但我似乎可以找到它。
-
没有。标签中的“保存”正在更改 Caption 属性,这需要设计视图并保存修改后的设计。