폴더의 이미지를 폼, 보고서 또는 데이터 액세스 페이지로 표시하는 방법

원래 KB 번호: 285820

참고

전문 코딩, 상호 운용성 및 다중 사용자 기술이 필요합니다. 이 문서는 Microsoft Access 데이터베이스(.mdb/.accdb) 및 Microsoft Access 프로젝트(.adp)에 적용됩니다.

요약

Microsoft Access 테이블에 이미지를 저장하는 것은 실용적이지 않은 경우가 있습니다. 이미지가 많거나 각 이미지 파일이 큰 경우 Microsoft Access 데이터베이스 파일의 크기가 빠르게 증가할 수 있습니다.

이 문서에서는 다음을 위해 사용할 수 있는 사용자 지정 함수를 보여 줍니다.

  • 파일 경로 및 이미지 이름을 테이블에 저장합니다.
  • 이미지 컨트롤을 사용하여 이미지를 표시합니다.
  • 사용할 수 있는 이미지가 없으면 이미지 컨트롤을 숨깁니다.
  • 이미지의 표시 상태 대한 피드백을 제공합니다.

이 문서에는 데이터 액세스 페이지에 이미지를 표시하는 데 사용할 수 있는 샘플 Visual Basic 스크립트도 포함되어 있습니다.

참고

이 예제에서는 비트맵 이미지(.bmp)를 사용하지만 .jpg, .pcx 및 .gif 같은 다른 이미지 형식을 사용할 수도 있습니다.

Microsoft에서 제공하는 프로그래밍 예제는 예시를 위한 것일 뿐이며 이와 관련하여 명시적이거나 묵시적인 어떠한 보증도 하지 않습니다. 이는 상품성이나 특정 목적에 대한 적합성의 묵시적인 보증을 포함하며 이에 제한되지 않습니다. 이 문서에서는 예제에 사용되고 있는 프로그래밍 언어와 프로시저를 만들고 디버깅하는 데 사용되는 도구를 사용자가 잘 알고 있는 것으로 가정합니다. Microsoft 지원 엔지니어는 사용자에게 도움이 되도록 특정 프로시저에 대한 기능을 설명할 수 있지만 사용자의 특정 요구 사항에 맞도록 예제를 수정하여 추가 기능을 제공하거나 프로시저를 구성하지는 않습니다.

파일 및 경로 데이터를 저장할 테이블 만들기

  1. 샘플 데이터베이스, Northwind.mdb 또는 샘플 프로젝트인 NorthwindCS.adp를 엽니다.

  2. Northwind.mdb 또는 NorthwindCS.adp에서 다음 테이블을 만듭니다.

    In Northwind.mdb:

    Table: tblImage
    ----------------------------
    Field Name: ImageID
    Data Type: AutoNumber
    Indexed: Yes (No Duplicates)
    
    Field Name: txtImageName
    Data Type: Text
    
    Table Properties: tblImage
    --------------------------
    PrimaryKey: ImageID
    

    NorthwindCS.adp에서:

    Table: tblImage
    -----------------------
    Column Name: ImageID
    Datatype: Int
    Allow Nulls: Unchecked
    Identity: Yes
    
    Column Name: txtImageName
    Datatype: varchar
    
    Table Properties: ImageTable
    -------------------------------
    Primary Key Constraint: ImageID
    
  3. 데이터시트 보기에서 tblImage 테이블을 연 다음 비트맵 파일의 경로와 이름을 각 레코드에 추가합니다. 다음 예제 표에서는 레코드의 모양을 보여 줍니다.

    유형 예시
    Absolute(로컬) C:\Windows\Zapotec.bmp
    Absolute(UNC 경로) \\Servername\sharename\Zapotec.bmp
    상대 Zapotec.bmp

사용자 지정 함수 만들기

  1. 새 모듈을 만든 다음 다음 코드를 붙여넣거나 입력합니다.

    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. 모듈을 Module1로 저장합니다.

