Solved

Parse text file

Posted on 1998-09-08
4
123 Views
Last Modified: 2012-05-07
I have huge text file and I want to export it to database
I have text(row) delimiters ///
I probably want 3 fields  1.identity-number 2.name-first line of text following ///  3.actual data between ///...///
Suppose I open file and save it into variable(variant), it is huge
Can I parse variant
using InStr Left and right functions
What is your best solution to this
Access and MSSQL data transformation services cannot understand
/// delimiters.
0
Comment
Question by:mativare
  • 2
4 Comments
 
LVL 1

Author Comment

by:mativare
ID: 1433764
Edited text of question
0
 
LVL 14

Accepted Solution

by:
waty earned 50 total points
ID: 1433765
You can use the following function :

Public Function GetTokens(sTxt As String, sToken As String) As Variant
   ' *** Returns an array to tokenized values
   ' ***  Ex:  GetTokens("Field1///Field2///Field3///", "///") = ({ "Field1", "Field2", "Field3" })

    Dim iTokenLen As Integer
    Dim iTokenCnt As Integer
    Dim lOffset As Long
    Dim lPrevOffset As Long
    Dim aTokens() As String

    iTokenLen = Len(sToken)
    lOffset = InStr(sTxt, sToken)
   
    Do While lOffset > 0
        ReDim Preserve aTokens(iTokenCnt)
        If lOffset - lPrevOffset > 1 Then
            aTokens(iTokenCnt) = Mid$(sTxt, lPrevOffset + 1, lOffset - 1 - lPrevOffset)
        Else
            aTokens(iTokenCnt) = ""
        End If
       
        lPrevOffset = lOffset
        lOffset = InStr(lOffset + iTokenLen, sTxt, sToken)
        iTokenCnt = iTokenCnt + 1
    Loop
   
    ReDim Preserve aTokens(iTokenCnt)
    aTokens(iTokenCnt) = Mid$(sTxt, lPrevOffset + 1)
    GetTokens = CVar(aTokens)

End Function

0
 
LVL 1

Author Comment

by:mativare
ID: 1433766
Thanks waty
This piece of code is platinum
0
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1433767
Bought This Question.
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

Suggested Solutions

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

911 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

17 Experts available now in Live!

Get 1:1 Help Now