Unable to append string ".txt" to another string

Posted on 2005-05-11
Medium Priority
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
  • 2
LVL 43

Accepted Solution

TimCottee earned 200 total points
ID: 13976550
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

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

Expert Comment

ID: 13977760
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Windows Explorer let you handle zip folders nearly as any other folder: Copy, move, change, and delete, etc. In VBA you can also handle normal files and folders, but zip folders takes a little more - and that you'll find here.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses
Course of the Month15 days, 13 hours left to enroll

850 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