Solved

How to Retrieve data from a memo field

Posted on 2004-10-11
11
405 Views
Last Modified: 2008-02-26
Hi,
I need your help !
Working with VB6, I have an Access database table (accessed with DAO) named "Table1" with a single MEMO field, named "Field1" and containing text lines (i.e. separated with CHR(13) & CHR(10))

I want to retrieve the 4 first characters of every line of every record. For example

1st record :
ABCDdrf
CDFRnh

2nd record :
DSERtrez
JKPDaaan
EFDRxx

3rd record :
DDDEs

I want to get the following data :
ABCD
CDFR
DSER
JKPD
EFDR
DDDE

... but I'm unable to write the right SQL query ! Is it possible ?

Thank's for your help
0
Comment
Question by:MSelect
11 Comments
 
LVL 11

Expert Comment

by:Jokra_the_Barbarian
Comment Utility
Try setting your output field to a function. Pass the memo field value into the function and get a string back. Use something similar to this within the function:

spos = 1

while spos<>0

  newStr = newStr & mid(oldStr, spos, 4) & chr(13)
  spos   = InStr(spos, oldStr, char(10))

wend
0
 

Author Comment

by:MSelect
Comment Utility
Hummmm ...
If I well understand, it's what I currently do when I read a single record to get the first chars of every line but what I want is to buid a new Table by using a single query only once. It would take too much time in reading every record successively !
0
 
LVL 11

Expert Comment

by:Jokra_the_Barbarian
Comment Utility
The only way to get what you want is to parse the memo field for each record. Memo fields are large and parsing unfortunately takes time. If you are building a table, then you will only be running this query once.

I don't know of any other way to get what you want without parsing.
0
 
LVL 26

Accepted Solution

by:
Alan Warren earned 500 total points
Comment Utility
Hi MSelect

Use the ADODB.Stream

Reference: Microsoft ActiveX Data Objects 2.6 Lib


Dim objStm As ADODB.Stream

Dim strSql As String
Dim strLine As String

strSql = "select Field1 from table1"
Set objRs = New ADODB.Recordset
objRs.Open strSql, CurrentProject.Connection, adOpenForwardOnly, adLockReadOnly
' If not inside access you will need to instantiate a connection object
' to use in place of CurrentProject.Connection.

With objRs
  While Not .EOF
   
    'Instantiate and open stream
    Set objStream = New Stream
    objStream.Open
   
    'Set line separator to line feed
    objStream.LineSeparator = adCRLF   ' or use adLF
       
    'Load text content Field1 into stream
    objStream.WriteText .Fields("Field1"), adWriteLine
    objStream.Position = 0
   
    Do Until objStream.EOS
        strLine = Left(objStream.ReadText(adReadLine), 4)
        Debug.Print strLine
    Loop
    .MoveNext
  Wend
End With



output:
ABCD
CDFR
DSER
JKPD
EFDR
DDDE




Alan
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 11

Expert Comment

by:Jokra_the_Barbarian
Comment Utility
Hey Alan, that's pretty slick. While you are still iterating through the recordset, I would think that this streaming example would be faster than parsing? Have you done any performance comparisons?
-Jokra
0
 
LVL 26

Expert Comment

by:Alan Warren
Comment Utility
Hi Jokra,

Thanks for the thumbs up :)

No not done any comparisons, I use streams a lot with blobs, so I am familiar with the syntax, they seem fairly robust to me. Memo field is basically a BLOB field (Binary) so I thought the ADODB.Stream was the way to go here.

Regarding speed, streams are in memory, so they cant take that long to process.

Alan
0
 
LVL 49

Expert Comment

by:Gustav Brock
Comment Utility
The streaming method is nice but - using DAO - why not just open the new (empty) table and the input table, read record by record of the input table, for each record read the memo field, and for each line in the memo field do an AddNew to the new table appending a record with the four chars.

If you wrap this in BeginTrans and CommitTrans it will run very fast.

/gustav
0
 
LVL 26

Expert Comment

by:Alan Warren
Comment Utility
Hi Venabili,

First of all let me say I really appreciate the excellent cleanup job you are doing, THANK YOU.
Personally I think cleanup operators should get a points commission on all Q's, similar to debt collection agent. - LOL

I am most interested in this questions final disposition, I am most interested in some feed-back from the author regarding the solution I proffered, it was tested and tried before I posted.

Take care...


Alan
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Introduction: Often, when running a query with joins, the results show up "duplicates", and often, those duplicates can be "eliminated" in the results using DISTINCT, for example. Using DISTINCT is simple: just add it after the SELECT keyword, an…
This article describes some very basic things about SQL Server filegroups.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 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