Link to home
Start Free TrialLog in
Avatar of teelamaria
teelamaria

asked on

Load IMage into record based on directory path

Hello...
I have a Property Sales database with a form that displays information for a Property Sale. I would like to add a property image to each respective record. Currently, I have a folder set up on my server with a folder contiang the property images. The table where my property record form (called "Sales Database") pulls its data from is called "Table1". In Table1, I have a column called "Image" where I have pasted the hyperlinks to each corresponding image to its record. Currently in my form, a user can just click on the hyper link and a local explorer window opens and displays the image (this occurs after Access gives the user a warning message about hyperlinks being harmful and they then have to click open anyway - annoying!).

Is there a way i can have the image quickly load up in a box in my form so it can be viewed in the form? Average image is under 100K, although some run a bit higher. I have about 600 records currently.

Thank you for your time. I am a complete rookie at Access, so please make all answers very clear and mickey-mouse :)
Avatar of Leigh Purvis
Leigh Purvis
Flag of United Kingdom of Great Britain and Northern Ireland image

:-O  As if we do Micky Mouse here??
:-)
If you add an image control to your form then you can assign it's Picture property at runtime which will present the image as required based on it's path (it's the full path that you specify in the Picture property of that Image control).
So something like an Event Procedure in your form's Current event (as shown in the code below).

Alternatively - if you're using Access 2007 then you can use the bound nature of the control and calculate your path in a query
e.g.
SELECT *, IIF(YourPathField Is Null, Null, Left(YourPathField, Instr(YourPathField, "#") - 1)) As PicPath FROM TableName

Then in your form control - you'd make the Image control's controlsource to be PicPath.
This would have the joint advantage of saving your using the code solution below - and also allow that you show the rows in a continuous form and see a different (appropriate) image for each row.
Feel free to post back if that's not desperately clear.  (But it may be Micky Mouse ;-)

Private Sub Form_Current()
 
    If IsNull(Me.YourPathField) Then
        Me.ImageControlName.Picture = ""
    Else
        Me.ImageControlName.Picture = Split(Me.YourPathField,"#")(0)
    End If
 
End Sub

Open in new window

Avatar of teelamaria
teelamaria

ASKER

I am running Access 203, but thank you for that extra info...

So I drew a bound object from the toolbox onto the form and put my datasource as "Image" (name of column in Table1). Objects name is ImageDisplay.
Is "yourPathField" = Image (that is the name of my column in Table1 where path is stashed)
Not sure where to dump that code...in properties for object dont see "At runtime" in Events tab...

Also, I made each image 3"w X 2" h and and making my object box exactly that size....
Im sorry, I see now where I should insert that code... On the Form Properties Events tab...

So I inserted an image from the toolbox and pointed to Record 1's image... and in the properties named it "ImageDisplay". Then plugged code I am attaching with my field names etc. in the Form Properties On Current code. The name of the column in Table1 where the path is stored is called "Image".

The code is doing something to my first record...pointing to the wrong image. Nothing displaying nothing on other records.

Thank you for your time.

Private Sub Form_Current()
 
    If IsNull(Me.Image) Then
        Me.ImageDisplay.Picture = ""
    Else
        Me.ImageDisplay.Picture = Split(Me.Image,"#")(0)
    End If
 
End Sub

Open in new window

LPurvis-

I'm trying to do the same thing, but the Picture property of the Image control doesn't accept URLs as an argument. It appears that the image files need to reside on either a local drive or a local network drive, not on the internet. I might want to display, for example, an image such as "http://i.usatoday.net/news/_photos/2008/09/11/ike3-sq.jpg" on my Access form without having to bring the image in locally. This allows the author of the image to change it and have those changes reflected next time I bring up the form with that image on it.

Can this be done?
My images live locally....I think my issue is I dont know what an Image Control actually is :)

Where do I get it? From toolbox?
Teelamaria-

Yes, the image control is found in the toolbox. It's the one that looks like the sun over a couple mountains.

Click that, then on your form and a dialog will come up asking you to locate your picture. At this point you can enter the location of any picture and later manipulate it programmatically as LPurvis is suggesting (also see below). If your image is local, then you shouldn't have to do the split() stuff; you should just be able to provide it with the location of your image (which can be the contents of the Image field from your Table1).

Table1 should include a unique piece of identifying information that would refer to that record and none other. If you don't have one already, add a field called "ID" and set its Data Type to AutoNumber. Then on your form, include this field somewhere (you can make it invisible if you don't want to see it on the form).

To display the right image, do something like the following (assuming your form is named "Form1" and you Image control is named "ImageDisplay"):

Private Sub Form_Current()
    RecordID = Forms("Form1")![ID]
    If IsNull(RecordID) Then
        MyImage = ""
    Else
        MyImage = Nz(DLookup("[Image]", "[Table1]", "[ID]=" & RecordID))
    End If
    Me.ImageDisplay.Picture = MyImage
End Sub

Open in new window

If you're using a URL then you'll either have to use a web browser control and view the image online through that (instead of an Image control) or use code to download the file to your HD and then set that local path to the Image control's Picture property.
(Obviously the former is easier than the latter, and will also seem faster as the image can load progressively in the web browser control - whereas it would have to download entirely before it could be displayed in the Image control :-)

>> "If your image is local, then you shouldn't have to do the split() stuff"

