External Images Referencing in ACCDB - image

I have a database application making use of VBA code that has reached its 2GB size limitation with image attachments on a per-record basis. We're wanting to have all forms in the application move away from "uploading" images as attachments for records, and instead upload them to a directory on a network server and reference the image files in the forms.
At this point, I'm trying to figure out the best way to approach this. Is it feasible to have the user somehow pull up an "Upload" dialog through an Access form? I've tried a couple cobbled-together solutions for Open File dialogs in VBA, but they've never quite worked properly.
Summary: File type is ".ACCDB", I need to allow users to upload images in a New Record creation form, have the images stored in a Network directory, and accessed on-the-fly throughout the application.
Thanks in advance.

From a general database point of view, you should not store images directly in a database, unless they are very small (less than 1 MB approximately), and you actually have the space to store them. The most common practice is to store the image path in the database, and then load those images directly from your the file directory where the images are stored when needed. It will add another level of complexity, but will reduce your DBs size radically.
Update:
This code allows the user to click some element frame in the form, store the path in the DB (with path as the username.jpg), and display the picture to the user afterwards in the clicked frame:
Private Sub SignatureFrame_Click()
On Error GoTo ErrorHandler
' Get path for the new picture from a dialog box
Dim Path As String
Dim fd As FileDialog
Dim ffs As FileDialogFilters
Set fd = Application.FileDialog(msoFileDialogOpen)
With fd
Set ffs = .Filters
With ffs
.Clear
.Add "Pictures", "*.jpg"
End With
.AllowMultiSelect = False
If .Show = False Then Exit Sub
Path = .SelectedItems(1)
End With
' Update the picture in the user interface
Me![SignatureFrame_Click].Picture = Path
' Copy the signature into the local Signature folder of the DB
Dim fs As Object
Dim oldPath As String, newPath As String, file As String
oldPath = Path 'Full path the file is located in
newPath = CurrentProject.Path & "\Signatures\Users\" & Screen.ActiveForm.UserName & ".jpg" 'Folder and path to copy file to
Set fs = CreateObject("Scripting.FileSystemObject")
fs.CopyFile oldPath, newPath 'This file was an .jpg file
Set fs = Nothing
' Set the new picture path for the form
Screen.ActiveForm.SignaturePath = newPath
Exit Sub
ErrorHandler:
MsgBox "Could not upload the image. Please check that the file format is of type jpg."
Resume Next
End Sub
Then at some later point in some other form you can retrieve the image like this:
Me!SignatureFrame.Picture = CurrentProject.Path & "\Signatures\username.jpg"
PS: The code has been translated in the SO editor, so I cannot guarantee that there are no mistakes.
Hope that helps!

Related

How to clear an Image in a Userform?

I have a Userform in which I use an Image control. I display a picture in this control, based on two criteria.
I don't know how to clear this Image in the Userform when I want to type two new criteria.
You can easily 'clear' your UserForm Image control (e.g. Image1) via Image1.Picture = LoadPicture(vbNullString). In the example below you would switch between showing a defined picture and displaying the empty control when clicking your command button. In order to control the Image status (empty or not) you check the image picture property via If Image1.Picture Is Nothing Then...
Code example in userform module (Excel)
Option Explicit
Private Sub CommandButton1_Click()
' Purpose: Change view between given image and no image
Dim sImgName As String ' picture name string
sImgName = "C:\Temp\MyPicture.gif"" ' <<< choose your picture name"
With Me.Image1
If .Picture Is Nothing Then ' picture property has been cleared already
.Picture = LoadPicture(sImgName)
Else ' a picture is already displayed
.Picture = LoadPicture(vbNullString) ' clear it now
End If
End With
End Sub
Hint to use in MS Access
Note the different use in MS Access repeating #DrMarbuse 's comment as late edit:
In Access 2010, the picture is directly set by the image path. So LoadPicture() shall not be used anymore (In Excel it is required). Everything stayed the same and worked like a charm.

How do I trap an error when OneDrive image renders file.OpenAsync error

