Obtain the picture from an OLE field type of ACCESS database-VBForums
Results 1 to 10 of 10

Thread: Obtain the picture from an OLE field type of ACCESS database

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    81

    Obtain the picture from an OLE field type of ACCESS database

    Obtain the picture from an OLE field type of ACCESS database

    I need to set a picture property of a commandbutton dinamically (at run-time) according to an OLE type field of ACCESS database.

    commandbutton1.Picture = recordset !Olefieldname 'ERROR TYPE MISMATCH

    plz help

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,135

    Re: Obtain the picture from an OLE field type of ACCESS database

    Search through my posts - I have two procedures posted: one to store files and one to read them back.

  3. #3
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Obtain the picture from an OLE field type of ACCESS database

    Try this. I'm not sure if you can load any type of picture onto a command button. Why don't you use a Picturebox, and use it's click event as a button?


    VB Code:
    1. commandbutton1.Picture = LoadPicture(recordset !Olefieldname)

  4. #4
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,089

    Re: Obtain the picture from an OLE field type of ACCESS database

    Quote Originally Posted by dglienna
    Try this. I'm not sure if you can load any type of picture onto a command button. Why don't you use a Picturebox, and use it's click event as a button?


    VB Code:
    1. commandbutton1.Picture = LoadPicture(recordset !Olefieldname)
    Sure you can. Just set the Style property to graphical and then load your image to its Picture property.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Star Wars Gangsta Rap Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel Core 2 Extreme Ed., 2 WD Raptor 10K RPM 300 GB HDs, 2 GBs DDR2 667 MHz RAM, 2 Viewsonic 24" LCDs, Windows 7 SP-1/Windows 8.1, Office 2010, VS 2013

  5. #5
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,135

    Re: Obtain the picture from an OLE field type of ACCESS database

    You have to extract file first, store it on the hard disk and then use LoadPicture() to actually load image into whatever control ...

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    81

    Re: Obtain the picture from an OLE field type of ACCESS database

    it doesnt work:
    commandbutton1.Picture = LoadPicture(recordset !Olefieldname)

    ok, i have to save the pic to hdd, but how can I do this?


    plz, help, im very new in VB.

  7. #7
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,135

    Re: Obtain the picture from an OLE field type of ACCESS database

    Here is a "ready-to-go" logic to extract image from database and load it into the Image control or Picturebox (your choice - read my comments):
    VB Code:
    1. Option Explicit
    2.  
    3. Private Const MAX_PATH = 260
    4. Private Const BLOCK_SIZE = 10000
    5.  
    6. Private Declare Function GetTempFileName Lib "kernel32" Alias "GetTempFileNameA" _
    7.     (ByVal lpszPath As String, ByVal lpPrefixString As String, _
    8.      ByVal wUnique As Long, ByVal lpTempFileName As String) As Long
    9.  
    10. Private Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" _
    11.     (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
    12.  
    13. Public Sub ExtractImage(adoConn As ADODB.Connection, _
    14.                         sPicId As String, imgTemp As Image)
    15. '=======================================================================================
    16. 'NOTES:
    17. '* this samples requires table ALL_PICTURES (could be any name) with at least 3 fields:
    18. '   - PICTURE_ID    (could be text or number)
    19. '   - ACTUAL_IMAGE  (OLE Object)
    20. '   - IMAGE_SIZE    (Number - Long)
    21. '
    22. '* adoConn must be valid and open connection to your database
    23. '* imgTemp can be define as Picturebox as well
    24. '* also, instead of sPicId you may pass entire sql statement
    25. '* logic is "adaptable" for any other database type including Oracle, SQL Server, ...
    26. '  however, field type will differ: in Oracle it would be "Long RAW" ...
    27. '* you may add one more field IMAGE_TYPE (text) so you know what type of file is stored
    28. '  so if it is some kind of image then you would use LoadImage() or if is not then
    29. '  you may need to simply open extracted file.
    30. '======================================================================================
    31. Dim adoRST As ADODB.Recordset, strSql$
    32. Dim bytes() As Byte
    33. Dim file_name As String
    34. Dim file_num As Integer
    35. Dim file_length As Long
    36. Dim num_blocks As Long
    37. Dim left_over As Long
    38. Dim block_num As Long
    39. Dim hgt As Single
    40.  
    41. On Error GoTo ErrHandler
    42.  
    43.     Screen.MousePointer = vbHourglass
    44.     DoEvents
    45.    
    46.     strSql = "SELECT * FROM ALL_PICTURES WHERE PICTURE_ID = '" & sPicId & "'"
    47.    
    48.     Set adoRST = New ADODB.Recordset
    49.     adoRST.Open strSql, adoConn
    50.    
    51.     If adoRST.EOF Then
    52.         Screen.MousePointer = vbDefault
    53.         Exit Sub
    54.     End If
    55.    
    56.     If IsNull(adoRST.Fields("ACTUAL_IMAGE")) Then
    57.         Screen.MousePointer = vbDefault
    58.         Exit Sub
    59.     End If
    60.    
    61.     ' Get a temporary file name.
    62.     file_name = TemporaryFileName()
    63.    
    64.     ' Open the file.
    65.     file_num = FreeFile
    66.     Open file_name For Binary As #file_num
    67.         ' Copy the data into the file.
    68.         file_length = adoRST.Fields("IMAGE_SIZE")
    69.        
    70.         num_blocks = file_length / BLOCK_SIZE
    71.         left_over = file_length Mod BLOCK_SIZE
    72.        
    73.         'get all chunks and write then to a temp file
    74.         For block_num = 1 To num_blocks
    75.             bytes() = adoRST.Fields("ACTUAL_IMAGE").GetChunk(BLOCK_SIZE)
    76.             Put #file_num, , bytes()
    77.         Next block_num
    78.         If left_over > 0 Then
    79.             bytes() = adoRST.Fields("ACTUAL_IMAGE").GetChunk(left_over)
    80.             Put #file_num, , bytes()
    81.         End If
    82.     Close #file_num
    83.    
    84.     'load image
    85.     '****************************************
    86.     imgTemp.Picture = LoadPicture(file_name)
    87.     '****************************************
    88.    
    89.     Screen.MousePointer = vbDefault
    90.     Exit Sub
    91.  
    92. ErrHandler:
    93. '-----------
    94.  
    95.     Debug.Print Err.Description
    96.     Err.Clear
    97.     Screen.MousePointer = vbDefault
    98.    
    99.     'Resume Next
    100.     Exit Sub
    101.  
    102. End Sub
    103.  
    104. Public Function TemporaryFileName() As String
    105. '==============================================
    106. Dim temp_path As String
    107. Dim temp_file As String
    108. Dim length As Long
    109.  
    110.     ' Get the temporary file path.
    111.     temp_path = Space$(MAX_PATH)
    112.     length = GetTempPath(MAX_PATH, temp_path)
    113.     temp_path = Left$(temp_path, length)
    114.  
    115.     ' Get the file name.
    116.     temp_file = Space$(MAX_PATH)
    117.     GetTempFileName temp_path, "per", 0, temp_file
    118.     TemporaryFileName = Left$(temp_file, InStr(temp_file, Chr$(0)) - 1)
    119.  
    120. End Function

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    81

    Re: Obtain the picture from an OLE field type of ACCESS database

    ERROR: invalid picture

    i just put the pic into the access as ole object..

  9. #9
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,135

    Re: Obtain the picture from an OLE field type of ACCESS database

    I have no clue how you do it so if you want then take a look at my replies in this thread .

  10. #10
    New Member
    Join Date
    Nov 2005
    Posts
    1

    Re: Obtain the picture from an OLE field type of ACCESS database

    hi, i wanted to use the ole pictures stored to be displayed in php. but all i get is gibberish after i convert the hex to string. is it my header wrong? my header is content-type : image/jpeg (this is done in separate file called test.php). then, another php content is <img src="test.php?id=123"> calling test.php

    still no image.. please help :-(

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.