The Split stuff allows for the hyperlink field's formatting.  However with this being a file rather than URL I dare say we need to check a different part of it.  
i.e. if the user typed in the text the the tags will probably just be surrounding the text as is and so we take the next element
Me.ImageDisplay.Picture = Split(Me.Image,"#")(1)

(Obviously we could write a function which did nothing else than parse out the text from the hyperlink field - incase the values in the field were inserted via code, whereby the tags wouldn't be inserted, or perhaps even a mixture of both).
But for simplicitly here - let's start by assuming the were entered via the UI.
If there are still discrepencies then we can look to a more robust dedicated function (which doesn't presume the entry method of the UNC paths).  Hmm - these are full paths to the file yes?  Including the entire UNC/mapped drive?  Not just the file name?

Assuming that the form is bound to the table which includes the hyperlink field (which you've called Image - not be most ideal choice as it's a reserved word) then there's no need for Lookups. Indeed they'd just be unnecessary hits on the database for data which is already loaded.
LPurvis-

>> "use code to download the file to your HD and then set that local path to the Image control's Picture property."

I'm not impressed with the appearance of the Web Browser Control on my form, so despite the advantages of that approach I'd like to see how the above approach works. What code would I use to DL the image to my local drive?
Really?  What about it's appearance don't you like?
Apart from that, file download.  Potentially accomplished in more than one way.
The more traditional would probably be found at the Access Web : http://www.mvps.org/access/modules/mdl0037.htm
You could either check that the file hasn't already been downloaded before (downloading again and then) displaying - or replace it every time regardless with each record navigation.
All I want is to display an image, but the Web Browser Control shows a vertical scroll bar (albeit ghosted) even when the image fits entirely within the control. There may be a way to eliminate that and simply have the browser "page" fit within the control, but I can't find much in the way of documentation for the Control.

I saw the InetTransferLib in my searches for a solution, but was hoping to get by without adding anything that wouldn't be "standard," both because our IT frowns on it (a lessor reason) and also because the application will be running on multiple computers that won't have the lib installed (the greater reason).
AFAIK the library used to be installed with IE...
It's not on your user's machines??
See below for my usual alternative module of choice.
Be advised - it's prone to caching!

Option Compare Database
Option Explicit
 
Const cModName = "basHTTPDownload"
 
Function fHTTPDownload(strTarget As String, strSaveAs As String, _
                            Optional strUN As String, Optional strPW As String) As Boolean
On Error GoTo errHere
 
    Dim xmlHTTP As Object
    Dim strRespText As String
    
    fHTTPDownload = True
    
    Set xmlHTTP = CreateObject("Microsoft.XMLHTTP")
    
    With xmlHTTP
        .Open "GET", strTarget, False, strUN, strPW
        .setRequestHeader "cache-control", "no-cache,must revalidate"
        .Send
        SaveFile strSaveAs, .responseBody
    End With
    
ExitHere:
    Set xmlHTTP = Nothing
    Exit Function
    
errHere:
    fHTTPDownload = False
    'Your error handling here
    Resume ExitHere
End Function
 
Private Sub SaveFile(strFilePath, bytArray)
On Error GoTo HandleErr
 
    Dim objStream As New ADODB.Stream
 
    With objStream
        .Type = adTypeBinary
        .Open
        .Write bytArray
        .SaveToFile strFilePath, adSaveCreateOverWrite
    End With
 
ExitHere:
    Exit Sub
 
HandleErr:
    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in " & cModName & ".SaveFile", vbExclamation
    Resume ExitHere
 
End Sub

Open in new window

Rag,
the code you provided wants work... but I get the attached error. Any suggestions?

Thanks...hope your issue gets worked out too...

-Tee


Document1.pdf
What's the actual code you're using?
That the error refers to the path including a pound sign implies that it can't be using the split method...
The below code in my Form>Properties>Event>On Current

Private Sub Form_Current()
    RecordID = Forms("Sales Database")![ID]
    If IsNull(RecordID) Then
        MyImage = ""
    Else
        MyImage = Nz(DLookup("[Image]", "[Table1]", "[ID]=" & RecordID))
    End If
    Me.ImageDisplay.Picture = MyImage
End Sub
That's not a method I suggested.  
Is your Image field not in the same table as your form is bound to?
My image field , called "Image", is in "Table1", where my form, "Sales Database", pulls its info from. I think that is what you mean by bound :)

I responded to your method but did not get a reply about issue I was confronting, so in menatime, I just  tried ragoertz's method to see if I could get it to work...My comment about your methos was at 3:52pm above (sorry, dont know what other way I can point you to it :)

Thank you everyone for your time!!!! Getting there....
ASKER CERTIFIED SOLUTION
Avatar of Leigh Purvis
Leigh Purvis
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
What a beautiful thing!!! Thank you so very much!!!
Tee-

I won't be checking this site for a while, but if you still need help, I'd be glad to do what I can. Just email me at ragoertz@gmail.com.

Ron
LPurvis-

Thanks for your help. I've decided to go with a Web Browser Control in a form of its own, then the form was resized to hide the ghosted scroll bar. Not the most elegant solution, and not as nice as it would have been had hyperlinks been allowed in Image Controls, but it works.

I've saved your download code for future use!

Ron
You're welcome teelamaria.  Glad you're sorted.