Visual Basic Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreVisual Basic Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 27th, 2004, 03:06 PM
jamesiv jamesiv is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Posts: 4 jamesiv User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Angry Problem with File Browse API Crashing Access Application

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]

Reply With Quote
  #2  
Old January 27th, 2004, 04:04 PM
victorpendleton victorpendleton is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jan 2003
Location: No es importante
Posts: 2,065 victorpendleton User rank is Private First Class (20 - 50 Reputation Level)victorpendleton User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 6 h 50 m 52 sec
Reputation Power: 8
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

Reply With Quote
  #3  
Old January 27th, 2004, 04:24 PM
jamesiv jamesiv is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Posts: 4 jamesiv User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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!!!

Reply With Quote
  #4  
Old January 27th, 2004, 04:27 PM
victorpendleton victorpendleton is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jan 2003
Location: No es importante
Posts: 2,065 victorpendleton User rank is Private First Class (20 - 50 Reputation Level)victorpendleton User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 6 h 50 m 52 sec
Reputation Power: 8
if ( isFile( file_name ) = false ) then
'Tell the user about it
else
'go ahead
end if

Reply With Quote
  #5  
Old January 27th, 2004, 05:08 PM
minor28 minor28 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Location: Sweden
Posts: 245 minor28 User rank is Sergeant (500 - 2000 Reputation Level)minor28 User rank is Sergeant (500 - 2000 Reputation Level)minor28 User rank is Sergeant (500 - 2000 Reputation Level)minor28 User rank is Sergeant (500 - 2000 Reputation Level)minor28 User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 h 6 m 11 sec
Reputation Power: 14
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.

Reply With Quote
  #6  
Old January 28th, 2004, 08:21 AM
jamesiv jamesiv is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Posts: 4 jamesiv User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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!!

Reply With Quote
  #7  
Old January 28th, 2004, 08:32 AM
minor28 minor28 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Location: Sweden
Posts: 245 minor28 User rank is Sergeant (500 - 2000 Reputation Level)minor28 User rank is Sergeant (500 - 2000 Reputation Level)minor28 User rank is Sergeant (500 - 2000 Reputation Level)minor28 User rank is Sergeant (500 - 2000 Reputation Level)minor28 User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 h 6 m 11 sec
Reputation Power: 14
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.

Reply With Quote
  #8  
Old January 28th, 2004, 08:47 AM
jamesiv jamesiv is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Posts: 4 jamesiv User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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!!!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreVisual Basic Programming > Problem with File Browse API Crashing Access Application


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT