How to Retrieve data from a memo field

Posted on 2004-10-11
Last Modified: 2008-02-26
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 :

2nd record :

3rd record :

I want to get the following data :

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

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

Expert Comment

ID: 12276733
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))


Author Comment

ID: 12277076
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 !
LVL 11

Expert Comment

ID: 12277370
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.
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

LVL 26

Accepted Solution

Alan Warren earned 500 total points
ID: 12278913
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
    '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
End With


LVL 11

Expert Comment

ID: 12279068
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?
LVL 26

Expert Comment

by:Alan Warren
ID: 12283117
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.

LVL 49

Expert Comment

by:Gustav Brock
ID: 12285120
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.

LVL 26

Expert Comment

by:Alan Warren
ID: 12511402
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...


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
Question about consuming GB from Comcast 5 61
best datatype for oracle table email creation 8 55
SYbase 4 36
What's wrong with this T-SQL Foreign Key? 7 51
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
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…

808 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