In a project I try to give users the option to select images from their personal libraries. Nothing special as such.
This goes perfectly when the user selects the desired image from a local imagelibrary. But when images are selected from OneDrive, an error occurs, and I'm not able to trap the error. The source of the error seems to be in the file.OpenAsync method.
It seems like there may be a relation with the size of the selected image, but I cannot tell, as i'm not able to catch the error.
Here's the codesnippet (it's taken from the XAML image SDK samples in fact)
Dim picker As New FileOpenPicker With {.ViewMode = PickerViewMode.Thumbnail, .SuggestedStartLocation = PickerLocationId.PicturesLibrary}
'picker.SuggestedStartLocation = PickerLocationId.PicturesLibrary
picker.FileTypeFilter.Add(".png")
picker.FileTypeFilter.Add(".jpeg")
picker.FileTypeFilter.Add(".jpg")
picker.FileTypeFilter.Add(".bmp")
Dim wBitMap as new WritableBitmap(200,200)
Dim file As StorageFile = Await picker.PickSingleFileAsync()
' Ensure a file was selected
If file IsNot Nothing Then
Try ' Set the source of the WriteableBitmap to the image stream
Using fileStream As IRandomAccessStream = Await file.OpenAsync(Windows.Storage.FileAccessMode.Read)
Await wBitmap.SetSourceAsync(fileStream)
wBitmap.Invalidate()
vwImage.Source = wBitmap
End Using
Catch e1 As TaskCanceledException
' The async action to set the WriteableBitmap's source may be canceled if the user clicks the button repeatedly
End Try
End If
As you can see, there is a try-catch in place, but nonetheless a non-trapped error pops up, and stepping through, I can detect that it happens in the line
Await file.OpenAsync(Windows.Storage.FileAccessMode.Read)
Now my question is: how do I catch this error? Am I right that the line above is the source of the error? And if not so: what is and how to overcome?
After a lot of experimenting, I think I found the solutoion.
When a StorgeFile is selected to be used as the source for an image, it is wise to check the StorageFile.Attributes.
Files that are selected will carry an Attributes value. In the case of a file from OneDrive, the Attributes value was 544 (the value that you get when combining Archive and LocallyIncomplete. So that file has to be downloaded first, and then it will be available to use.
This is explained here:
Link to the MSDN documentation on StorageFile.Attributes

VB6 Common Dialog Control File Open does not give correct folder on some Windows 7 PCs

My VB6 application uses the Common Dialog Control for File Open etc.
The code is working OK on hundreds of PCs.
But I have one customer for which the File Open command displays the wrong folder.
He's running Windows 7.
Here's an excerpt of my code:
Private Sub mnuFileItem_Click(Index As Integer)
CommonDialog1.FileName = "" 'workaround for problem with .InitDir (20051010)
CommonDialog1.InitDir = strDataPath
' Set filters
CommonDialog1.Filter = ApplicationName & " Files (*" & PreProcessorFileExtension & ")|*" & PreProcessorFileExtension
' Set dialog box so error occurs if the dialog box is cancelled '20020418
CommonDialog1.CancelError = True
Select Case Index ' Check index value of selected menu item.
Case 0 ' If index = 0, the user chose "New"
'code omitted here
Case 1 ' If index = 1, the user chose "Open..."
' display the File Open dialog
CommonDialog1.Action = 1
FileName = CommonDialog1.FileName
OpenCLSFile
Case 2 To 3 'If index = 2, "Save..." = 3, "Save As..."
'code omitted here
End Select
End Sub
Typically the variable used to define CommonDialog1.InitDir, strDataPath, is set to:
C:\Users\Public\Documents\Mincad Systems\CIRCLY5\data
But for the user that has the problem, the File Open dialog displays:
C:\Users\Public\Documents\Mincad Systems\CIRCLY6\data
I have logs from the user that confirm that CommonDialog1.InitDir =
C:\Users\Public\Documents\Mincad Systems\CIRCLY5\data
before every statement in Sub mnuFileItem_Click.
The user is also using a vb.net version of my application.
In that application the File Open dialog uses the folder:
C:\Users\Public\Documents\Mincad Systems\CIRCLY6\data
Is there some way that the vb.net application resets some property of the Common Dialog Control properties used by the VB6 application?
I have tried replacing the VB6 Common Dialog File Open by using a Win32 API version - but my user still has the same problem.
Regards,
Leigh
I know this post is old, but maybe this will help someone. What I found in my case is that if the directory you are setting Initdir to does not exist on that PC, then the current directory is used.

MS Access VBA: how do I clear Image control?

I have an Image control that reads a file from disk as the user navigates. If there is no file (an empty field), I need to erase the previous image. The microsoft page says to do as follows:
Sub Form_Click ()
picture1.picture = LoadPicture()
End Sub
But when I do it, Access complains that "the argument is not optional"! I also tried LoadPicture("") as stated here and here, to no avail. It says "Microsoft Access can't open the file '0'." How can I clear the picture in runtime? I'm using Access 2002. Thanks in advance!
Use the following:
picture1.picture = ""

Save images from Access database to dropbox folder

I have a MS Access 2010 application with an item table that stores the image of the item. How can I save this image in a Dropbox folder?
Exemple
Table: ItemTable
Fields: ID = 0001, DESCRIPTION = Item1, CATEGORY = C1, CUSTOMER = Cust1,
FOLDER = CATEGORY & "\" & CUSTOMER, IMAGE = Item1.jpg
I need to save the image Item1.jpg in a folder C1, subfolder Cust1, into dropbox. The data type for the IMAGE field could be a string data (the image file is located in a folder on the hard drive of the local pc) or could be a OLE object, doesn't matter.
You can use FileCopy in VBA, for example:
FileCopy "C:\Images\Img1.jpg", "Z:\Users\Raul\Dropbox\Public\SavedImg1.jpg"

Resources