If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Visual Basic > VBA question..

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-26-10, 07:01
st3ven_J st3ven_J is offline
Registered User
 
Join Date: Feb 2009
Posts: 35
VBA question..

Hi

I was wondering if somebody can help or at least point me in the right direction..
I have a txt input file and am trying to export specific data from this file into an output txt file.

The input file looks something like this :

CPNTCS
LIN01
LIN02
LIN03
CPNTCS
LIN01
LIN02
CPNPTC
LIN01
CPNLPL
LIN01
LIN02

For the output, I need the file to output ONLY the "CPNTCS" with their corresponding lines (to skip all other records.)

I have attached the sample code I am using - this basically outputs the CPNTCS lines but no other info. See below :

Option Compare Database
Option Explicit

Function OutputTCS(InFile As String, OutFile As String)

Dim intInHandle As Integer
Dim intOutHandle As Integer
Dim strInLine As String
Dim strOutLine As String

intInHandle = FreeFile
Open InFile For Input As #intInHandle
intOutHandle = FreeFile
Open OutFile For Output As #intOutHandle

Do Until EOF(intInHandle)
Line Input #intInHandle, strInLine
If Left(strInLine, 6) = "CPNTCS" Then
strOutLine = strInLine
End If
Print #intOutHandle, strOutLine
Loop

End Function


How can this be done? or what statement would be required.

Thanks
Reply With Quote
  #2 (permalink)  
Old 03-04-10, 09:54
Ax238 Ax238 is offline
Registered User
 
Join Date: May 2009
Posts: 257
Hello st3ven_J,

Sorry for the late response. If every header line begins with "CPN", you can change your Do loop to the following:
Code:
Do Until EOF(intInHandle)
	Line Input #intInHandle, strInLine
	If Left(strInLine, 3) = "CPN" Then
		strHeader = Left(strInLine, 6)
	End If
	If strHeader = CPNTCS" Then
		Print #intOutHandle, strInLine
	End If
Loop
You'll need a new strHeader variable and will no longer need the strOutLine variable.

Regards,

Ax
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On