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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using Me![ImageFrame].Picture = Me![ImagePath] 2

Status
Not open for further replies.

Mayhem9

Technical User
Dec 19, 2009
155
AU
Hello - I was reading the (closed) thread702-289543 in an attempt to solve a problem I have.

I am building a database to log my tools and I wanted to include a photo of each item. I added the field "ImagePath" into the table and placed an image into the form, named "ImageFrame". The following code was then used:

Code:
Private Sub Form_AfterUpdate()
    On Error Resume Next
    Me![ImageFrame].Picture = Me![ImagePath]
End Sub

Private Sub Form_Current()
    On Error Resume Next
    Me![ImageFrame].Picture = Me![ImagePath]
End Sub

Now this works perfectly until I exit the database! When I open it up again, each record shows the default image (that has been placed into the records where I have not yet taken a photo).

If I delete the ImageFrame picture in the form and create it again, it works. As you can imagine, it is a bit of a pain to do this each time I open the database.

I read somewhere that the image that I selected to add the image into the form should be deleted (i.e. leave “Picture” blank in the image properties). I tried this and it simply prevents any picture from being shown.

Any help is greatly appreciated,
Darren.

p.s. I have no idea what my user type means - there wasn't a "has no idea what he is doing" option!
 
make a default image. Something like an image with the text "No Image Available"

Private Sub Form_Current()
On Error Resume Next
if not trim(me!imagePath & " ") = "" then
Me![ImageFrame].Picture = Me![ImagePath]
else
Me![ImageFrame].Picture = "C:\defaultImage.bmp"
end if
End Sub

I also think you can simply put in the else
Me![ImageFrame].Picture = ""
but untested
 
Thank you both for your help.

dhookom - I had not included the full path to the image and changing the database to reflect this has solved the problem. It was obviously linking the path to the default image that I was selecting to create the ImageFrame. This explained why it stopped working when the database was closed.

MajP – I already had a default image, which displays when no image for the item exists. I created this to overcome the problem associated with the image of one item being displayed for subsequent items (when no image existed).

I have used your code, which now means that I don’t have to add the default image to each record where an image does not yet exist.

I am not very knowledgeable when it comes to the code used in Access and represent the classical cut and paste user! As such, I am wondering if I can ask another question that related to this subject – rather than opening another thread:

To make life simpler, I am naming the image files so they are the same as the serial number for the item. So, if the serial number is ABC123 then the image will be called ABC123.jpg and the FilePath will be set to D:\Tools\Images\ABC123.jpg

Is there any way to have the field ImagePath automatically created from the serial number? At the moment, I have set a default value for ImagePath, so that it enters “D:\Tools\Images\” and I then add the image name.

If my thinking is correct, the code provided by MajP will show the default image if the image path automatically created does not lead to an image (for example, I have entered the tool but not yet taken the picture). If so, this will be a big help.

Thanks again – I’m off to search on how to show the record numbers (record x of y), so I can get rid of the default one.
 
This is how I do it. I do not even save the path. I only save the Serial. And I name each image by the serial. It can be a bmp, jpg, or gif.

Put this code in a standard module. No need to save the path.
Code:
Public Sub LoadImage(strDirectory As String, strSerial As Variant, mImageControl As Access.Image)
   'On Error GoTo PictureError
   
   Dim objFileSystem As Object
   Dim strPath As String
   Dim blnFileExists As Boolean
   Dim imageTypes() As String
   Dim i As Integer
   
   Set objFileSystem = CreateObject("Scripting.FileSystemObject")
   strSerial = Nz(strSerial, "")
   If Not Right(strDirectory, 1) = "\" Then
     strDirectory = strDirectory & "\"
   End If
   strPath = strDirectory & strSerial
   imageTypes = Split(".jpg,.gif,.bmp", ",")
   For i = LBound(imageTypes) To UBound(imageTypes)
     strPath = strPath & imageTypes(i)
     If objFileSystem.FileExists(strPath) Then
       blnFileExists = True
       Exit For
     End If
   Next i
   If blnFileExists Then
      mImageControl.Picture = strPath
      mImageControl.Visible = True
   Else
      mImageControl.Picture = ""
      mImageControl.Visible = False
   End If
   Exit Sub
