Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Limit file sizes for images

Status
Not open for further replies.

tedmillerx

Programmer
Jan 6, 2001
52
US
I'm using the Northwind method of adding pictures to my database. Problem is that navigating gets really unstable when the user tries to add a jpg image that's more than 600k, even though I'm linking (not embedding).

It really doesn't need jpgs larger than about 200k to do the job. Is there a way to limit images to 200k or less? Ideally, when the user selects a file too large, a message box would pop up saying, "Please use images smaller than 200k."

Here's the function for the Add Image button I designed, in case that helps:

Sub getFileName()
' Displays the Office File Open dialog to choose a file name
' for the current record. If the user selects a file
' display it in the image control.
Dim fileName As String
Dim result As Integer
With Application.FileDialog(msoFileDialogFilePicker)
.Title = "Select Picture"
.Filters.Add "All Files", "*.*"
.Filters.Add "Bitmaps", "*.bmp"
.Filters.Add "JPEGs", "*.jpg"
.Filters.Add "TIFFs", "*.tif"
.FilterIndex = 3
.AllowMultiSelect = False
.InitialFileName = CurrentProject.Path
result = .Show
If (result <> 0) Then
fileName = Trim(.SelectedItems.Item(1))
Me![ImagePath].Visible = True
Me![ImagePath].SetFocus
Me![ImagePath].Text = fileName
Me![ImagePath].SetFocus
Me![ImagePath].Visible = True
End If
End With
End Sub
 
You can use the FileSystemObject to check file size.

Code:
Dim fs, f, filespec

filespec = CurrentProject.Path & "\test.bmp"

Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFile(filespec)
Debug.Print f.Size
 
Or simpler:
If FileLen(fileName) > 200000 Then
MsgBox "Please use images smaller than 200k."
Exit Sub
End If

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
thanks, guys! I really like the sound of PHV's answer. I'll give it a try.

by the way, has anybody else experience this problem with large graphics? I'd rather not hassle the user with this issue, but the amount of crashes have been ridiculous. often times the image load bar will just freeze up.

is there a more elegant solution?
 
Or simpler:
If FileLen(fileName) > 200000 Then
MsgBox "Please use images smaller than 200k."
Exit Sub
End If

Hope This Helps, PH."

I tried using this solution, but struggled to make it fit in the above code I showed. Could you tell me where to insert it?

Thanks
 
should be like:
Code:
Sub getFileName()
    ' Displays the Office File Open dialog to choose a file name
    ' for the current record.  If the user selects a file
    ' display it in the image control.
    Dim fileName As String
    Dim result As Integer
    With Application.FileDialog(msoFileDialogFilePicker)
        .Title = "Select Picture"
        .Filters.Add "All Files", "*.*"
        .Filters.Add "Bitmaps", "*.bmp"
        .Filters.Add "JPEGs", "*.jpg"
        .Filters.Add "TIFFs", "*.tif"
        .FilterIndex = 3
        .AllowMultiSelect = False
        .InitialFileName = CurrentProject.Path
        result = .Show
        If (result <> 0) Then
            fileName = Trim(.SelectedItems.Item(1))
            [b]if FileLen(fileName) > 200000 then
              MsgBox"Please user images smaller than 200k."
              Exit Sub
            else
              Me![ImagePath].Visible = True
              Me![ImagePath].SetFocus
              Me![ImagePath].Text = fileName
              Me![ImagePath].SetFocus
              Me![ImagePath].Visible = True
            End If[/b]
        End If
    End With
End Sub

Leslie

Anything worth doing is a lot more difficult than it's worth - Unknown Induhvidual

Essential reading for anyone working with databases:
The Fundamentals of Relational Database Design
Understanding SQL Joi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top