Solved

manipulating text

Posted on 2006-11-21
8
134 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…

864 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now