양식에서 사용자 지정 함수 사용

  1. tblImage 테이블을 기반으로 하는 다음 새 양식을 만듭니다.

    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>
    

    참고

    폼에 경로를 표시하지 않으려면 컨트롤의 txtImageName 속성을 False로 설정할 Visible 수 있습니다.

  2. 보기 메뉴에서 코드를 클릭한 다음 다음 코드를 붙여넣거나 입력합니다.

    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. 폼 보기에서 frmImage 양식을 엽니다. 폼에는 각 레코드에 해당하는 비트맵이 표시됩니다. 필드가 txtImageName 비어 있거나 이미지를 찾을 수 없는 경우 이미지 프레임 대신 적절한 메시지를 받습니다.

보고서에서 사용자 지정 함수 사용

  1. ImageTable 테이블을 기반으로 하는 다음 새 보고서를 만듭니다.

    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>
    

    참고

    보고서에 경로를 표시하지 않으려면 컨트롤의 txtImageName 속성을 False로 설정할 Visible 수 있습니다.

  2. 보기 메뉴에서 코드를 클릭한 다음 다음 코드를 붙여넣거나 입력합니다.

    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. 인쇄 미리 보기에서 rptImage 보고서를 엽니다. 보고서에는 각 레코드에 해당하는 비트맵이 표시됩니다. 필드가 txtImageName 비어 있거나 이미지를 찾을 수 없는 경우 이미지 프레임 대신 적절한 메시지를 받습니다.

데이터 액세스 페이지에서 사용자 지정 함수 복제

  1. tblImage 테이블을 기반으로 하는 다음 새 데이터 액세스 페이지를 만듭니다.

    Data Access Page: dapImage
    -----------------------------
    Title: Image Data Access Page
    
    Image Control
    ---------------------------------
    ID: ImageFrame
    
    Text box
    ----------------------
    ID: txtImageID
    ControlSource: ImageID
    
    Text box
    ---------------------------
    ID: txtImageName
    ControlSource: txtImageName
    

    참고

    경로가 페이지에 표시되지 않도록 하려면 컨트롤의 txtImageName 속성을 Hidden으로 설정할 Visibility 수 있습니다.

  2. 도구 메뉴에서 매크로를 가리킨 다음 Microsoft Script Editor 클릭합니다.

  3. HTML 문서의 HEAD 태그 부분에 있는 MSODSC의 Current 이벤트에 다음 스크립트를 추가합니다.

    참고

    이벤트를 트리거하려면 매개 변수를 전달해야 합니다.

    <SCRIPT language=vbscript event=Current(oEventInfo) for=MSODSC>
    <!--
    ImageFrame.src=txtImageName.value
    -->
    </SCRIPT>
    
  4. 페이지 보기에서 dapImage 페이지를 엽니다. 페이지에는 각 레코드에 해당하는 비트맵이 표시됩니다. txtImageName 필드가 비어 있으면 컨트롤 아이콘이 표시됩니다. 이미지를 찾을 수 없는 경우 이미지 컨트롤에 X 아이콘이 나타납니다.

양식에서 http:// 경로 사용

양식에서 http:// 경로를 사용하려면 다음과 같이 웹 브라우저 컨트롤(shdocvw.dll)을 사용합니다.

  1. 양식에 Microsoft 웹 브라우저 컨트롤을 추가하고 이름을 WebBrowser로 지정합니다.

  2. 모듈에 다음 코드를 추가합니다.

    Public Function DisplayImageWeb(ctlBrowserControl As Control, _
    strImagePath As Variant)
    
    On Error GoTo Err_DisplayImage
    
    Dim strDatabasePath As String
    Dim intSlashLocation As Integer
    
    With ctlBrowserControl
     If IsNull(strImagePath) Then
     ElseIf Left(strImagePath, 4) = "http" Then
         .Navigate (strImagePath)
     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
         .Navigate (strImagePath)
     End If
    End With
    
    Exit_DisplayImage:
     Exit Function
    
    Err_DisplayImage:
     Select Case Err.Number
         Case Else
             MsgBox Err.Number & " " & Err.Description
             Resume Exit_DisplayImage:
     End Select
    End Function
    
  3. 양식 뒤에 다음 코드를 추가합니다.

    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()
     DisplayImageWeb Me.WebBrowser9, Me.txtImageName
    End Sub