我在嘗試使用 MS Access 2016 中的 FileDialog 屬性顯示檔案選擇器時遇到問題。我嘗試了早期和后期系結,但檔案選擇器從未顯示。沒有檢測到錯誤,也沒有顯示訊息。當我嘗試逐行除錯時,.show 不會觸發表單。我也試過If .show=-1代替,.show但這也不起作用。我已經嘗試洗掉對 Microsoft Office 16.0 物件庫的庫參考,即使使用后期系結視窗仍然不顯示。關于出了什么問題以及如何補救的任何想法?我在下面添加了早期和晚期系結示例。
Public Sub FP_EarlyBinding()
Dim fd As Office.FileDialog
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
.AllowMultiSelect = True
.Show
For Each vrtSelectedItem In .SelectedItems
Debug.Print vrtSelectedItem
Next vrtSelectedItem
End With
End Sub
Public Sub FP_LateBinding()
Const msoFileDialogFilePicker As Long = 3
Dim fd As Object
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
.AllowMultiSelect = True
.Show
For Each vrtSelectedItem In .SelectedItems
Debug.Print vrtSelectedItem
Next vrtSelectedItem
End With
End Sub
---------------EDIT-------------------------
根據評論,我被指示不要這樣做這在一個類模塊中。我已經編輯了代碼,如下所示。這仍然不允許表單出現。
這是在標準模塊中:
Public Function filePicker() As Variant
Dim fd As FileDialog
Dim sFiles$
Dim vrtSelectedItem As Variant
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
.AllowMultiSelect = True
.Show
For Each vrtSelectedItem In .SelectedItems
sFiles = sFiles & vrtSelectedItem & ","
Next vrtSelectedItem '-----------loops again to attach other files
End With
filePicker = sFiles
End Function
我從一個類模塊呼叫這個程序:
Public Sub Test()
Dim vFileList As Variant, vFile As Variant
vFileList = Split(filePicker(), ",")
For Each vFile In vFileList
Attachments.Add vFile
Next vFile
End Sub
-------------最終編輯--------------------
原來這里的問題是 Access 安裝...我去了到安裝目錄,找到MSACCESS.EXE,右鍵,修復。
uj5u.com熱心網友回復:
你所擁有的看起來幾乎不錯。
我建議您始終,但始終始終將選項顯式放置在代碼模塊的開頭。
例如這個:
Option Compare Database
Option Explicit
如上所述,您的代碼無法編譯。我的意思是,在您輸入代碼后,我假設您從代碼選單中進行了除錯-> 編譯。我會在一個典型的一天(在編輯代碼之后)做 100 次。所以,養成這個習慣。
此外,要為新代碼模塊設定默認值(設定不會影響現有),請在 VBA 代碼編輯器中設定此選項:工具->選項

所以,你的代碼片段看起來沒問題,但是,如果上面的選項顯式,那么你必須宣告所有變數(并且 compile 會產生錯誤的拼寫或錯過輸入的變數名)。
所以,你這樣的代碼應該可以正常作業:
Public Sub FP_LateBinding()
Const msoFileDialogFilePicker As Long = 3
Dim fd As Object
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
.AllowMultiSelect = True
.Show
Dim vrtSelectedItem As Variant
For Each vrtSelectedItem In .SelectedItems
Debug.Print vrtSelectedItem
Next vrtSelectedItem
End With
End Sub
現在,還不清楚您如何測驗/運行上述內容。但是,我們假設您創建了一個新的代碼模塊(不是類模塊)
然后,您將在上面鍵入/粘貼。保存
從 VBA 編輯器選單中進行除錯 -> 編譯 - 編譯正常嗎?(如果其他地方有其他錯誤 - 首先修復那些錯誤)。
現在,要運行它,將游標放在該代碼存根內的任何位置,按 f5。
或者您可以在除錯視窗中輸入上述子程式的名稱,如下所示:
FP_LateBinding
所以你發布的代碼 - 看起來真的很好!- 都好。試試上面的選項顯式。當然,現在在您的示例中,為 vrtSelectedItem 添加宣告
編輯:================================================ ============
Now, of course if this is a class module, then just like code in a form/report, you can't just hit f5 in the code module, nor can you JUST type in the name of the sub.
In fact, if you place your cursor in the code and hit F5, then you get this:

So, it not that the code or file does not appear, you get the above - a big WHOPPER of a difference issue.
And if it is a class module as opposed to a regular code module?
Then to test or use the code, you have to write in test code into a REGULAR code module. You can't use the debug/VBA editor to JUST run a class.
This not different then creating any other object.
So, if we create a new class module, paste in above code, say we save the class code module as MyClassTest ?
Then you have to write code in another standard code module like this:
Sub Test1()
Dim clsTest As New MyClassTest
clsTest.FP_LateBinding
End Sub
So, you can't run class code (even code in a forms code behind module), or any custom class module, you have to FIRST create a instance.
Now it is "possbile" that you used VBA, VB5, VB6. And in fact used "early" versions of Access (before access 2000 - 21 years ago).
you will find by default, it is possbile to use call class module code without first creating a instance of the class module. This ability was and is for keeping compatibility for pre access 2000 VBA, in which class code modules did not require to be delcared as a new instance. This so called "base" class instance thus was possbile.
it turns out, that if you been importing code for the last 20 years from previous verisons of Access? This flag setting - and option STILL exists in Access 2019 - over 20 years later!!!!
I doubt that you are/did import code from such older versions of Access, but it is in fact still possible to set a class module to NOT require a instance of the class to be created in code. However, I don't recommend doing this, despite the fact that this is still possible.
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/345035.html
上一篇:NGINX兩個目錄一個URL(Codeigniter,HTML)
下一篇:迭代地將矩陣添加到更大的矩陣中
