【发布时间】:2012-02-28 06:21:47
【问题描述】:
(这是在 Access 2003 中。) 我正在编写一个宏,它打开一个表单以允许用户输入他们希望导入的文件的路径和名称。然后它关闭表单,并导入指定文件中的数据。我有正确打开表单和用户输入的代码。一旦我获得路径和文件名,我也有执行数据导入/操作的代码。
我的问题是:表单关闭后,如何在宏/其他subs中使用表单中输入的信息?
这是我的表单代码的简单版本(“输入导入信息”):
Option Compare Database
Option Explicit
Sub RunButton_Click()
FilePathTextBox.SetFocus
DataPath = FilePathTextBox.Text
FileNameTextBox.SetFocus
DataName = FileNameTextBox.Text
DoCmd.Close
End Sub
这是我的宏尝试的简单版本(失败):
Option Compare Database
Option Explicit
Option Base 1
Public DataPath As String
Public DataName As String
Public CompleteName As String
Function StartImport()
'Open form to let user enter import file info
DoCmd.OpenForm "Enter Import Info", acNormal, , , , acDialog
'Get complete file path/name. I think this is where the problem is.
CompleteName = DataPath & "\" & DataName
'Import
ImportData
'Data clean up
DataCleanup
End Function
Function ImportData()
'Import CompleteName
'The import code here works fine when CompleteName has the right info
End Function
Sub DataCleanup()
'cleans up the data as needed (using DoCmd.RunSQL). Works fine.
End Sub
我花了 2 个多小时在 Internet 上寻找解决方案,但没有找到。一位朋友建议创建一个临时表,将表单中输入的值存储为字符串(这意味着该表将只有 1 个字段和 1 条记录)。但我不知道如何将其作为字符串从表中检索出来。
很抱歉,这篇文章很长 - 我想尽可能清楚。 提前感谢您的帮助!
==============编辑(附加信息)
表单中的原始代码要求用户手动输入路径和文件名。整个代码当时都在工作。
但是,我想通过使用 FileDialog 来确定路径和名称来避免错误/错误,因此我创建了在单击文本框时调用的 subs(FileNameTextBox_Click 和 FilePathTextBox_Click)。 FileNameTextBox_Click() 的代码可以在这里找到:FileDialog doesn't work
这有什么影响吗?
【问题讨论】:
-
我同意托尼的意见。这对我来说可以。您确定没有在其他地方声明 Datapath 和 Dataname 吗?顺便说一句,不要使用控件的文本属性,然后您不必设置焦点,它会在某些阶段导致悲伤。只需说
DataPath = Me.FilePathTextBox- 我对于避免名称混淆很重要。如果您必须有一个属性,请使用.value,即使控件没有焦点也可用。