Manufacturing Information Solutions Forum Index Manufacturing Information Solutions
Your Place for Support and Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Display Images in Access in Code

 
Post new topic   Reply to topic    Manufacturing Information Solutions Forum Index -> Microsoft Access
View previous topic :: View next topic  
Author Message
mistux
Site Admin


Joined: 25 Jun 2004
Posts: 1042
Location: South Bend, Indiana USA

PostPosted: Wed Dec 03, 2008 2:13 pm    Post subject: Display Images in Access in Code Reply with quote

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.

Creating the table to store file and path data

1. Open the sample database, Northwind.mdb, or the sample project, NorthwindCS.adp.
2. Create the following table either in Northwind.mdb or in NorthwindCS.adp.

Code:
Table: tblImage
   ----------------------------
   Field Name: ImageID
   Data Type: AutoNumber
   Indexed: Yes (No Duplicates)

   Field Name: txtImageName
   Data Type: Text

   Table Properties: tblImage
   --------------------------
   PrimaryKey: ImageID


3. Open the tblImage table in Datasheet view, and then add the path and name of a bitmap file to each record. The following table of examples shows how the records might look:

Code:
--------------------------------------------------------------
| Type                 | Example                             |
--------------------------------------------------------------
| Absolute (Local)     | C:\Windows\Zapotec.bmp              |
| Absolute (UNC Path)  | \\Servername\sharename\Zapotec.bmp  |
| Relative             | Zapotec.bmp                         |
--------------------------------------------------------------


Creating the custom function
1.Create a new module, and then paste or type the following code:

Code:
Option Compare Database
Option Explicit

Public Function DisplayImage(ctlImageControl As Control, strImagePath As Variant) As String
On Error GoTo Err_DisplayImage

Dim strResult As String
Dim strDatabasePath As String
Dim intSlashLocation As Integer

With ctlImageControl
    If IsNull(strImagePath) Then
        .Visible = False
        strResult = "No image name specified."
    Else
        If InStr(1, strImagePath, "\") = 0 Then
            ' Path is relative
            strDatabasePath = CurrentProject.FullName
            intSlashLocation = InStrRev(strDatabasePath, "\", Len(strDatabasePath))
            strDatabasePath = Left(strDatabasePath, intSlashLocation)
            strImagePath = strDatabasePath & strImagePath
        End If
        .Visible = True
        .Picture = strImagePath
        strResult = "Image found and displayed."
    End If
End With
   
Exit_DisplayImage:
    DisplayImage = strResult
    Exit Function

Err_DisplayImage:
    Select Case Err.Number
        Case 2220       ' Can't find the picture.
            ctlImageControl.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


2. Save the module as Module1.

Using the custom function in a form
1. Create the following new form that is based on the tblImage table.

Code:
Form: frmImage
   ----------------------
   Caption: Image Form
   RecordSource: tblImage

   Image Control
   ---------------------------------
   Name: ImageFrame
   Picture: "C:\Windows\Zapotec.bmp"

   Text box
   ----------------------
   Name: txtImageID
   ControlSource: ImageID

   Text box
   ---------------------------
   Name: txtImageName
   ControlSource: txtImageName

   Text box
   ---------------------------
   Name: txtImageNote
   ControlSource: <Blank>


NOTE: If you do not want the path to appear in the form, you can set the Visible property of the txtImageName control to False.

2. On the View menu, click Code, and then paste or type the following code:

Code:
Option Compare Database
Option Explicit

Private Sub Form_AfterUpdate()
    CallDisplayImage
End Sub

Private Sub Form_Current()
    CallDisplayImage
End Sub

Private Sub txtImageName_AfterUpdate()
    CallDisplayImage
End Sub

Private Sub CallDisplayImage()
    Me!txtImageNote = DisplayImage(Me!ImageFrame, Me!txtImageName)
End Sub
   


3. Open the frmImage form in Form view. Note that the form displays the corresponding bitmap for each record. If the txtImageName field is blank or if the image cannot be found, you receive appropriate messages instead of the image frame.

Using the custom function in a report

1. Create the following new report that is based on the ImageTable table.

Code:
Report: rptImage
   ----------------------
   Caption: Image Report
   RecordSource: tblImage

   Image Control
   ---------------------------------
   Name: ImageFrame
   Picture: "C:\Windows\Zapotec.bmp"

   Text box
   ----------------------
   Name: txtImageID
   ControlSource: ImageID

   Text box
   ---------------------------
   Name: txtImageName
   ControlSource: txtImageName

   Text box
   ---------------------------
   Name: txtImageNote
   ControlSource: <Blank>


NOTE: If you do not want the path to appear in the report, you can set the Visible property of the txtImageName control to False.

2. On the View menu, click Code, and then paste or type the following code:

Code:
Option Compare Database
Option Explicit

Private Sub Detail_Print(Cancel As Integer, PrintCount As Integer)
    Me!txtImageNote = DisplayImage(Me!ImageFrame, Me!txtImageName)
End Sub


3. Open the rptImage report in print preview. Note that the report displays the corresponding bitmap for each record. If the txtImageName field is blank or if the image cannot be found, you receive appropriate messages instead of the image frame.
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic    Manufacturing Information Solutions Forum Index -> Microsoft Access All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group