Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7878105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:33:56+00:00 2026-06-03T03:33:56+00:00

I have a Microsoft Access VBA file as shown below. Sub ExportTextFileDelimited(FileName As String,

  • 0

I have a Microsoft Access VBA file as shown below.

Sub ExportTextFileDelimited(FileName As String, _
    DataSet As String, _
    Delimiter As String, _
    TextQualifier As String, _
    WithFieldNames As Boolean)
    On Error GoTo ExportTextFile_Err

    Dim cnn As ADODB.Connection
    Dim rst As New ADODB.Recordset

    Dim Directory As String
    Dim MyString As String, strSQL As String
    Dim strDS As String
    Dim I As Integer

    Open FileName For Output As #1
    Set cnn = CurrentProject.Connection

    rst.Open DataSet, cnn, adOpenForwardOnly, adLockReadOnly
    If WithFieldNames Then
        For I = 0 To rst.Fields.Count - 1
            MyString = MyString & TextQualifier & rst(I).Name & TextQualifier & Delimiter
        Next I
        MyString = Left(MyString, Len(MyString) - 1)
        Print #1, MyString
    End If
    rst.MoveFirst
    Do While Not rst.EOF
        MyString = ""
        For I = 0 To rst.Fields.Count - 1
            'check for text datatype (202)
            If rst(I).Type = 202 Then
                MyString = MyString & TextQualifier & _
                rst(I)
            Else
                MyString = MyString & rst(I)
            End If
        Next I
            MyString = Left(MyString, Len(MyString) - 2)
        Print #1, MyString
        rst.MoveNext
    Loop

ExportTextFile_Exit:
    ' Close text file.
    Close #1
    rst.Close
    Set cnn = Nothing
    Exit Sub
ExportTextFile_Err:
    MsgBox Err.Description
    Resume ExportTextFile_Exit
End Sub

This code is working fine and it outputs a Query.txt file with the following data:

Please take a look at and refer to this example image.

Example of the problem described below

My question is, how do I apply maximum width for both AMAL and SAMANTHA GAMAGE data?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T03:33:58+00:00Added an answer on June 3, 2026 at 3:33 am

    What you are asking for here is usually referred to as “Fixed Width” (or “Fixed Length”), which is often considered to be a different type of text file than a Comma Delimmited (or CSV) file. Although you can mix the two concepts together in the same file.

    To get a String value to be a specified length you need to use VBA’s Space Function.

    Dim s as String
    s = "AMAL"
    s = s & Space(15 - len(s)) 'Add spaces to end
    s = Space(15 - len(s)) & s 'Add spaces to beginning
    

    It’s unclear from your post why you are adding spaces only to this one field and how you are going to determine how long that field should be (ie, how many spaces to add). Your function really isn’t setup properly for this feature. There are a large variety of ways to redesign this function so that it would know which fields to add spaces to and how many spaces to add to that field. And which way you choose really depends on your individual needs.

    Just to give you some idea how you would use this in your function:

    Dim iMaxLength as Integer
    Do While Not rst.EOF
        If Len(rst("UserName").Value) > iMaxLength Then iMaxLength = Len(rst("UserName").Value)
        rst.MoveNext
    Loop
    
    Do While Not rst.EOF
        rst.movefirst
        MyString = ""
        For I = 0 To rst.Fields.Count - 1
            'check for text datatype (202)
            If rst(I).Type = 202 Then
                If rst(I).Name = "UserName" Then
                    MyString = MyString & TextQualifier & _
                    rst(I) & Space(iMaxLength - Len(rst(I))) 
                Else
                    MyString = MyString & TextQualifier & _
                    rst(I)
                End If
            Else
                MyString = MyString & rst(I)
            End If
        Next I
            MyString = Left(MyString, Len(MyString) - 2)
        Print #1, MyString
        rst.MoveNext
    Loop
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i dont have microsoft access but would like to open an mdb file, is
So I have some experience with Microsoft Access, building database apps for people, vba,
I have one Microsoft Access .MDB file and want to use that database in
I have been successful in querying a Microsoft Access 2003 database (.mdb file) and
I have a Microsoft Access database query that I'm trying to import into a
I have a Microsoft Access database and there is a macro there. How do
I have a Microsoft Access form that is bound to a linked SQL Server
Does Microsoft Access have Full Text Search? I know that MySQL and SQL Server
I have a very complex Microsoft Access report. This report is run for multiple
I have a bound form in microsoft access that allows a user to add

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.