Unable to append string ".txt" to another string

Posted on 2005-05-11
Last Modified: 2006-11-18
I need to write database data to a text file. I am able to open the save file dialog box. I need to append the .txt for file name if the user has not given the extension
I have the following VBA code :
    Dim lngFormHwnd As Long
    Dim lngAppInstance As Long
    Dim strInitDir As String
    Dim strFileFilter As String
    Dim strFileName As String
    Dim lngResult As String
    Dim strTitle As String
    Dim mlngFileNum As Long
    mlngFileNum = FreeFile
    strTitle = "Save File"

strFileName = "" & String(256, 0)

    lngResult = objDialog.ExportDialog(lngFormHwnd, lngAppInstance, strInitDir, strFileFilter, strFileName, strTitle)

Public Function ExportDialog(lngFormHwnd As Long, lngAppInstance As Long, strInitDir As String, strFileFilter As String, strFileName As String, strTitle As String) As String
      Dim txtPosition As Integer
    With Ofn
        .lStructSize = Len(Ofn)
        .hwndOwner = lngFormHwnd
        .hInstance = lngAppInstance
        .lpstrFilter = strFileFilter
        .nFilterIndex = 1
        .lpstrFile = strFileName
        .nMaxFile = Len(Ofn.lpstrFile) - 1
        .lpstrFileTitle = Ofn.lpstrFile
        .nMaxFileTitle = Ofn.nMaxFile
        .lpstrInitialDir = strInitDir
        .lpstrTitle = strTitle
        .Flags = 0
    End With
    If GetSaveFileName(Ofn) = 0 Then
        mstrFileName = "none"
        mblnStatus = False
        mstrFileName = Trim(Ofn.lpstrFile)
        mstrFileTitle = Trim(Ofn.lpstrFileTitle)
        mblnStatus = True
    End If
     txtPosition = InStr(RTrim(LTrim(mstrFileName)), ".txt")
     If txtPosition Then
         ExportDialog = RTrim(LTrim(mstrFileName))
    ExportDialog = RTrim(LTrim(mstrFileName)) & ".txt"
    End If
End Function

I am not able to append the .txt string at the end of mstrFileName
"C:\Documents and Settings\ramakris.k\My Documents\test12367....(till total chars are 256)"
Why this  chars are coming? Ltrim and Rtrim are also not working!
Question by:rama_krishna
    LVL 44

    Accepted Solution

    Hi rama_krishna,

    They are character 0 because you have filled your string with null chars: strFilename = "" & String(256,0)

    Rather than using rtrim etc do:

    mstrFileName = Left(mstrFileName,instr(mstrFileName,Chr(0))-1)

    Before testing for .txt and adding it, after the end if and before txtPosition = Instr......

    Tim Cottee
    LVL 44

    Expert Comment

    Also I believe the maximum number of characters in a filename is 255, not 256.
    LVL 44

    Expert Comment

    I've done some playing and found the maximum no of characters in the filename before the extension is 240.  I believe Windows XP allows up to 15 characters for the extension and with the period, that would make 256 characters for the name excluding the path.  

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
    This isn't a frequent question on EE. I must have seen it three or four times (among several thousand questions). However, I use this trick quite often, most frequently as a delayed Current event. A form does not expose it's calculation dependenc…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
    What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now