2017-12-11 1 views
0

공용 함수 및 쿼리가있는 MS Access 데이터베이스가 있습니다. 함수가 'Attachments'열의 모든 필드를 반복하여 그 필드의 모든 첨부 파일을 저장하고 싶습니다. "SEDOL"열과 해당 행 값을 파일 이름의 첫 번째 부분으로 저장해야하지만 아래 코드의 "Set rsA2 = fld2.Value"행에 도청이 계속됩니다. SEDOL 열은 일반적인 텍스트 필드 열입니다. 코드는 SEDOL 저장 이름 부분없이 작동합니다. 나는 그것이 작동하도록하는 방법에 대한 몇 가지 제안을 좋아한다. 감사합니다함수 및 쿼리를 사용하여 액세스에서 첨부 파일 저장

Public Function SaveAttachments(strPath As String, Optional strPattern As String = "*.*") As Long 

Dim dbs As DAO.Database 
Dim rst As DAO.Recordset2 
Dim rst2 As DAO.Recordset2 
Dim rsA As DAO.Recordset2 
Dim rsA2 As DAO.Recordset2 
Dim fld As DAO.Field2 
Dim fld2 As DAO.Field2 
Dim strFullPath As String 

'Get the database, recordset, and attachment field 
Set dbs = CurrentDb 
Set rst = dbs.OpenRecordset("Core Securities") 
Set fld = rst("Attachments") 
Set fld2 = rst("SEDOL") 
'Navigate through the table 
Do While Not rst.EOF 

'Get the recordset for the Attachments field 
Set rsA = fld.Value 
'BUGS IN NEXT LINE 
Set rsA2 = fld2.Value 
'Save all attachments in the field (works without rsA2) 
Do While Not rsA.EOF 
    If rsA("FileName") Like strPattern Then 
     strFullPath = strPath & "\" & rsA2("SEDOL") & " - " & rsA("FileName") 

    'Make sure the file does not exist and save 
    If Dir(strFullPath) = "" Then 
     rsA("FileData").SaveToFile strFullPath 
    End If 

    'Increment the number of files saved 
    SaveAttachments = SaveAttachments + 1 
    End If 

    'Next attachment 
    rsA.MoveNext 
Loop 
rsA.Close 

'Next record 
rst.MoveNext 
Loop 

rst.Close 
dbs.Close 

Set fld = Nothing 
Set rsA = Nothing 
Set rst = Nothing 
Set dbs = Nothing 

End Function 

답변

1

SEDOL 열이 단지 문자열이기 때문에이 값은의에, 당신이 레코드를 할당 할 수 없습니다. -이 완벽하게 작동

Public Function SaveAttachments(strPath As String, Optional strPattern As String = "*.*") As Long 
    Dim dbs As DAO.Database 
    Dim rst As DAO.Recordset2 
    Dim rsA As DAO.Recordset2 
    Dim strFullPath As String 

'Get the database, recordset, and attachment field 
    Set dbs = CurrentDb 
    Set rst = dbs.OpenRecordset("Core Securities") 

'Navigate through the table 
    Do While Not rst.EOF 
'Get the recordset for the Attachments field 
     Set rsA = rst("Attachments").Value 
'Save all attachments in the field (works without rsA2) 
     Do While Not rsA.EOF 
      If rsA("FileName") Like strPattern Then 
       strFullPath = strPath & "\" & rst("SEDOL").Value & " - " & rsA("FileName")  
'Make sure the file does not exist and save 
       If Dir(strFullPath) = "" Then 
        rsA("FileData").SaveToFile strFullPath 
       End If 
'Increment the number of files saved 
       SaveAttachments = SaveAttachments + 1 
      End If 
'Next attachment 
      rsA.MoveNext 
     Loop 
     rsA.Close 
'Next record 
     rst.MoveNext 
    Loop 

    rst.Close 
    dbs.Close 

    Set rsA = Nothing 
    Set rst = Nothing 
    Set dbs = Nothing 

End Function 

나는 또한 가능성 중 불필요하거나 적극적으로

+0

매우 감사 버그의 행동을 유발했다 수많은 이상한 일을 제거했습니다 : 그것은 가치의에

그냥 참조 – chandu

관련 문제