Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    joeyz is offline Novice
    Windows 10 Access 2016
    Join Date
    May 2020
    Posts
    10

    Pulling Picture from Table Query into Picture Box on form

    Hi team!

    Its been about 15 years since I used access/vba and stuck on a problem with images to display into a picture box or label on form.

    I am trying to pull up a picture image on form to a label or picture box. I have even tried changing data types in the filed of the table OLE and Attached. yes, there is data (images) there and my query works just fine.

    Everything works except retrieving the image into form picture box (or label).

    my code...



    Dim strName As String
    Dim imgPicture As Image

    Set rs = CurrentDb.OpenRecordset("qry_Team")
    If Not rs.EOF Then
    strName = rs.Fields(1)
    imgPicture = rs.Fields(2)
    Me.txt_Name.Caption = strName
    Me.img_Box.Picture = imgPicture
    End If

  2. #2
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    7,162
    Not clear what you have/are doing. AFAIK a label cannot have a picture so you lost me there. Some controls such as a command button can have an embedded pic, but then there is the image control, and IMO sometimes people try to use an unbound object control, which seldom works. I don't know what you mean by "picture control" since there's no such object by that name. Also, I'm afraid the code you posted makes little sense as there would be no point in creating a recordset just to get one value from an arbitrary record from one field and do nothing with the other field. In that code, one thing seems totally unrelated to the next.

    State where your images are (I presume in a table, which is less than ideal and perhaps should be the starting point of this problem), what control type you have, and what you are trying to do. It might be worth noting at this point that keeping images in a table is not something experienced developers would do because it contributes immensely to database bloat (file size). That may be the most important thing at this point.
    - "doesn't work" doesn't help. Implement changes in copies of your database.
    "Everyone has a photographic memory; some just don't have film." Steven Wright

  3. #3
    joeyz is offline Novice
    Windows 10 Access 2016
    Join Date
    May 2020
    Posts
    10
    Sorry, I will explain better.

    I have a Table called "Team" with 3 fields and 10 records.

    "Team"
    -----------------
    0 1 2
    ID
    Name
    Picture
    (AUTO), (LongText), (OLE Object)


    I have a query that randomly picks 1 team (ID, Name and Picture) as the winner.

    I have a Form with a...
    TextBox to display name of the team.
    PictureBox to display the picture of that team.

    How do I get the picture to load from table into the form?
    Everything works except picture won't load into imagebox on form.
    I thought it might be because I had the data type as "Attached" so I switched to "OLE Obejct" but still won't work.

    I get a run-time error in both cases.
    Something about "Object Variable or With Block variable not set."

    thanks.

  4. #4
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    7,162
    Maybe if you posted the actual code someone can help with it. What doesn't really help is there is no such thing as a "picturebox" or an "image box" and the error you describe has nothing to do with any query you're using. Maybe you should post a compacted and zipped copy of your db with instructions on what to do to replicate the issue and include enough data to work with it. In that case, it needs to be clear as to what you want to happen and when.
    - "doesn't work" doesn't help. Implement changes in copies of your database.
    "Everyone has a photographic memory; some just don't have film." Steven Wright

  5. #5
    joeyz is offline Novice
    Windows 10 Access 2016
    Join Date
    May 2020
    Posts
    10
    SuperHeroes.zip

    Okay, I will upload the accb

    when I do,
    Check out Form 1
    Then check out Form 2. <- this is the one Im trying to learn how to pull images from a stored database table. I think I should be using the attached and not OLE.
    Last edited by joeyz; 05-16-2020 at 11:07 PM. Reason: corrections

  6. #6
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    7,162
    Your db is missing tbl_Superheros that the query is based on?

    One issue will be that you've coded as Me.Att_Box.source There is no property for that control by the name of "source".

    I'll limit this part to objects native to Access: Intellisense pops up when you begin coding for an object (e.g. a form) so as soon as you enter Me. you get a popup list of its properties, methods and collections. Thus Att_Box will show up. You can type or pick it from the list. When you do, and then enter . again, up comes intellesense with the same type of list. Do that and you will not find "source" in it. You will find controlsource in it. That is the property (or sometimes method) that you're after. That's where my fixing has to end because as I said, in this version your code then fails for a different reason - the missing table needed by the query that your code is trying to run.

    Note that sometimes, but rarely, the list does not contain every method or property. In this case, it's not that 'source' is valid but missing from the list.

    Maybe that db doesn't paint the complete picture of what you're doing, but based on that I don't see how the command button code is going to provide a random character. Also, if you have a query that will return an attachment id, there's no need for a recordset I'd say.
    Last edited by Micron; 05-17-2020 at 11:24 AM. Reason: added comment
    - "doesn't work" doesn't help. Implement changes in copies of your database.
    "Everyone has a photographic memory; some just don't have film." Steven Wright

  7. #7
    joeyz is offline Novice
    Windows 10 Access 2016
    Join Date
    May 2020
    Posts
    10
    SuperHeroes.zip

    oops! sorry about the wrong query. I exported wrong one.

    its fixed. Here ya go.

    I am trying to capture the picture from FIELD(2) and put into my form.

    See Screen shot and accb.
    Attached Thumbnails Attached Thumbnails Image.png  

  8. #8
    joeyz is offline Novice
    Windows 10 Access 2016
    Join Date
    May 2020
    Posts
    10
    All I am trying to do is hard code pictures to come up on form2 through query.
    Attached Thumbnails Attached Thumbnails image2.png   image4.png   image3.png  

  9. #9
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    7,162
    why not just bind the form to the query and let the button call a Me.Requery? You have the query to do the heavy lifting - the code seems entirely unnecessary. I find the issue is easily solved with what you posted (that query didn't work either because the table it wanted is not in the db) by doing just that. I've never used an attachment control because I'd use an image control and pass the file location to it. Thus I'd have to research it, and it may not even be possible to alter the picture displayed by that control by using vba.
    - "doesn't work" doesn't help. Implement changes in copies of your database.
    "Everyone has a photographic memory; some just don't have film." Steven Wright

  10. #10
    orange's Avatar
    orange is online now Moderator
    Windows 10 Access 2016
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    14,931
    I have not found an example that shows how or if you can display all the attachments in a table on a form. In my case (Access in O365) I have a table with 1 attachment field that has 2 records(ID 2 and 3). The first record has 1 attachment and the second record has 5 attachments.
    In the attached pngs, attach1.png shows a form created with the wizard. This shows record 1 and the only attachment it has.
    I moved to the next record and it displays the first attachment of the 5 for this record. As soon as you click on the graphic a small box appears with 2 arrows and a paperclip as shown in attach2.png Attach3.png show the result of clicking thee paperclip.

    I have never used attachment fields other than a few responses to posts for saving attachments to the file system or loading from file system to an attachment field.

    There are a number of posts for various time frames that show nobody has had much success with this use of attachment fields.
    Many are frustrated with the lack of documentation and examples for this subject.

    I tried (vba) to get a recordset with all the attachments in the table, then to display them randomly based on a button click. I got repeated datatype errors and item not found in collection, and finally gave up. As I said I don't use attachment fields, so may have missed the obvious.

    If you find a solution, I recommend that you post same as I'm sure others will be grateful.

    Good luck.
    Attached Thumbnails Attached Thumbnails attach1.PNG   attach2.png   attach3.jpg  

  11. #11
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    7,162
    If you find a solution, I recommend that you post same as I'm sure others will be grateful.
    Did you see my post where I said that AFAIC, I solved this particular case? I'm not saying it would be the solution for every case, just this one.
    - each record has one attachment
    - the db has a query that returns the attachment field
    - bind the form to the query, bind the control to the field, click the button and requery the form. The query returns a random record.

    I've read that an attachment field is a kind of multi value field, so what you're saying about one record having several images makes sense, although I've never seen it before.
    - "doesn't work" doesn't help. Implement changes in copies of your database.
    "Everyone has a photographic memory; some just don't have film." Steven Wright

  12. #12
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    7,162
    Update: After posting that I had read that an attachment field was multi value field type, I remembered the Recordset2 so I started playing around. I was able to drill down to the attachment field multi values which are YourFieldName.FileData, .FileName and .FileType
    However, you cannot set the attachment control controlsource property to one of these values - it has to be the name of an attachment field. So my conclusion as to why no one has had success with this is because the only acceptable property value for the control's controlsource is a domain field.
    The ControlSource property must be set to the name of a field that stores attachments.
    https://docs.microsoft.com/en-us/off....controlsource
    - "doesn't work" doesn't help. Implement changes in copies of your database.
    "Everyone has a photographic memory; some just don't have film." Steven Wright

  13. #13
    joeyz is offline Novice
    Windows 10 Access 2016
    Join Date
    May 2020
    Posts
    10
    Quote Originally Posted by Micron View Post
    why not just bind the form to the query and let the button call a Me.Requery? You have the query to do the heavy lifting - the code seems entirely unnecessary. I find the issue is easily solved with what you posted (that query didn't work either because the table it wanted is not in the db) by doing just that. I've never used an attachment control because I'd use an image control and pass the file location to it. Thus I'd have to research it, and it may not even be possible to alter the picture displayed by that control by using vba.

    Huh???The SuperHeroes was just an example to illustrate what I'm trying to accomplish.
    Don't you think I already know that I can bind to a form

    I am simply trying to figure out how to call pictures into an image box on form using VBA. Sure it can be done because I did it years ago.
    There is a way to call a picture attachment from a stored table record through vba code, I just forgot how and can't remember. It might be a resources file I'm missing.

  14. #14
    joeyz is offline Novice
    Windows 10 Access 2016
    Join Date
    May 2020
    Posts
    10
    Thanks Orange.

    I'm with ya, works just fine dynamically if you set and bind the source and launch all the records. There is a way for separate operations where your your working on a project that is not the records and scrolling through them with images. When I find out, I will share with you. There is a way to push to screen through vb, C#, c++. I did it vba years ago and don't remember how I did it.

  15. #15
    joeyz is offline Novice
    Windows 10 Access 2016
    Join Date
    May 2020
    Posts
    10
    Orange, check this out (see code).
    I call it brute force, swapping tables and records.
    It works but I prefer the cleaner way. Some people may find it useful.

    Micron, there is always a way to load attachments (pictures) from stored tables through vba code.



    But I discovered a bug with number generator I got off the forum. Note how many times I ran it and it doesn't seem to include first record. Maybe you talented guys can check this. Why it seems to never include #1 record in table on random generator.

    SELECT TOP 1 *
    FROM SuperFemaleHeroes_Table
    ORDER BY Rnd(Int(Now()*id)-Now()*id);
    Attached Thumbnails Attached Thumbnails SuperHeroesPicture.png  
    Attached Files Attached Files

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 8
    Last Post: 12-17-2017, 12:33 PM
  2. Query Help (picture)
    By cmf0106 in forum Access
    Replies: 1
    Last Post: 03-10-2013, 01:28 AM
  3. Replies: 2
    Last Post: 09-02-2011, 09:16 AM
  4. Replies: 1
    Last Post: 11-02-2010, 03:35 PM
  5. Can't Load a picture from the table
    By dada in forum Reports
    Replies: 1
    Last Post: 09-18-2010, 10:17 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 - Senior Forums