Imdabaum
07-17-2006, 12:40 PM
:banghead: I am trying to create an easy way to upload an image. All photos go into a folder called Photos, pretty cut and dry. I then created a tblProjectPhotos with ProjectID, Caption, Location as fields. I have got code that writes the (supposed) location into the record along with the ProjectID that corresponds with the image. Does anyone know how I can copy an image from local machine to a Network address?
http://msdn2.microsoft.com/en-us/library/bwfbss93.aspx
"The path is not valid for one of the following reasons: it is a zero-length string, it contains only white space, it contains invalid characters, or it is a device path (starts with \\.\ (file://\\.\)).
Private Sub btnAdd_Click()
On Error GoTo Err_btnAdd_Click
Dim strAddress As String
Dim db As Database
Dim rst As Recordset
Dim txtTest As String
Dim I As Integer
Dim txtletter As String
Dim intLength As Integer
Dim SourceFile As String
Dim DestinationFile As String
Dim intImage As Integer ' 0=NO, no images, -1=YES, has images
Dim strPath As String ' Directory for all images
Dim strProjFile As String ' Directory with isolated images
Dim strImagePath As String ' Path comes from tblImagePath
Dim lngLen As Long ' Length of strImagePath
Dim Counter As Long
Dim sngStart As Single ' Used to calc time
Dim sngEnd As Single
Dim sngElapsed As Single
strImagePath = DLookup("[ImagePath]", "tblImagePath") ' Path to ISOLATED images from tblImagePath
lngLen = Len(strImagePath)
For I = lngLen To 1 Step -1 ' strPath = where ALL images are located
txtletter = Mid(strImagePath, I, 1)
If txtletter = "\" Then
Exit For
End If
Next I
strPath = Left(strImagePath, I - 1)
Set db = CurrentDb
' Set rst = db.OpenRecordset("tblPropertiesPhoto", dbOpenTable)
Set rst = db.OpenRecordset("tblPropertiesPhoto", dbOpenDynaset) ' SMRF 14746 - Ruth Radmall - 1 Apr 2002
strPropFile = strImagePath & "\" & Forms!frmPropertiesNew!PropertyNumber & "\* " 'order by propNum
' need to test if strPropFile already exists... if not create it, or prompt for approval to create
'
If Dir(strPropFile) = "" Then ' Test if already have isolated images
'sngStart = Timer ' Get start time.
' Why are we killing all .jpg file types? In strImagePath
'Kill strImagePath & "\*.jpg"
SourceFile = strPath & "\" & Forms!frmPropertiesNew!PropertyNumber & "* "
DestinationFile = strImagePath & "\"
intImage = CopyFile(SourceFile, DestinationFile) ' CopyFile in modMisc
' Wait about 6.8 seconds for CopyFile to finish (runs asynchronously)
DoCmd.Hourglass True
Counter = 0
Do While Counter < 15000000
Counter = Counter + 1 ' Increment Counter.
' If Counter = 14999999 Then ' If condition is True, and if it is false it exits one ms later? Point of this?
' Exit Do ' Exit loop.
End If
Loop
DoCmd.Hourglass False
' sngEnd = Timer ' Get end time.
' sngElapsed = Format(sngEnd - sngStart, "Fixed") ' Elapsed time.
' MsgBox ("It took " & sngElapsed _
& " seconds to run.")
End If
strAddress = GetOpenFile_TSB(strImagePath & "\", "Images for Selected Property") ' WORKS
If strAddress <> "" Then
Me.Image1.Picture = strAddress
txtTest = Right(strAddress, 74) ' SubjectName = 70 char + ".jpg"
For I = 74 To 1 Step -1
txtletter = Mid(txtTest, I, 1)
If txtletter = "\" Then
Exit For
End If
intLength = intLength + 1
Next I
Me.txtSubject = Mid(txtTest, I + 1, intLength - 4) ' Extracted Subject Name
With rst
.AddNew
!ProjectID = Forms.frmProjectNew.ID
!Caption = txtSubject
!location = strPath & "\" & txtSubject & ".jpg"
.Update
End With
UpdateList
End If
Exit_btnAdd_Click:
Exit Sub
Err_btnAdd_Click:
If Err.Number = 53 Then ' File not found
Resume Next
Else
MsgBox Err.Number & " " & Err.Description
Resume Exit_btnAdd_Click
End If
End Sub
The CopyFile Function goes something like this:
Function CopyFile(SourceFile As String, Destfile As String) As Integer
'************************************************************************** **********************
'PURPOSE: Copy a file on disk from one location to another.
'Accepts: The name of the source file and destination file.
'RETURNS: 0=NO, -1=YES
'LOCATED: modMisc
'************************************************************************** **********************
Dim copyString As String
CopyFile = 0 ' NO
If Dir(SourceFile) = "" Then
MsgBox Chr(34) & Right(SourceFile, 10) & Chr(34) & _
" There are no images for this property."
Else
SourceFile = Chr(34) & SourceFile & Chr(34)
Destfile = Chr(34) & Destfile & Chr(34)
copyString = "CMD.EXE /C Copy " & SourceFile & _
" " & Destfile
Call Shell(copyString, 0)
CopyFile = -1 ' YES
End If
End Function
http://msdn2.microsoft.com/en-us/library/bwfbss93.aspx
"The path is not valid for one of the following reasons: it is a zero-length string, it contains only white space, it contains invalid characters, or it is a device path (starts with \\.\ (file://\\.\)).
Private Sub btnAdd_Click()
On Error GoTo Err_btnAdd_Click
Dim strAddress As String
Dim db As Database
Dim rst As Recordset
Dim txtTest As String
Dim I As Integer
Dim txtletter As String
Dim intLength As Integer
Dim SourceFile As String
Dim DestinationFile As String
Dim intImage As Integer ' 0=NO, no images, -1=YES, has images
Dim strPath As String ' Directory for all images
Dim strProjFile As String ' Directory with isolated images
Dim strImagePath As String ' Path comes from tblImagePath
Dim lngLen As Long ' Length of strImagePath
Dim Counter As Long
Dim sngStart As Single ' Used to calc time
Dim sngEnd As Single
Dim sngElapsed As Single
strImagePath = DLookup("[ImagePath]", "tblImagePath") ' Path to ISOLATED images from tblImagePath
lngLen = Len(strImagePath)
For I = lngLen To 1 Step -1 ' strPath = where ALL images are located
txtletter = Mid(strImagePath, I, 1)
If txtletter = "\" Then
Exit For
End If
Next I
strPath = Left(strImagePath, I - 1)
Set db = CurrentDb
' Set rst = db.OpenRecordset("tblPropertiesPhoto", dbOpenTable)
Set rst = db.OpenRecordset("tblPropertiesPhoto", dbOpenDynaset) ' SMRF 14746 - Ruth Radmall - 1 Apr 2002
strPropFile = strImagePath & "\" & Forms!frmPropertiesNew!PropertyNumber & "\* " 'order by propNum
' need to test if strPropFile already exists... if not create it, or prompt for approval to create
'
If Dir(strPropFile) = "" Then ' Test if already have isolated images
'sngStart = Timer ' Get start time.
' Why are we killing all .jpg file types? In strImagePath
'Kill strImagePath & "\*.jpg"
SourceFile = strPath & "\" & Forms!frmPropertiesNew!PropertyNumber & "* "
DestinationFile = strImagePath & "\"
intImage = CopyFile(SourceFile, DestinationFile) ' CopyFile in modMisc
' Wait about 6.8 seconds for CopyFile to finish (runs asynchronously)
DoCmd.Hourglass True
Counter = 0
Do While Counter < 15000000
Counter = Counter + 1 ' Increment Counter.
' If Counter = 14999999 Then ' If condition is True, and if it is false it exits one ms later? Point of this?
' Exit Do ' Exit loop.
End If
Loop
DoCmd.Hourglass False
' sngEnd = Timer ' Get end time.
' sngElapsed = Format(sngEnd - sngStart, "Fixed") ' Elapsed time.
' MsgBox ("It took " & sngElapsed _
& " seconds to run.")
End If
strAddress = GetOpenFile_TSB(strImagePath & "\", "Images for Selected Property") ' WORKS
If strAddress <> "" Then
Me.Image1.Picture = strAddress
txtTest = Right(strAddress, 74) ' SubjectName = 70 char + ".jpg"
For I = 74 To 1 Step -1
txtletter = Mid(txtTest, I, 1)
If txtletter = "\" Then
Exit For
End If
intLength = intLength + 1
Next I
Me.txtSubject = Mid(txtTest, I + 1, intLength - 4) ' Extracted Subject Name
With rst
.AddNew
!ProjectID = Forms.frmProjectNew.ID
!Caption = txtSubject
!location = strPath & "\" & txtSubject & ".jpg"
.Update
End With
UpdateList
End If
Exit_btnAdd_Click:
Exit Sub
Err_btnAdd_Click:
If Err.Number = 53 Then ' File not found
Resume Next
Else
MsgBox Err.Number & " " & Err.Description
Resume Exit_btnAdd_Click
End If
End Sub
The CopyFile Function goes something like this:
Function CopyFile(SourceFile As String, Destfile As String) As Integer
'************************************************************************** **********************
'PURPOSE: Copy a file on disk from one location to another.
'Accepts: The name of the source file and destination file.
'RETURNS: 0=NO, -1=YES
'LOCATED: modMisc
'************************************************************************** **********************
Dim copyString As String
CopyFile = 0 ' NO
If Dir(SourceFile) = "" Then
MsgBox Chr(34) & Right(SourceFile, 10) & Chr(34) & _
" There are no images for this property."
Else
SourceFile = Chr(34) & SourceFile & Chr(34)
Destfile = Chr(34) & Destfile & Chr(34)
copyString = "CMD.EXE /C Copy " & SourceFile & _
" " & Destfile
Call Shell(copyString, 0)
CopyFile = -1 ' YES
End If
End Function