2016-05-23 22 views
0

所以,愚蠢的問題,但我無法弄清楚。我有以下代碼搜索文件路徑名稱,我相信將該記錄添加到表(未經測試)。但是,問題是我無法調用這個子程序。我希望能夠點擊表單上的按鈕並運行。有誰知道我是如何做到這一點的?謝謝!如何從訪問表單上的按鈕單擊調用此子例程?

Public Function SelectFile() As String 

Dim f As FileDialog 
Set f = Application.FileDialog(msoFileDialogOpen) 

With f 
    .AllowMultiSelect = False 
    .Title = "Please select file to attach" 
    If .Show = True Then 
     SelectFile = .SelectedItems(1) 
    Else 
     Exit Function 
    End If 
End With 

Set f = Nothing 

End Function 


Public Sub AddAttachment(ByRef rstCurrent As DAO.Recordset, ByVal strFieldName As String, ByVal strFilePath As String) 

Dim dbs As DAO.Database 
Dim rst As DAO.Recordset 

'Ask the user for the file 
Dim filepath As String 
filepath = SelectFile() 

'Check that the user selected something 
If Len(filepath) = 0 Then 
    Debug.Assert "No file selected!" 
    Exit Sub 
End If 

Set dbs = CurrentDb 
Set rst = dbs.OpenRecordset("Table1") 

''''change this 
'Add a new row and an attachment 

rst.AddNew 
AddAttachment rst, "Files", filepath 
rst.Update 

'Close the recordset 
rst.Close 
Set rst = Nothing 
Set dbs = Nothing 

End Sub 

回答

0

您將事件過程添加到按鈕的問題:

  1. 在窗體設計模式,單擊按鈕
  2. 在屬性表,選擇建設者[...] On Click事件的按鈕
  3. 您將轉到VBA編輯器。輸入如下代碼:

    Private Sub cmdAddAttachment_Click() AddAttachment Nothing, "", "" End Sub

這就是說,你的日常AddAttachment有一個明顯的無限循環。該行:

AddAttachment rst, "Files", filepath 

似乎並沒有實際填充任何字段值。實際上,代碼中未使用變量rstCurrentstrFieldNamestrFilePath。您可能需要先調試此例程才能使用。

相關問題