code to check length of data fields and to convert them into text file as per the bytes prescribed

vinod9111

Active Member
Joined
Jan 21, 2009
Messages
426
Hi All,

Have to do huge task of reporting more than 80 lakhs rows of data at account level to regulators. Request your help for construction of code in access which can check the length of the data fields and also convert them into text post checking as per the bytes mentioned.
Job_Code

<tbody>
</tbody>
Month_

<tbody>
</tbody>
Year_

<tbody>
</tbody>
FillerI

<tbody>
</tbody>
BSR_CODE_PART_I

<tbody>
</tbody>
FillerII

<tbody>
</tbody>
pag_no

<tbody>
</tbody>
sr_no

<tbody>
</tbody>
FCL

<tbody>
</tbody>
dist code

<tbody>
</tbody>
population grp_Code

<tbody>
</tbody>
type of acct

<tbody>
</tbody>
org code

<tbody>
</tbody>
occupation

<tbody>
</tbody>
nature of borrw acct

<tbody>
</tbody>
asset class

<tbody>
</tbody>
Secured/Unsecured Loan code

<tbody>
</tbody>
Fixed/Floating interest Flag

<tbody>
</tbody>
BANK RATE1

<tbody>
</tbody>
AMTFIN_roundoff

<tbody>
</tbody>
POS_roundoff

<tbody>
</tbody>
Lot_no

<tbody>
</tbody>
Colan

<tbody>
</tbody>
Unique ID

<tbody>
</tbody>
Length

<tbody>
</tbody>
2

<tbody>
</tbody>
2

<tbody>
</tbody>
4

<tbody>
</tbody>
1*71*
4*

<tbody>
</tbody>
4*

<tbody>
</tbody>
1312252111410104120
Bytes

<tbody>
</tbody>
1-2

<tbody>
</tbody>
3-4

<tbody>
</tbody>
5-8

<tbody>
</tbody>
9-9

<tbody>
</tbody>
10-16

<tbody>
</tbody>
17-17

<tbody>
</tbody>
18-21

<tbody>
</tbody>
22-25

<tbody>
</tbody>
26-26

<tbody>
</tbody>
27-29

<tbody>
</tbody>
30-30

<tbody>
</tbody>
31-32

<tbody>
</tbody>
33-34

<tbody>
</tbody>
35-39

<tbody>
</tbody>
40-41

<tbody>
</tbody>
42-42

<tbody>
</tbody>
43-43

<tbody>
</tbody>
44-44

<tbody>
</tbody>
45-48

<tbody>
</tbody>
49-58

<tbody>
</tbody>
59-68

<tbody>
</tbody>
69-72

<tbody>
</tbody>
73-73

<tbody>
</tbody>
74-93

<tbody>
</tbody>

<tbody>
</tbody>
* is a space

a) From the above table the first row is a header.

b) Second row is the length of the data required for submission. The asterisk length is a space required for that particular filed.

c) The third row indicates the bytes required for conversion of data into text.

Request you to help me with the code which can check the length of this humongous data and also help me convert the data in text file with the above bytes position.

any help in this regard will be appreciated.

regards,

Vinod

<tbody>
</tbody>
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Your post is kind of cryptic to me. Maybe a sample of the actual data and desired outcome would help. I'm not getting the significance of the byte row values.
If you want to know the length of the value in a field, it's just Len(NameOfField). If you want to convert it to text, use the CStr function. Note that you'll generate errors if the field is Null, so you'd have to wrap CStr around the Nz function. And what are "lakhs"?

Edit: Never mind. I looked it up.
 
Last edited:
Upvote 0
Dear Micron,

Thanks for your revert

Sample data is below

Job_CodeMonth_Year_FillerI BSR_CODE_PART_I FillerII pag_no sr_no FCL dist codepopulationtype of acctorg codeoccupationnature of borrw acctasset classSecured/Unsecured Loan codeFixed/Floating interestBANK RATE1AMTFIN_roundoffPOS_roundoffLot_noColonUnique IDSemi colan
60

<tbody>
</tbody>
0920170000567144043041950199941287904503400001:0000303LN37130840034;
600920170000789144043041950199941245603202100001:0000303LN37130921575;

<tbody>
</tbody>

I need a code to check the data in the field has the length which is shown above , for instance job code should be two digit, month should be two digit and so on and the blank cells are to be left blank. Hope am able to explain properly

With regard to export of tables into text files, the bytes i mentioned is the starting point and width required of each field while exporting.

Lakhs is the measure to count in India, 10 lakhs = 1 million


regards,

Vinod
 
Upvote 0
rough and ready (and i'm sure someone has a better way)
=SUM(LEN(B4)+LEN(C4)+LEN(D4)+LEN(E4)+LEN(F4)+LEN(G4)+LEN(H4)+LEN(I4)+LEN(J4)+LEN(K4)+LEN(L4)+LEN(M4)+LEN(N4)+LEN(O4)+LEN(P4)+LEN(Q4)+LEN(R4)+LEN(S4)+LEN(T4)+LEN(U4)+LEN(V4)+LEN(W4)+LEN(X4)+LEN(Y4))
 
Upvote 0
As far as I know, Cstr() won't necessarily give you fixed width fields, btw. The basic task here is to create a fixed width text file as output. You have to do some padding one way or another to make sure that fields that aren't the necessary length are padded to fill in the extra space.
 
Upvote 0
As far as I know, Cstr() won't necessarily give you fixed width fields, btw. The basic task here is to create a fixed width text file as output. You have to do some padding one way or another to make sure that fields that aren't the necessary length are padded to fill in the extra space.
Not sure how you arrived at that conclusion but I'll go with it. The "sample" data doesn't do much for me. To me it says, job code, month, account type, etc. always has to be 2 characters. Year has to be 4. BSR has to be padded so that it is 7; Unique ID has to be 20, etc. For padding, I would suggest using the solution found here in post 21. If I'm right about the rest, then validation a the table field level might be easiest. If I'm not, then I still don't know what the need is.

As for CStr, I took this literally
and also convert them into text
 
Last edited:
Upvote 0
Fixed width fields come in two flavors: one is where the data is stored in a fixed width and is also always exactly of a certain length. Example is a four digit year that must be four digits. The other is where data is stored in a fixed width field of but the data itself is of a variable length (i.e, -- the data is stored in a fixed width field but can be padded). Example is month that is stored in a 2-byte field with or without leading zeros and/or padding with spaces.

so we can imagine a fixed width files like these:
201701
20171
2017 1
The first is a four byte year followed by a 2 byte month, no padding.
The second is a four byte year followed by a 2 byte month where the month is padded with a trailing space.
The third is a four byte year followed by a 2 byte month where the month is padded with a leading space (not so common I guess).

Generally fixed width files are for importing or exporting data, and the nature of the beast is such as that typically you are providing the data to a somewhat older system that still uses fixed width files like this. Naturally if the data is stored in a database the job can be simplified if the table fields themselves are requiring data be stored with constraints to ensure valid data, at some level (such as years be four digits or months be numbers between 1 and 12).



Edit: note that what I usually do in these situations is write a custom routine that generates the text file - as each line is written, the fields can be validated and formatted to meet requirements.
 
Last edited:
Upvote 0
Thanks for the insight. As old as I am, I've never had to work with such legacy files :)
 
Upvote 0
VBA remembers those good old days (the I/O routines contain procedures that are made for reading and writing formatted output. You can also declare fixed width strings (a little known fact, I guess):

Code:
Dim s As String * 10
s = "Hello"
Debug.Print "|" & s & "|"
 
Upvote 0

Forum statistics

Threads
1,214,923
Messages
6,122,283
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top