尝试在Access中重命名附件到表中的字段

时间:2017-10-11 18:27:48

标签: database vba ms-access records

目前,我有一张表格,其中包含ID'字段是通过多个其他字段的组合自动生成的。示例:ID =' 2017_FileName_Chancellor' 现在我一直试图找出一种方法让我将这个附件重命名为该ID(通过可能按下表单中的按钮),而无需手动复制粘贴ID并重命名附加文件,因为这样做时会很乏味它有成千上万的记录。反正我还能这样做吗?我对VBA一无所知,所以我无法篡改它。

提供我的表格图片可能有助于https://i.imgur.com/gaTUCtk.png

1 个答案:

答案 0 :(得分:0)

更改Access中附件的名称:

Private Sub Command15_Click()
    Dim NewName As String
    Dim NewNameWithExt As String

    NewName = Me.TestID.Value

    NewNameWithExt = NewName & ".txt"

    DoCmd.RunSQL ("UPDATE TestTable SET TestAttachment.FileName = '" & 
    NewNameWithExt & "' WHERE TestID = " & NewName)

End Sub

更改桌面上文件的名称:

Private Sub Command0_Click()
    Dim NewName As String
    Dim OldName As String
    Dim rs As Object
    Dim strSQL As String

    Set rs = CreateObject("ADODB.Recordset")

    strSQL = "SELECT TestAttachment.FileName FROM TestTable WHERE TestID = 1"

    rs.Open strSQL, CurrentProject.Connection, 1, 3

    Do Until rs.EOF
        OldName = rs.Fields(0)
        NewName = CurrentDb.TableDefs("TestTable").Fields(0).Name

        Name "C:\Users\TestUser\desktop\" & OldName As 
        "C:\Users\TestUser\desktop\" & NewName & ".TXT"

        rs.MoveNext
    Loop

    rs.Close
    Set rs = Nothing

End Sub