Upload a photo

B

Brad

I would like to add a button on my form that will open the save dialog box
and allow a user to select a picture from any location then it will save the
photo to a directory I specified and put a link to the picture in the table
that the form is linked to. The code to do this is fairly simple in excel but
I'm not sure how it translates to access. This is the code I would use in
excel, is there an equivalent in access?

Dim picFileName As String ' User selected location of picture file
Dim destFile As String ' Destination location to copy user selected picture
to
Dim num As String ' Issue_num (used as unique identifier for each photo)

destFile = "C:\Pictures." & num & ".jpg"
picFileName = Application.GetSaveAsFilename
'==They have cancelled.
If picFileName = "False" Then Exit Sub
'==Save the picture
FileCopy picFileName, destFile

I would like to use the record number as my num variable and place the
destFile in the corresponding field in my table. Thanks in advance for any
direction/help with this.
 
D

DStegon via AccessMonster.com

Brad said:
I would like to add a button on my form that will open the save dialog box
and allow a user to select a picture from any location then it will save the
photo to a directory I specified and put a link to the picture in the table
that the form is linked to. The code to do this is fairly simple in excel but
I'm not sure how it translates to access. This is the code I would use in
excel, is there an equivalent in access?


I just gave a similar issue this repsonse. Read through it and it should
answer most of your questions. You will have to change table and field names
to match your of course but the general code is there

Are you actually storing the picture in the database??? Why? Why not store
the pictures in a picture folder on the computer and then store the path to
the picture in your database. The user scans the picture and saves it to the
folder where all the sketches and drawings are stored. In the form have the
field for the path to the picture ( you can use UNC or if on the same
computer "C:\blah\blah.jpg"). Place an imagecontrol on the form and have the
program use the picture path to update and display the image. You can have a
Common Dialog box appear on a command button or on entering the path field
(test that it is blank) and have the person pick from the actual picture file.

I will use a ProdID as the Product (drawingID).... hopefully you can follow.

Private Sub ChangePicture(ProdID As Long)
On Error GoTo HandleErr

On Error Resume Next
Dim FileText As String
Dim cdl As CommonDlg
Dim rst As New ADODB.Recordset

Set cdl = New CommonDlg

With cdl
.DialogTitle = "Product Picture"
.hWndOwner = Me.hWnd
.InitDir = "\\YOUR_COMPUTERS_NETOWRK_NAME\pictures" 'Use "C:\pictures\....
" if on same computer
.Filter = "JPEG File Interchange Format (*.jpg)" & Chr(0) & "*.jpg; *.
jpeg" & Chr(0)
.DefaultExt = "jpg"
Call .ShowOpen
FileText = .FileName
End With

If FileText <> "" Then
With rst 'This is a separate table the holds the ProdID and the Path to
the item. Use relational table just in case not all prods have a picture
.Open "Select * FROM tbl_Product_File WHERE Product_ID=" & ProdID,
CurrentProject.Connection, adOpenKeyset, adLockOptimistic
If .eof Then .AddNew
!Product_ID = ProdID
!File_Path = FileText
.update
.Close
End With
End If

GetPicture ProdID

exithere:

Exit Sub

HandleErr:
If QueueError(Err) Then GoTo exithere: On Error GoTo 0: Resume

End Sub

Private Sub GetPicture(ProdID As Long)
Dim rst As New ADODB.Recordset
Dim strFilePath As String

On Error GoTo HandleErr

With rst
.Open "Select * From tbl_Product_File WHERE ((Product_File_Type_ID = 1)
and (Product_ID=" & ProdID & "))", CurrentProject.Connection, adOpenKeyset,
adLockOptimistic
If Not .eof Then
strFilePath = !File_Path
Else
strFilePath = ""
End If
.Close
End With

DisplayImage Me.PictureFrameSearch, strFilePath

exithere:
Exit Sub

HandleErr:
If QueueError(Err) Then GoTo exithere
Resume
End Sub

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 = "Not Found"
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 = "Found"
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 = "Not Found"
Resume Exit_DisplayImage:
Case Else ' Some other error.
MsgBox Err.Number & " " & Err.Description
strResult = "Not Found"
Resume Exit_DisplayImage:
End Select
End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top