Hi all,
My primary form has a image box on it for a logo. My subform has a document management form on it for adding documents which all worked great until
I added another subform BusinessContacts which also has a image box on it. IN any event, I have spent the better part of this past week trying to fix this as
I keep getting an error invalid use of null. I do believe the issue is within the added code. See below. I also copied the entire function to below it too for reference.
Basically, If I am on Primary form, and I select a file for a business Logo, then it stores in a folder attached to where my db is stored. If I select a document then that gets stored in a document folder in the business folder, and If i select a business contact MyPhoto, then it should store in the businesscontact folder and contact id...
If when i call for one of the DocTypes, it shouldnt want info from a different folder then the one I am looking for but it does. Hope this makes sense.
I am pretty sure this is the issue but I am not sure how to correct it?
Code:
Profile = GetProfile()
DataFolder = Profile & "\Data"
BusinessFolder = DataFolder & "\Business"
BusinessIDFolder = BusinessFolder & "\" & BusinessID
DocumentFolder = BusinessIDFolder & "\Documents"
'I added this below
BusinessContactFolder = BusinessIDFolder & "\BusinessContact"
BusinessContactIDFolder = BusinessContactFolder & "\" & BusinessContactID
' I believe this is where the issue is in the code........
CheckFolder Profile
CheckFolder DataFolder
CheckFolder BusinessFolder
CheckFolder BusinessIDFolder
If Not CheckFolder(DocumentFolder) Then 'Then Exit Sub was origional
' I added this below
ElseIf Not CheckFolder(BusinessContactFolder) Then
ElseIf Not CheckFolder(BusinessContactIDFolder) Then Exit Sub
End If
This is the full function....
Code:
Public Function CheckFolder(FolderName) As Boolean
Dim FN As String
FN = FolderName
CheckFolder = True
If Not FolderExists(FN) Then
MkDir FolderName
If Not FolderExists(FN) Then
MsgBox "ERROR! Could not create " & FN
CheckFolder = False
End If
End If
End Function
Public Sub SelectFile(DocType As String)
Dim Profile As String
Dim DataFolder As String
Dim BusinessFolder As String
Dim BusinessIDFolder As String
Dim DocumentFolder As String
Dim BusinessID As Long
Dim FD As Office.FileDialog
Dim PickFile As Variant
Dim FileName As String, FilePath As String
Dim NewFilePath As String
Dim NewFilename As String
' I added this below
Dim BusinessContactFolder As String
Dim BusinessContactIDFolder As String
Dim BusinessContactID As Long
Dim BusinessDocumentID As Long
If IsLoaded("frmBusiness") Then
BusinessID = Forms!frmBusiness!BusinessID
Else
BusinessID = 0
End If
Profile = GetProfile()
DataFolder = Profile & "\Data"
BusinessFolder = DataFolder & "\Business"
BusinessIDFolder = BusinessFolder & "\" & BusinessID
DocumentFolder = BusinessIDFolder & "\Documents"
'I added this below
BusinessContactFolder = BusinessIDFolder & "\BusinessContact"
BusinessContactIDFolder = BusinessContactFolder & "\" & BusinessContactID
CheckFolder Profile
CheckFolder DataFolder
CheckFolder BusinessFolder
CheckFolder BusinessIDFolder
If Not CheckFolder(DocumentFolder) Then 'Then Exit Sub was origional
' I added this below
ElseIf Not CheckFolder(BusinessContactFolder) Then
ElseIf Not CheckFolder(BusinessContactIDFolder) Then Exit Sub
End If
Randomize
Set FD = Application.FileDialog(msoFileDialogFilePicker)
With FD
.InitialFileName = Profile
.AllowMultiSelect = IIf(DocType = "BulkAdd", True, False)
.Title = "Select a File"
.Filters.Clear
'Not sure if I need and AND or and OR between the Business and BusinessContact?"
If DocType = "Business" Or "BusinessContact" Then
.Filters.Add "JPG Files", "*.jpg"
.Filters.Add "GIF Files", "*.gif"
.Filters.Add "PNG Files", "*.png"
Else
.Filters.Add "All Files", "*.*"
End If
If .Show = True Then
For Each PickFile In .SelectedItems
FilePath = PickFile
FileName = FileNameFromPath(FilePath)
NewFilename = Format(Now(), "yymmdd") & "-" & Format(Int(Rnd * 1000), "0000") & "-" & FileName
If DocType = "Business" Then
Forms!frmBusiness!MyLogo = NewFilename
NewFilePath = BusinessIDFolder & "\" & NewFilename
ElseIf DocType = "BusinessContact" Then
Forms!frmBusiness!sfrmBusinessContact.Form!MyPhoto = NewFilename
NewFilePath = BusinessContactIDFolder & "\" & NewFilename
ElseIf DocType = "Document" Then
Forms!frmBusinessDocument!FileName = NewFilename
Forms!frmBusinessDocument!Description = NewFilename
NewFilePath = DocumentFolder & "\" & NewFilename
Forms!frmBusinessDocument.RefreshDocImage
ElseIf DocType = "DocumentSubform" Then
Forms!frmBusiness!sfrmBusinessContact.Form!FileName = NewFilename
Forms!frmBusiness!sfrmBusinessContact.Form!Description = NewFilename
NewFilePath = DocumentFolder & "\" & NewFilename
Forms!frmBusiness!sfrmBusinessContact.Form.RefreshDocImage
Else ' Bulk Add
DoCmd.SetWarnings False
DoCmd.RunSQL "INSERT INTO tblBusinessDocument (BusinessID, Filename, Description) " & _
"VALUES (" & BusinessID & ", """ & NewFilename & """,""" & NewFilename & """)"
DoCmd.SetWarnings True
NewFilePath = DocumentFolder & "\" & NewFilename
End If
FileCopy FilePath, NewFilePath
If Not FileExists(NewFilePath) Then
MsgBox "ERROR: File Copy Failed. " & NewFilePath
Exit Sub
End If
'Kill FilePath
'If FileExists(FilePath) Then
' MsgBox "Could not delete original picture. " & NewFilePath
'End If
Next
End If
End With
End Sub
Public Function FileNameFromPath(FilePath As String) As String
FileNameFromPath = Right(FilePath, Len(FilePath) - InStrRev(FilePath, "\"))
End Function