Make max size limit for OLE object (1 Viewer)

cage4000

Registered User.
Local time
Yesterday, 21:46
Joined
Oct 14, 2015
Messages
49
Hi Everyone,

I need to make it so that a user can input photos into a OLE object field on one of my forms but i want to make sure they do not put too large of a file in it. is there a way to block files that i deem are to large (example anything larger then 1MB) in the access form that will stop the user from adding the file to the OLE object?

on a side note, I do know it is a lot better to link the photos in a folder on your desktop and pull the link into access that way. This approach is not conducive of what i'm trying to accomplish. I only state this because I'm looking for ways to solve this issue and not a way around it.
 

Ranman256

Well-known member
Local time
Today, 00:46
Joined
Apr 9, 2015
Messages
4,339
this code is on a form ,user clicks a button to pick the file

Code:
dim vFile

vFile = UserPick1File()
if vFile <> "" then
  If FileLen(vFile) < 1000000 Then        'store file in ole field
      Me.Field = LoadPicture(vFile)
  else
      msgbox "File too big"
  End If
endif


put this code into a MODULE for user to pick the file

Code:
Public Function UserPick1File(Optional pvPath)
Dim strTable As String
Dim strFilePath As String
Dim sDialog As String, sDecr  As String, sExt As String


'===================
'YOU MUST ADD REFERENCE : Microsoft Office xx.0 Object Library, in vbe menu, TOOLS, REFERENCES
'===================

With Application.FileDialog(msoFileDialogFilePicker)     'msoFileDialogSaveAs
    .AllowMultiSelect = False
    .Title = "Locate a file to Import"
    .ButtonName = "Import"
    .Filters.Clear
     '.Filters.Add "CSV Files", "*.csv"
     '.Filters.Add "Excel Files", "*.xls;*.xlsx"
    .Filters.Add "All Files", "*.*"
    .InitialFileName = pvPath
    .InitialView = msoFileDialogViewList    'msoFileDialogViewThumbnail
    
        If .Show = 0 Then
           'There is a problem
           Exit Function
        End If
    
    'Save the first file selected
    UserPick1File = Trim(.SelectedItems(1))
End With
End Function
 

cage4000

Registered User.
Local time
Yesterday, 21:46
Joined
Oct 14, 2015
Messages
49
This is exactly what i'm looking for. the only problem is that I run into an error when i run the code:

Run-Time error: 13
type Mismatch

and the code stops on:
Code:
    .InitialFileName = pvPath

if it helps the OLE object in access is connected to a datatype "Image" in the SQL server backend.
 

Galaxiom

Super Moderator
Staff member
Local time
Today, 14:46
Joined
Jan 20, 2009
Messages
12,851
Code:
Public Function UserPick1File(Optional pvPath As String)

Without the type declaration it is passing a Variant which, when not supplied, is a Null.

InitialFileName is a String so you get a mismatch because it can't be Null.
 

cage4000

Registered User.
Local time
Yesterday, 21:46
Joined
Oct 14, 2015
Messages
49
This fixed it. it works great on the import and blocking now.

Now what i'm having trouble with is that it is not saving it as a bitmap image so it will not show up on the form now. the file i'm saving is already a bitmap image so i just need the import to recognize it. is there any suggestions on the code that will accomplish this?
 

Users who are viewing this thread

Top Bottom