|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Hello All,
First I want to say I have been referencing this fourm for sometime now and really appreciate everyones very helpfull posts. I have been able to find the answers to many questions by searching long and hard, but finally I have something that requires me to post: I am using the following Module to call an API to allow users to select a file to attach into an Access 2000 program I'm running. Problem is this feature works fine if a user highlights a file and selects 'OK'. It also works good if the user selects 'Cancel'. The problem is if a user highlights a Folder instead of a file and Clicks 'OK' the whole access database crashes out completely and exits back to the PC desktop with no error message. Is there some way I can do validation and say if this module returns a folder only not to run any further? Or allow only for file selection only.. Thanks Module 1 Code:
Option Compare Database
Option Explicit
'This funtion is what launches the browse window
Public Const MAX_PATH = 260
Public Const BIF_BROWSEINCLUDEFILES = &H4000
Private Type BrowseInfo
hWndOwner As Long
pIDLRoot As Long
pszDisplayName As Long
lpszTitle As Long
ulFlags As Long
lpfnCallback As Long
lParam As Long
iImage As Long
End Type
Public Declare Sub CoTaskMemFree _
Lib "ole32.dll" _
(ByVal hMem As Long)
Public Declare Function GetDesktopWindow _
Lib "user32" () As Long
Public Declare Function lstrcat _
Lib "kernel32" _
Alias "lstrcatA" _
(ByVal lpString1 As String, _
ByVal lpString2 As String) As Long
Public Declare Function SHBrowseForFolder _
Lib "shell32" _
(lpbi As BrowseInfo) As Long
Public Declare Function SHGetPathFromIDList _
Lib "shell32" _
(ByVal pidList As Long, _
ByVal lpBuffer As String) As Long
Public Function SelectDir(strTitle As String) As String
Dim PathLen As Integer
Dim lpIDList As Long
Dim RetCode As Long
Dim DirPath As String
Dim BFFInfo As BrowseInfo
With BFFInfo
.hWndOwner = GetDesktopWindow
.lpszTitle = lstrcat(strTitle, Chr$(0))
.ulFlags = BIF_BROWSEINCLUDEFILES
End With
DirPath = ""
lpIDList = SHBrowseForFolder(BFFInfo)
If lpIDList Then
DirPath = String$(MAX_PATH, 0)
RetCode = SHGetPathFromIDList(lpIDList, DirPath)
Call CoTaskMemFree(lpIDList)
PathLen = InStr(DirPath, vbNullChar)
If PathLen Then
DirPath = Left$(DirPath, PathLen - 1)
End If
End If
SelectDir = DirPath
End Function
Module 2 Code:
Option Compare Database
'This module Parses the String path to the file attachement
Function CountCSWords(ByVal s) As Integer
'Counts the words in a string that are separated by commas.
Dim WC As Integer, Pos As Integer
If VarType(s) <> 8 Or Len(s) = 0 Then
CountCSWords = 0
Exit Function
End If
WC = 1
Pos = InStr(s, "\")
Do While Pos > 0
WC = WC + 1
Pos = InStr(Pos + 1, s, "\")
Loop
CountCSWords = WC
End Function
Module 3 Code:
Option Compare Database
'THis module returns the filename of the uploaded attachment
Function GetCSWord(ByVal s, Indx As Integer)
'Returns the nth word in a specific field.
Dim WC As Integer, Count As Integer
Dim SPos As Integer, EPos As Integer
WC = CountCSWords(s)
If Indx < 1 Or Indx > WC Then
GetCSWord = Null
Exit Function
End If
Count = 1
SPos = 1
For Count = 2 To Indx
SPos = InStr(SPos, s, "\") + 1
Next Count
EPos = InStr(SPos, s, "\") - 1
If EPos <= 0 Then EPos = Len(s)
GetCSWord = Trim(Mid(s, SPos, EPos - SPos + 1))
End Function
And here is an example of the ON_Click event i'm running on my form which displays the Browse window by callilng all of the above modules: Code:
Private Sub cmdBrowse_Click()
'This button is the button which allows a user to include an attachment of any file type.
'It then saves this attachment to the location T:\Data Processing\PCAdmin\service2\KO # HERE\Issue\FILENAME.XXX
Dim strFolder As String
'call module 1 which pops up the file selection box
strFolder = SelectDir("c")
'This will display the source location on the form for the attachment.
txtIssueSource.SetFocus
txtIssueSource.Text = strFolder
If strFolder <> "" Then
'This function will add in the filecopy: count # of words in sourcename
Dim I As Integer
Dim intCnt As Integer
Dim strFinalDest As String
'Find out how many \ separated words are present
intCnt = CountCSWords(strFolder)
'Now call the 3rd module function to retrieve each one in turn
I = intCnt
strFinalDest = GetCSWord(strFolder, I)
'copy source file to network location
Dim DestinationFile As String
Dim strIDValue As String
strIDValue = txtID.value
'state the destination save folder for the attachment
DestinationFile = "T:\Data Processing\PCAdmin\service2\KO\" + strIDValue + "\Issue\" + strFinalDest
FileCopy strFolder, DestinationFile
'This will copy the saved location to the database
Forms!frmData!AttachmentIssue = DestinationFile
MsgBox "Attachment has been Added. To add another Attachment just repeat the process."
Else
End If
End Sub
All most positive the error lies somewhere in the On_Click event. I think my program is crashing because this On_click event is trying to save a whole folder instead of a individual file. I need some sort of validation to basically not continue the On_click event if the module returns a Folder selected only and not a individual File. Any help would be greatley Appreciated!![CODE] |
|
#2
|
|||
|
|||
|
You can do a check by calling the FileSystem methods of isFile(). If this returns true process on, otherwise inform the user.
__________________
El éxito consiste en una serie de pequeñas victorias día a día MySQL, MS SQL, MS ACCESS, Oracle Database Manager - http://victorpendleton.net/products/psdviewer.html |
|
#3
|
|||
|
|||
|
Thanks, Anyway you could give me an example of isFile() in use? I am honestly new to the VB side of access. I have been trying to learn as much as I can, but I get confused with using Macros and calling functions... If not I will try searching using the "isFile()" as a reference. Thank you for your reply!!!
|
|
#4
|
|||
|
|||
|
if ( isFile( file_name ) = false ) then
'Tell the user about it else 'go ahead end if |
|
#5
|
|||
|
|||
|
DestinationFile = "T:\Data Processing\PCAdmin\service2\KO\" + strIDValue + "\Issue\" + strFinalDest
The blue part must be an exsiting path. The strIDValue (assigned as string) comes from txtID.value (numeric?). This must be an existing folder. strFinalDest is the file name or in case of misstake a folder name. The folder name should generate a run-time error messagebox. I would do like this. Code:
'state the destination save folder for the attachment
DestinationFile = "T:\Data Processing\PCAdmin\service2\KO\" + strIDValue + "\Issue\" + strFinalDest
On Error GoTo Error_message
FileCopy strFolder, DestinationFile
On Error GoTo 0
'This will copy the saved location to the database
Forms!frmData!AttachmentIssue = DestinationFile
MsgBox "Attachment has been Added. To add another Attachment just repeat the process."
Out:
Else
End If
Exit Sub
Error_message:
MsgBox "Error = " & Err.Number & ": " & Err.Description
OnError GoTo 0
GoTo Out
End Sub
Last edited by minor28 : January 28th, 2004 at 08:29 AM. |
|
#6
|
|||
|
|||
|
Yes strIDValue is a String value for a pre-exisitng folder name. the strFinalDest is the file name and in case of a folder it is not a filename which causes my application to crash.
I will try this code and get back to you. I tried the code Victor sugested, but then I find even when I click on Add Attachment in my program it is crashening right away. Does anyone know what the string strFinalDest looks like when holding a folder value? Is it set to NULL or is it actually holding just for example "Desktop" for a folder name as a string? Saying I could just check the string for a . and if so then continue and if not than end sub? thanks so much for all of your replies!! |
|
#7
|
|||
|
|||
|
strFinalDest holds the name of the folder. As I said, the folder name in FileCopy will cause a run-time error. In place of my suggested error message you can place whatever message you like and goto whatever address you like.
Last edited by minor28 : January 28th, 2004 at 08:40 AM. |
|
#8
|
|||
|
|||
|
Thanks minor 28. The error handling works great and fixes all my issues!! I added a custom msgbox to the error_message. You have saved me!!!
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > Visual Basic Programming > Problem with File Browse API Crashing Access Application |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|