Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 使用文件选择器将图像文件从一个位置复制到另一个位置_Vba_Ms Access - Fatal编程技术网

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

Vba 使用文件选择器将图像文件从一个位置复制到另一个位置,vba,ms-access,Vba,Ms Access,我在表上有一个名为itemage的图像控件设置为itemage字段。 我使用文件选择器浏览到一个位置并复制图像,然后将其保存到另一个位置,并使用文本框值重命名图像,然后将其完整位置添加到表中 问题: access上的图像控件是否与.jpg文件一起工作?或者我需要转换为.bmp?如果是这样,当我将文件从一个位置复制到另一个位置时,如何维护文件扩展名? 有没有更好或更有效的方法来完成这类任务 我当前尝试复制图像,但未在表单上显示已知的.bmp图像。 请参阅下文: Private Sub itemIm

我在表上有一个名为
itemage
的图像控件设置为
itemage
字段。 我使用
文件选择器
浏览到一个位置并复制图像,然后将其保存到另一个位置,并使用文本框值重命名图像,然后将其完整位置添加到表中

问题:

access上的
图像控件
是否与
.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

据我所知,实际的问题是jpeg文件扩展名,而不是jpg。Access未在图像控件中渲染jpeg图像。我还注意到,只需将文件扩展名从jpeg更改为jpg,即可访问渲染文件。因此,请考虑:

strExt = Mid(ofd.SelectedItems(1), InStrRev(ofd.SelectedItems(1), "."))
If strExt = ".jpeg" Then strExt = ".jpg"
或者不必使用strExt和If-Then条件,只需使用Replace()函数即可。另外,使用FileCopy类似于:

FileCopy filePath,images\u path&Replace(fileName,.jpeg,.jpg”)

图像控件当然可以使用jpg。不能简单地更改图像文件名的文件扩展名并期望图像可读。通过从原始名称中提取并设置一个变量,然后将该变量连接到新文件名来保持相同的文件扩展名。CopyImage子文件可能更简单。与CopyFile不同,FileCopy不需要FSO对象。不打开记录集对象,只需运行更新操作SQL。@June7扩展提取部分就是我遇到的问题。当然,更新操作比使用记录集要好。我已经更新了代码并完全删除了CopyImage子文件。请参阅更新的代码。实际上你有什么问题?您已经了解了字符串操作函数。只需使用它们来提取扩展。因此,代替
&.bmp“
,例如:
&Mid(ofd.SelectedItems(1),instrev(ofd.SelectedItems(1),”)
。您允许在FileDialog中选择多个项目,但仅当count=1时才运行copy。是否只允许一个选择?如果使用FileCopy,为什么仍然设置fso对象变量?