IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Access Discussion :

Ouverture d'un fichier stockée dans un champ image


Sujet :

Access

  1. #1
    Membre du Club
    Profil pro
    Inscrit en
    Juin 2005
    Messages
    179
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2005
    Messages : 179
    Points : 58
    Points
    58
    Par défaut Ouverture d'un fichier stockée dans un champ image
    Bonjour, j'utilie SQL Server en SRV et Access en client. Via MSDN, j'ai trouvé le code me permettant de stocker un fichier dans un champ image (blob).

    Ma question est la suivante : j'ai un formulaire qui me permet de sélectionner l'adresse du fichier et ensuite de l'enregistrer dans ma DB.

    Ensuite, dans un autre formulaire je souhaiterai que lorsque je sélectionne un fichier celui-ci se lance dans l'appli correspondante (ex. fichier word dans word, fichier zip dans winzip...).

    Mais là j'avoue ne pas savoir faire. Qqn a t il une idée ?

    Ci-dessous mon code MSDN m'ayant permis de stocker mes fichier, c'est la fonction Read.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
     
    Option Compare Database
    Option Explicit
     
    Const BLOCKSIZE = 32768
     
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    'FUNCTION: fncReadBLOB()
    '
    'PURPOSE:
    '   Reads a BLOB from a file and stores it in specified table and field.
    '
    'PREREQUISITES:
    '   Table with the Image field to contain the binary data must
    '   be opened using Visual Basic for Applications code and the correct
    '   record navigated to, prior to calling the fncReadBLOB() function.
    '
    'ARGUMENTS:
    '   strSource - Path and filename of external file to be read and stored.
    '   rstTable - The table object to store the data in.
    '   strField - The Image field in table rstTable to store the data in.
    '
    'RETURN:
    '   The number of bytes read from the Source file.
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Function fncReadBLOB(strSource As String, rstTable As ADODB.Recordset, _
        strField As String)
     
        Dim intNumBlocks As Integer, intSourceFile As Integer, intI As Integer
        Dim lngFileLength As Long, lngLeftOver As Long
        Dim strFileData As String
        Dim varRetVal As Variant
     
        On Error GoTo Err_ReadBLOB
     
        'Open the source file.
        intSourceFile = FreeFile
        Open strSource For Binary Access Read As intSourceFile
     
        'Get the length of the file.
        lngFileLength = LOF(intSourceFile)
     
        'File is invalid if length equals zero.
        If lngFileLength = 0 Then
            fncReadBLOB = 0
            Exit Function
        End If
     
        'Calculate the number of blocks to read and the leftover bytes.
        intNumBlocks = lngFileLength \ BLOCKSIZE
        lngLeftOver = lngFileLength Mod BLOCKSIZE
     
        'Read the leftover data, writing it to the table.
        strFileData = String$(lngLeftOver, 32)
     
        'Read data from the external file.
        Get intSourceFile, , strFileData
     
        'Write the data to the Image field.
        rstTable(strField).AppendChunk (strFileData)
     
        'Read the remaining blocks of data, writing them to the table.
        strFileData = String$(BLOCKSIZE, 32)
     
        For intI = 1 To intNumBlocks
            Get intSourceFile, , strFileData
            rstTable(strField).AppendChunk (strFileData)
        Next intI
     
        'Update the record and terminate the function.
        rstTable.Update
        Close intSourceFile
        fncReadBLOB = lngFileLength
        Exit Function
     
    Err_ReadBLOB:
        fncReadBLOB = -Err
        Exit Function
     
    End Function
     
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    'FUNCTION: fncWriteBLOB()
    '
    'PURPOSE:
    '   Writes the BLOB stored in table and field to specified disk file.
    '
    'PREREQUISITES:
    '   Table with the Image field containing the binary data must be opened
    '   using Visual Basic for Applications code and the correct record
    '   navigated to prior to calling the fncWriteBLOB() function.
    '
    'ARGUMENTS:
    '   rstTable - The table object containing the binary information.
    '   strField - Image field in table containing binary information to
    '              write.
    '   strDestination - Path and filename to write the binary information to.
    '
    'RETURN:
    '   The number of bytes written to the destination file.
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Function fncWriteBLOB(rstTable As ADODB.Recordset, strField As String, _
        strDestination As String)
     
        Dim intNumBlocks As Integer, intDestFile As Integer, intI As Integer
        Dim lngFileLength As Long, lngLeftOver As Long
        Dim strFileData As String
        Dim varRetVal As Variant
     
        On Error GoTo Err_WriteBLOB
     
        'Get the size of the field.
        lngFileLength = rstTable(strField).ActualSize
     
        'Cancel if field is empty.
        If lngFileLength = 0 Then
            fncWriteBLOB = 0
            Exit Function
        End If
     
        'Calculate number of blocks to write and the leftover bytes.
        intNumBlocks = lngFileLength \ BLOCKSIZE
        lngLeftOver = lngFileLength Mod BLOCKSIZE
     
        'Create pointer for to destination file.
        intDestFile = FreeFile
        Open strDestination For Output As intDestFile
        Close intDestFile
     
        'Open the destination file.
        Open strDestination For Binary As intDestFile
     
        'Write the leftover data to the output file.
        strFileData = rstTable(strField).GetChunk(lngLeftOver)
     
        'Write data to the external file.
        Put intDestFile, , strFileData
     
        'Read the leftover chunks and write it to output file.
        For intI = 1 To intNumBlocks
            strFileData = rstTable(strField).GetChunk((intI - 1) * _
              BLOCKSIZE + lngLeftOver)
            Put intDestFile, , strFileData
        Next intI
     
        'Close the external file and terminate the function.
        Close intDestFile
        fncWriteBLOB = lngFileLength
        Exit Function
     
    Err_WriteBLOB:
        If Err.Number = 94 Then
            Resume Next
        Else
            fncWriteBLOB = -Err
            Exit Function
        End If
    End Function
     
    Sub test()
     
        Dim varBytesRead As Variant, varBytesWritten As Variant
        Dim strMsg As String
        Dim Conn As New ADODB.Connection
        Dim rstTable As New ADODB.Recordset
     
        'Create connection and open the tblBlob table.
        Set Conn = CurrentProject.Connection
        rstTable.Open "FICHIER", Conn, adOpenDynamic, adLockOptimistic
     
        'Create a new record and move to it.
        rstTable.AddNew
        rstTable("Nom") = "test"
        rstTable.Update
     
        'Call the Read Blob function.
        varBytesRead = fncReadBLOB("c:\test.doc", rstTable, "Fiche")
     
     
     
        End Sub

  2. #2
    Expert éminent
    Avatar de LedZeppII
    Homme Profil pro
    Maintenance données produits
    Inscrit en
    Décembre 2005
    Messages
    4 485
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Yvelines (Île de France)

    Informations professionnelles :
    Activité : Maintenance données produits
    Secteur : Distribution

    Informations forums :
    Inscription : Décembre 2005
    Messages : 4 485
    Points : 7 768
    Points
    7 768
    Par défaut
    Bonjour,

    ça doit pouvoir se faire avec la fonction ShellExecute de l'API.

    A+

Discussions similaires

  1. Réponses: 7
    Dernier message: 12/10/2006, 08h44
  2. Réponses: 1
    Dernier message: 18/05/2006, 12h52
  3. lire/écrire un fichier stocké dans le JAR ?
    Par SheikYerbouti dans le forum Entrée/Sortie
    Réponses: 11
    Dernier message: 24/03/2006, 10h37
  4. Contenu fichier stocké dans une base de données
    Par t_om84 dans le forum Général Python
    Réponses: 20
    Dernier message: 02/03/2006, 11h45
  5. Fichier texte dans un champs
    Par pobrouwers dans le forum Access
    Réponses: 4
    Dernier message: 25/02/2006, 13h10

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo