Results 1 to 10 of 10
  1. #1
    Zipster1967 is offline Novice
    Windows 7 64bit Access 2003
    Join Date
    May 2006
    Location
    Wisconsin
    Posts
    19

    Adding images to form for each record


    I am trying to build a database that keeps track of different fireworks so that we can remember which ones were good from year to year. I am trying to add pictures of each firework so we can recognize them in the store. But I tried using the example from the Microsoft support forum but I keep getting a compile error message and I am not sure why. Can anyone help me with this? I am sending my database so you can see what is going on.
    Last edited by Zipster1967; 07-04-2011 at 10:49 PM.

  2. #2
    Rod is offline Expert
    Windows Vista Access 2007
    Join Date
    Jun 2011
    Location
    Metro Manila, Philippines
    Posts
    679
    Your calling syntax is wrong.

    Code:
    Private Sub Form_Current()
    CallDisplayPic
    End Sub
    For Sub procedures the generalised syntax is:

    ModuleName.ProcedureName ArgumentList

    For Function procedures, such as yours, the syntax is:

    Code:
    strResult = DisplayPic.DisplayPic(ctlPicture:=Whatever, strImagePath:=Whatever)
    I advise you not to name any procedure the same as the module!

    My example above uses the option to name each argument; you can simply pass the argument values by position.

    I shall repost with what I consider a slightly more elegant way of coding your requirement.

  3. #3
    Rod is offline Expert
    Windows Vista Access 2007
    Join Date
    Jun 2011
    Location
    Metro Manila, Philippines
    Posts
    679
    I've just stumbled across the source where you obtained your code - Microsoft no less. You failed to include the following private sub procedure in the coding behind your form.

    Code:
    Private Sub CallDisplayImage()
    Me!txtImageNote = DisplayImage(Me!ImageFrame, Me!txtImageName)
    End Sub
    Now it's a lot clearer!

    There are numerous other errors in your setup, the most serious is that you have included a Bound Object Frame in your form while Microsoft's code works for an Image control.

    I shall post back when I've sorted it all out.

  4. #4
    Rod is offline Expert
    Windows Vista Access 2007
    Join Date
    Jun 2011
    Location
    Metro Manila, Philippines
    Posts
    679
    Code:
    Option Compare Database
    Option Explicit
    Public Function DisplayImage(rctlImage As Access.Control, rvarImagePath As Variant) As String
    On Error GoTo Err_DisplayImage
    Dim strDatabasePath As String
    Dim strImagePath As String
    Dim intSlashLocation As Integer
    Dim strResult As String
     
    'Check control type.
     
    If Not (TypeOf rctlImage Is Access.Image) Then
    rctlImage.Visible = False
    strResult = "Invalid image control."
    GoTo Exit_DisplayImage
    End If
     
    'Check general format of image path.
     
    If Trim(Nz(rvarImagePath, "")) = "" Then
    rctlImage.Visible = False
    strResult = "No image path."
    GoTo Exit_DisplayImage
    Else
    strImagePath = Trim(CStr(rvarImagePath)) 'Convert to string.
    End If
     
    'Add full path extension if necessary.
     
    If strDatabasePath Like "[A-Z]:\*" Then 'Assume full path is supplied. May need to test for [a-z]
    Else
    strDatabasePath = CurrentProject.FullName
    intSlashLocation = InStrRev(strDatabasePath, "\", Len(strDatabasePath))
    strDatabasePath = Left(strDatabasePath, intSlashLocation)
    If strImagePath Like "\*" Then 'Strip leading back slash.
    strImagePath = Right(strImagePath, Len(strImagePath) - 1)
    End If
    strDatabasePath = strDatabasePath & strImagePath
    End If
     
    rctlImage.Picture = strDatabasePath
    rctlImage.Visible = True
    strResult = "Image found."
     
    Exit_DisplayImage:
    DisplayImage = strResult
    Exit Function
     
    'I have altered none of the error handling code.
    Err_DisplayImage:
    Select Case Err.Number
    Case 2220 ' Can't find the picture.
    rctlImage.Visible = False
    strResult = "Can't find image in the specified name."
    Resume Exit_DisplayImage
    Case Else ' Some other error.
    MsgBox Err.Number & " " & Err.Description
    strResult = "An error occurred displaying image."
    Resume Exit_DisplayImage
    End Select
    End Function

    I believe that the procedure should check that the control type of the first argument is an image control. You could go one step further and be more explicit in defining the argument as rimgControl As Access.Image
    I think
    Code:
    If Trim(Nz(rvarImagePath, "")) = "" Then
    Is a better test than simply testing for null since it will catch values that are zero length string and values that are one or more spaces.



    The comment that if there are no back-slashes then the path is ‘relative’ is misleading. What is meant is that the image resides in the ‘same directory’ as the Access mdb. Here I think a better test is as follows:
    1. If the path begins with a letter followed by a colon, then it is safe to assume the complete explicit path is supplied.
    2. If there are no ‘\’s in the path, then assume the same directory.
    3. If there are ‘\’s in the path assume a sub directory. (Ensure that there is no double ‘\’)
    The second and third actions above require identical code.
    The original code relies heavily on Access’ automatic variable type conversions. I prefer to force the type, hence I convert the path to a string at the earliest opportunity.
    There will still be run-time errors if the path is not properly formed or the picture does not exist so the error handling is essential.
    (BTW I have prefixed the argument names with a lower case ‘r’ indicating that the arguments are passed by reference. This is non-standard and is a little quirk of mine. It corresponds to prefixing arguments with ‘v’ when they are passed by value. It’s easy to determine which when viewing the procedure definition but not always so easy when writing the calling code.)
    You had some errors – mainly typos – in your procedure. Label names end in a colon but when referenced in the code the colon is omitted.
    OK, how to get you up and running?
    Delete the Bound Object Frame from your form and insert an Image control. Name this control imgFirework – or whatever.
    You have a field on the table called, ‘Picture.’ I assume this is to contain the full or fragment path of the associated picture. (I suggest you make the default for this field, “\images\0000.jpg” rather than just “\images\.”
    In the declaration section of the code behind your form type the following.
    Code:
    Dim strResult as String
    Somewhere else type
    Code:
    Private Sub CallDisplayImage()
    strResult = DisplayImage(Me!imgFirework, Me!Picture)
    End Sub

  5. #5
    Zipster1967 is offline Novice
    Windows 7 64bit Access 2003
    Join Date
    May 2006
    Location
    Wisconsin
    Posts
    19

    Still not working

    Okay I think I implemented the changes you suggested but I am not sure what I am suppose to put in the on Current procedures and before Update procedures for the form. At least I think that is where my error is coming from. I am still getting a compile error in my form when I open it. The debugger shows me the following:
    Code:
    Private Sub Form_Current()
        Call DisplayImage
    What is suppose to be there?

  6. #6
    Rod is offline Expert
    Windows Vista Access 2007
    Join Date
    Jun 2011
    Location
    Metro Manila, Philippines
    Posts
    679
    Hi,

    The code behind your form should look something like:

    Code:
     
    Option Compare Database
    Option Explicit
    Private Sub Form_BeforeUpdate(Cancel As Integer)
    CallDisplayPic
    End Sub
    Private Sub Form_Current()
    CallDisplayPic
    End Sub
    Private Sub CallDisplayPic()
    Dim strReturn As String
    strReturn = DisplayPic.DisplayImage(Me!imgFirework, Me!Picture)
    Select Case strReturn
    Case "Image found."
    Case Else
    MsgBox strReturn
    End Select
    End Sub
    I have assumed your module is still called DisplayPic and that you have copied my version of the function into it. Thus the function is called DisplayImage.

    What does the compile error say?

  7. #7
    Rod is offline Expert
    Windows Vista Access 2007
    Join Date
    Jun 2011
    Location
    Metro Manila, Philippines
    Posts
    679
    Just noticed that in your post there is an imbedded space in the sub procedure name. It should be CallDisplayPic and not Call DisplayPic. (Call is actually a valid Access command used for Windows APIs and the like.)

  8. #8
    Zipster1967 is offline Novice
    Windows 7 64bit Access 2003
    Join Date
    May 2006
    Location
    Wisconsin
    Posts
    19

    Thanks

    Well that did it. I appreciate everything you did to help me out. It will take me a while to fully understand the code but I am glad to have it working. I plan on using this function in more of my databases where image display is needed. I hope you don't mind. I can see I need to learn alot about VBA for use in access. Can you suggest my best course of action to learn VBA in depth? Thaniks again for everything.

  9. #9
    Rod is offline Expert
    Windows Vista Access 2007
    Join Date
    Jun 2011
    Location
    Metro Manila, Philippines
    Posts
    679
    Great news! As you plan to reuse the code I shall repost with an mdb in which I think the module is more suited to portability. I'll try and include lots of comments so your learning curve is eased.

  10. #10
    Rod is offline Expert
    Windows Vista Access 2007
    Join Date
    Jun 2011
    Location
    Metro Manila, Philippines
    Posts
    679
    Here you go.Attachment 3725

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 9
    Last Post: 06-20-2011, 03:42 PM
  2. Adding new Record issue
    By yosik20 in forum Forms
    Replies: 3
    Last Post: 04-13-2011, 10:19 AM
  3. Replies: 1
    Last Post: 03-29-2010, 04:11 AM
  4. Replies: 17
    Last Post: 08-26-2009, 11:27 AM
  5. Images per record, database form OCX
    By lochie360 in forum Forms
    Replies: 3
    Last Post: 07-26-2009, 03:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums