Results 1 to 5 of 5
  1. #1
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Niagara Falls, New York, USA
    Posts
    1,877
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Length of Path and File Name? (a2k (9.0.6926) SP-3 Jet 4.0 SP-8)

    When I execute the following code:

    Me.strAttachment = cdlg.GetName

    Returns a value with length of 257 for F:AppsAccess2kFileName.ext

    I have to use the following to avoid length error
    Me.strAttachment = Left(cdlg.GetName, 255)

    Is there a proper way to retrieve the actual path and file name without using above statement?

    Thanks, John

    <pre>'F:AppsAccess2kimaNov2001 CommonDialog.mdb, Browse for file
    Private Sub cmdAPIFileOpen_Click()

    On Error GoTo ErrHandler

    If IsNull(Me.strAttachment) Or Me.strAttachment = "" Then

    Dim cdlg As New CommonDialogAPI
    Dim lngFormName As Long
    Dim lngAppInstance As Long
    Dim strInitDir As String
    Dim strFileFilter As String
    Dim lngResult As Long

    lngFormName = Me.hwnd
    lngAppInstance = Application.hWndAccessApp

    If CurrentUser() = "John" Or CurrentUser() = "mgmgg" Then 'WinXpSer1 & 2
    strInitDir = Left(GetLinkedTablePathEx("tblBPermit"), 3)
    Else
    If Not IsNull(GetPref("Path of Attachment Directory")) Then
    strInitDir = GetPref("Path of Attachment Directory")
    Else
    'return F: of the drive where the database is located
    strInitDir = Left(GetLinkedTablePathEx("tblBPermit"), 3)
    End If
    End If

    strFileFilter = "Excel Files (*.xls)" & Chr(0) & "*.xls" & Chr(0) & _
    "Word Files (*.doc)" & Chr(0) & "*.doc" & Chr(0) & _
    "Picture Files (*.gif, *.jpg, *.bmp)" & Chr(0) & _
    "*.gif; *.jpg; *.bmp" & Chr(0) & _
    "PDF Files (*.pdf)" & Chr(0) & "*.pdf" & Chr(0) & _
    "Text Files (*.csv, *.txt)" & Chr(0) & "*.csv; *.txt" & Chr(0)

    lngResult = cdlg.OpenFileDialog(lngFormName, lngAppInstance, _
    strInitDir, strFileFilter)

    If cdlg.GetStatus = True Then

    'MsgBox "Len(cdlg.GetName): " & Len(cdlg.GetName)
    'MsgBox "cdlg.GetName: " & cdlg.GetName

    Me.strAttachment = Left(cdlg.GetName, 255)

    Else
    MsgBox "No file selected."
    End If
    Else
    Call OpenAttachment_Click
    End If

    ExitHandler:
    On Error Resume Next
    Exit Sub

    ErrHandler:
    MsgBox Err.Description & " " & Err.Number & "cmdAPIFileOpen_Click() ", vbExclamation
    Resume ExitHandler

    End Sub
    </pre>


  2. #2
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,265
    Thanks
    2
    Thanked 187 Times in 173 Posts

    Re: Length of Path and File Name? (a2k (9.0.6926)

    Hi John,
    As you haven't included the code for your commondialogAPI class, I'm going to have to guess a bit. I suspect that the GetName method returns a variable that has been declared as a string with a fixed length of 257 characters. You should find that using Trim will work to return only the bit you want.
    Hope that helps.
    Regards,
    Rory

    Microsoft MVP - Excel

  3. #3
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Niagara Falls, New York, USA
    Posts
    1,877
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Length of Path and File Name? (a2k (9.0.6926)

    Hi Rory

    You are absolutely right

    .lpstrFile = String(257, 0)

    was the culprit in the following code floating around in many snipits of code on the internet

    Thanks, John


    <pre>Public Function OpenFileDialog(lngFormHwnd As Long, _
    lngAppInstance As Long, strInitDir As String, _
    strFileFilter As String) As Long

    Dim OpenFile As OPENFILENAME
    Dim X As Long

    With OpenFile
    .lStructSize = Len(OpenFile)
    .hwndOwner = lngFormHwnd
    .hInstance = lngAppInstance
    .lpstrFilter = strFileFilter
    .nFilterIndex = 1
    .lpstrFile = String(255, 0)
    .nMaxFile = Len(OpenFile.lpstrFile) - 1
    .lpstrFileTitle = OpenFile.lpstrFile
    .nMaxFileTitle = OpenFile.nMaxFile
    .lpstrInitialDir = strInitDir
    .lpstrTitle = "Open File"
    .Flags = 0
    End With

    X = GetOpenFileName(OpenFile)
    If X = 0 Then
    mstrFileName = "none"
    mblnStatus = False
    Else
    mstrFileName = Trim(OpenFile.lpstrFile)
    mblnStatus = True
    End If
    End Function
    </pre>


  4. #4
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Length of Path and File Name? (a2k (9.0.6926)

    That's because they're leaving room for the EOF characters at the end of the string buffer.,
    Charlotte

  5. #5
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Niagara Falls, New York, USA
    Posts
    1,877
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Length of Path and File Name? (a2k (9.0.6926)

    Thanks Charlotte

    I knew there had to be a reason.

    John

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •