使用文件选择器将图像文件从一个位置复制到另一位置

时间:2020-10-02 16:14:26

标签: vba ms-access

我在表上将一个名为itemImage的图像控件设置为ItemImage字段。 我正在使用file picker浏览到某个位置并复制图像,然后将其保存到其他位置,并使用文本框值对其进行重命名,然后将其完整位置添加到表中。

问题:

image control上的访问权限可以与.jpg文件一起使用吗?还是我需要转换为.bmp?如果是这样,当我将文件从一个位置复制到另一个位置时,如何保持文件扩展名? 有没有更好或更有效的方式来完成此类任务?

我当前的尝试是复制图像,但是在表单上不显示已知的.bmp图像。 请看下面:

Private Sub itemImage_DblClick(Cancel As Integer)
On Error GoTo 0
Dim ofd As Object
Dim fso As Object
Dim theFile As String
Dim theFileLocation As String
Dim filePath As String
Dim fullFileName As String
Dim theFileName As String

Set ofd = Application.FileDialog(3)
ofd.AllowMultiSelect = False
ofd.Show
If ofd.SelectedItems.Count = 1 Then
theFile = Mid(ofd.SelectedItems(1), InStrRev(ofd.SelectedItems(1), "\") + 1, Len(ofd.SelectedItems(1)))
Set fso = VBA.CreateObject("Scripting.FileSystemObject")
filePath = ofd.SelectedItems(1)

CopyImage filePath, Me.donationNumber & Mid(ofd.SelectedItems(1), InStrRev(ofd.SelectedItems(1), "."))

CurrentDb.Execute "UPDATE tblDonatedItems SET ItemImage =  '" & Application.CurrentProject.Path + "\ItemImages\" + donationNumber.Value + Mid(ofd.SelectedItems(1), InStrRev(ofd.SelectedItems(1), ".")) & "' WHERE DonationNumber = '" & Me.donationNumber.Value & "'", dbFailOnError

Else
   MsgBox "Image update Cancel!"
End If

End Sub

Sub CopyImage(filePath As String, fileName As String)
Dim fs As Object
Dim images_path As String
images_path = CurrentProject.Path & "\ItemImages\"
Set fs = CreateObject("Scripting.FileSystemObject")
fs.CopyFile filePath, images_path & fileName
Set fs = Nothing

End Sub

1 个答案:

答案 0 :(得分:1)

据我了解,实际问题是jpeg文件扩展名,而不是jpg。访问未在“图像”控件中呈现jpeg图像。我还观察到,只要将文件扩展名从jpeg更改为jpg,Access即可渲染该文件。因此请考虑:

strExt = Mid(ofd.SelectedItems(1), InStrRev(ofd.SelectedItems(1), "."))
If strExt = ".jpeg" Then strExt = ".jpg"

或者不用理会strExt和If Then条件,只需使用Replace()函数即可。另外,使用FileCopy就像:
FileCopy filePath, images_path & Replace(fileName, ".jpeg", ".jpg")

相关问题