Solved

manipulating text

Posted on 2006-11-21
8
136 Views
Last Modified: 2010-04-30
This is a continuation of the following post

http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_22050353.html

the code below works great but I need it is moving text that I need to stay in the same position to the right depending on the length of the amount.

For instance,

109290511122555        *****WORKORDR12.00               NO
2Amount Charged  

109290511122555        *****WORKORDR1200.00               NO
2Amount Charged  


Should be
109290511122555        *****WORKORDR12.00               NO
2Amount Charged  

109290511122555        *****WORKORDR1200.00           NO
2Amount Charged  

I need the words letters NO to remain in the same position no matter what the  size of the amount.

Can anyone help?


Option Explicit

Private Sub Command1_Click()
   Dim FF         As Integer
   Dim Length     As Long
   Dim sText      As String
   Dim vSplit()   As String
   Dim sValue     As String
   Dim pos        As Long
   Dim i          As Long
   Dim j          As Long
   
   FF = FreeFile
   Open App.Path & "/WorkOrder.txt" For Input As #FF
   Length = LOF(FF)
   sText = Input$(Length, FF)
   Close FF
   vSplit = Split(sText, vbCrLf)
   For i = 0 To UBound(vSplit)
      If Left$(vSplit(i), 1) = "2" Then
         sValue = FormatNumber$(Val(Mid$(vSplit(i), 19)), 2)
         pos = InStr(vSplit(i - 1), "WORKORDR") + 7
         If pos Then
            For j = pos To Len(vSplit(i - 1))
               If Mid$(vSplit(i - 1), j, 1) = " " Then
                  vSplit(i - 1) = Left$(vSplit(i - 1), j) & _
                     sValue & Mid$(vSplit(i - 1), j + 1)
                  Exit For
               End If
            Next
         End If
      End If
   Next
   For i = 0 To UBound(vSplit)
      Debug.Print vSplit(i)
   Next
   sText = Join(vSplit, vbCrLf)
   FF = FreeFile
   Open App.Path & "/WorkOrderNew.txt" For Output As #FF
   Print #FF, sText
   Close FF
End Sub
0
Comment
Question by:earngreen
  • 4
  • 4
8 Comments
 
LVL 10

Expert Comment

by:Clif
ID: 17988895
Here's one way:

'Create a string the total length that you will write:
sWriteLine = Space$(56)

'The put the fields where you need them:
Mid$(sWriteLine, 1) = sAccountNumber '(109290511122555)
Mid$(sWriteLine, 24) = "*****WORKORDR"
Mid$(sWriteLine, 37) = sAmount 'Formatted (1200.00)
Mid$(sWriteLine, 55) = "NO"

'Write sWriteLine to the file
0
 

Author Comment

by:earngreen
ID: 17989423
So are you saying rebuild the file again?
0
 
LVL 10

Expert Comment

by:Clif
ID: 17995080
No, I'm saying that, instead of sText = Join(vSplit, vbCrLf), you should build a string as I suggested above.

Instead of creating vSplit, though, you should put your data in named variables.  vSplit(1) means nothing, but sAccountNumber is easier to understand what the code is trying to do.  Trust me, it will be a lot easier two or three years from now when you (or someone else) has to modify your code.
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:earngreen
ID: 18112805
OK,

I am still working on this but here is what I have. I apologize for being a little slow but new to VB6. The following is what I came up with.
Do I just want to print out the swriteline at the end?


//Total Lenght of the file
sWriteLine = Space$(56)

'The put the fields where you need them:
sAccountNumber = Mid$(sWriteLine, 1) //sAccountNumber '(109290511122555)
sWorkOrder = Mid$(sWriteLine, 24) //"*****WORKORDR"
sAmount = Mid$(sWriteLine, 37)//sAmount 'Formatted (1200.00)
Confirmation = Mid$(sWriteLine, 55) //"NO"
Description = Mid$(sWriteLine, 57) //2AmountCharged

sAccountNumber & sWorkOrder & SAmount & Confirmation & Vbclrf &_
Description
0
 
LVL 10

Accepted Solution

by:
Clif earned 500 total points
ID: 18114412
I'm not sure what you'll be wanting to do with the sWriteline at the end, that wasn't part of your initial question.

But, if you do print it out, in fixed-width font, all of the NO's will line up.
0
 

Author Comment

by:earngreen
ID: 18122625
OK,

The approach that you describe above will work. However, It gets back to one of my original problem in the following post

http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_22050353.html

It takes the value that I want to move from line two to line one.

For Instance,

if I have

109290511122555        *****WORKORDR                          NO
2Amount Charged  12.00

109290511122555        *****WORKORDR                          NO
2Amount Charged  1200.00

And it returns this

109290511122555        *****WORKORDR                         NO
2Amount Charged  

109290511122555        *****WORKORDR12.00                 NO
2Amount Charged  


0
 
LVL 10

Expert Comment

by:Clif
ID: 18123236
Work picked up a bit, so I'll have to get to this as I can.

Just to clarify, you need this:

109290511122555        *****WORKORDR                          NO
2Amount Charged  12.00

109290511122555        *****WORKORDR                          NO
2Amount Charged  1200.00

To return this:

109290511122555        *****WORKORDR1200.00                   NO
2Amount Charged

109290511122555        *****WORKORDR                          NO
2Amount Charged


Right?

0
 

Author Comment

by:earngreen
ID: 18123337
Sorry,

I finally figured it out. I jsut needed to pad the value and that strightened it out.

Thanks for the assistance.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Excel VBA combine two working workbooks 8 60
Looking for VB6 code to read SQL table export it to ascii 8 43
Passing a Text Box name to a Sub 6 99
Problem to With line 4 63
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

830 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