SQL query  to Text file via VBA

Posted on 2015-01-13
Medium Priority
Last Modified: 2015-01-14
I need help with  a VBA script that pulls data from sql and inserts into a text file and will also be padding 80bytes on each record. Please help with any ideas .
Question by:valentinemhlanga
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
LVL 23

Assisted Solution

by:Michael Fowler
Michael Fowler earned 2000 total points
ID: 40547687
Here are some articles to get you started

Here is a step by step guide from Microsoft on importing data from SQL Server to Excel

and here is a guide to exporting to a text file

Lastly what sort of padding do you want to attach to the file and what is the goal of this?

Author Comment

ID: 40547704
@Michael74 , I am trying to create a file  in Bank of America file format, they need each record 80 bytes padded , numeric fields should be justified and 0 filled . Alpha numeric fields should be left justified and filled with spaces and all Alpha characters must be uppercase

Thank your for your  help
LVL 18

Expert Comment

ID: 40547750
Do you have a complete specification for the required format?

Can you supply some dummy records that you'd like output in the format?
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

LVL 23

Assisted Solution

by:Michael Fowler
Michael Fowler earned 2000 total points
ID: 40547797
This should not be hard to do but as noted by @SimonAdept we will need exact specifications to do it. eg

How large should a field be so that the appropriate number of zeros or spaces can be added and what sort of padding needs to be added eg whitespace

To start with I would recommend that you look at exporting the data to a worksheet from SQL Server using the example I provided. Note: You do not need VBA to do this https://support.office.com/en-in/article/Connect-to-import-SQL-Server-data-0f3d2e8f-7d94-4ded-baa6-3b0df183ff98

Once you have this you can sanitise the doc to give an example file to work with. If you are having any issue with just let us know

Author Comment

ID: 40548100
Thank you all for responding , the input data from SQL  would look something like this

IDNumber              ReportID      ReportName                         Amount      EMPLOYEE
########2154      20              Testing Report Submission         +25.00          436196
########2154      28               General Expense                              +29.00           233568

and the output file should look like the doc attatched Starting on the column where it says P70.  I would like to use SSIS Script task to create this file instead of using excel.
Please do let me know if you need more info regarding this
LVL 18

Expert Comment

ID: 40549405
>and the output file should look like the doc attatched

There was no doc attached!

I also note that your two example lines don't share a common format (when pasted to a text editor with a monospace font).

This really needs to be specificied excactly to work successfully.

Author Comment

ID: 40550261
I've requested that this question be deleted for the following reason:

LVL 23

Accepted Solution

Michael Fowler earned 2000 total points
ID: 40550014

Not sure why you have asked to have this deleted as n/a

We are happy to help with your request but unfortunately to create an output in a specific format we need to have exact specs on the format along with some sample data. Given this information the experts on this site can create a script which will meet your needs.

To give you an idea of what is needed

column: 18 characters, left aligned
12 numeric characters, padded with zeros as required

column: 18 characters, center aligned
numeric characters, no zero padding

This sort of detail is required regardless of the method used to generate the file

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

770 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