PictureError:
   MsgBox Err.Number & "  " & Err.Description & Chr(13) & "In PictureFromFile Class"
End Sub

Now you would call this from your form's on current event like:

Private Sub Form_Current()
Call LoadImage("C:\", Me.strSerial, Me.imgCntrlOne)
End Sub

Where strSerial is the field with the serial, imgControlOne is the image control, and "C:\" is the directory.

If it can not find a ABC123.jpg, .bmp, or .gif in the provided directory it will actuall hide the image control.
 
Thank you MajP,

I haven't had time to play with my database since my last post but hope to later this week. A dead HDD on my main PC has thrown an unneeded spanner in the works.

Just out of curiosity, I have a few items that have no serial number and wonder if there is a way to factor this in. I actually forgot about this.

Also - where should I place the main body of code? I haven’t done much coding.

Thanks,
Darren
 
What do you use for a name if there is no serial. I used the term "serial" in the code, but actually I use whatever the unique primary key is. So if my primary key is an autonumber then the pictures are simply saved as 1.jpg, 2.jpg, 3.jpg, 4.gif, 5.bmp..........

The code goes in a standard module. Each form has a class module, and you can make standard modules which are general group of code that can be used throughout the application. When in the visual basic window choose, "Insert", "Module".
 
The reason I do not save the path is what happens when you need to move the database? Most of my db's have a database information table. This is where I can keep default values and other information. In the table I can put the directory location of the pictures. Normally this is a subfolder inside the folder where the database is located. Now in the above code I would pull the "strDirectory" from the table using a dlookup. If I move the pictures, I can just change the directory in the table, instead of the path to every picture.
 
OK - you have lost me with the modules! I will have to do some reading I think. This is one of those ‘the more you know, the more you know that you don’t know’ moments!

Serial is recorded as 'N/A' when no real value exists. I always set a table ID, which is an auto number and primary key, as I may actually have more than one of the same tool (e.g. welding clamps).

In this table, ToolID is set as the primary key and is an auto number. Manufacturer is a drop down list and Serial is a text field into which the serial is entered.

I have the images in a sub folder of the folder in which the db is located. The db is in a folder named Tools, which is on my network, so I can access it from my workshop. I don’t think I will ever need to move it but it sounds like a good skill to learn. Thanks for the tip.

Darren.
 
Thanks - I haven't gotten to those yet but would probably make it generic. Something like NA0001, NA0002 etc. However, if there is a naming system that will make the process more straight forward, I would happily use it.

A lot of the fields are not yet populated and I only have a handful of pictures (to test with). I had most of the data in Excel but decided that I wanted to store more info and display it on the screen more efficiently. I also like the auto filter in Excel to find what I am looking for. Now I just have to figure out how to filter in Access!
 
Happy New Year!

I haven't played around with my database for a few weeks, given the silly season.

Having managed to get two events to run in the Form, On Current procedure some weeks ago, I was pretty disappointed to be greeted by the following error:

Your Microsoft Office Access database or project contains a missing or broken reference to the file 'DR_BUTTON_CONTROLL.OCX' version 19.0.

I couldn't find a solution, so I deleted the code for the image (this post) and the custom navigation (thread702-1583844) and decided to start again (I made a copy of the database before doing this)

I replaced each component piece by piece and tested it between each step. I started with the custom navigation, as that included more events. There were no problems until I added the image code for the On Current procedure (using subA and subB).

The error message is:

Compile error:
Can't find project or library

When I click OK, I am taken to the VB code and “Private Sub subA()” is highlighted in yellow and “!ImagePath” is highlighted in blue.

Code:
Private Sub subA()
    On Error Resume Next
    If Not Trim(Me!ImagePath & " ") = "" Then
      Me![ImageFrame].Picture = Me![ImagePath]
    Else
      Me![ImageFrame].Picture = "D:\Tools\Images\No_Pic.gif"
    End If
End

I have included the ImagePath field in the table and it shows the image name (i.e. the field exists in the table and contains appropriate data).

The entire code for the Form, On Current events is as follows:

Code:
private sub Form_Current()
  call subA
  call subB
end sub

private sub subA()
    On Error Resume Next
    if not trim(me!imagePath & " ") = "" then
      Me![ImageFrame].Picture = Me![ImagePath]
    else
      Me![ImageFrame].Picture = "D:\Tools\Images\No_Pic.gif"
    end if
end sub

private sub subB()
    If Me.RecordsetClone.RecordCount > 0 Then        
        Me.RecordsetClone.MoveLast
        Me![TotRec] = Me.RecordsetClone.RecordCount
    Else
        Exit Sub
    End If
end sub

Any ideas as to what I am doing wrong here?

Cheers,
Darren
 
When in VBE, menu Tools -> References ...
Untick any MISSING reference

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Happy New Year!

I haven't played around with my database for a few weeks, given the silly season.

Having managed to get two events to run in the Form, On Current procedure some weeks ago, I was pretty disappointed to be greeted by the following error:

Your Microsoft Office Access database or project contains a missing or broken reference to the file 'DR_BUTTON_CONTROLL.OCX' version 19.0.

I couldn't find a solution, so I deleted the code for the image (this post) and the custom navigation (thread702-1583945) and decided to start again (I made a copy of the database before doing this)

I replaced each component piece by piece and tested it between each step. I started with the custom navigation, as that included more events. There were no problems until I added the image code for the On Current procedure (using subA and subB).

The error message is:

Compile error:
Can't find project or library

When I click OK, I am taken to the VB code and “Private Sub subA()” is highlighted in yellow and “!ImagePath” is highlighted in blue.

Code:
Private Sub subA()
    On Error Resume Next
    If Not Trim(Me!ImagePath & " ") = "" Then
      Me![ImageFrame].Picture = Me![ImagePath]
    Else
      Me![ImageFrame].Picture = "D:\Tools\Images\No_Pic.gif"
    End If
End

I have included the ImagePath field in the table and it shows the image name (i.e. the field exists in the table and contains appropriate data).

The entire code for the Form, On Current events is as follows:

Code:
private sub Form_Current()
  call subA
  call subB
end sub
------------------------------------------------------------
private sub subA()
    On Error Resume Next
    if not trim(me!imagePath & " ") = "" then
      Me![ImageFrame].Picture = Me![ImagePath]
    else
      Me![ImageFrame].Picture = "D:\Tools\Images\No_Pic.gif"
    end if
end sub
------------------------------------------------------------
private sub subB()
    If Me.RecordsetClone.RecordCount > 0 Then        
        Me.RecordsetClone.MoveLast
        Me![TotRec] = Me.RecordsetClone.RecordCount
    Else
        Exit Sub
    End If
end sub

Any ideas as to what I am doing wrong here?

Cheers,
Darren
 
Thanks PHV - I will have a look at those and see if I can figure it out.

Sorry about the double post, I hit the preview button and received an error. Hit the back button, corrected an incorrect link to another thread and hit submit. I cannot see how to delete it - so if someone wishes to delete it, please feel free to do so.
 
Thank you PHV,

I unchecked the missing button reference and all is working fine now.

Cheers,
Darren
 
Hello,

I am sure that someone told me how to get the images to show as soon as they are selected, rather than when the record is navigated away from and then back to but I cannot find it anywhere. I have searched all the posts I have created/replied to and cannot see it. What is worse, is that it is currently working but I cannot see the code that does it!

The reason I am looking for it is that I have also added in a similar feature to store the path of certain tool specific documents (exploded diagrams, manuals etc), so these can be opened directly from the database. I have this working perfectly but they do not show until the record is navigated away from.

Also, is there a way to default to the first page in a tab control when moving from one record to the next? The image of the tool is on the first page, whilst the documents are on the second page.

Thanks,
Darren
 
You probably need to reequery or refresh the form after you add the document. Look at the "requery" and "refresh" methods.

The onCurrent event of a form occurs every time you move to a record. So use the on current event. The value of a tab control is the page number ( they are zero based)

me.tabCtlOne.value = 0
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top