Solved

How to Retrieve data from a memo field

Posted on 2004-10-11
11
457 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 11

Expert Comment

by:Jokra_the_Barbarian
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))

wend
0
 

Author Comment

by:MSelect
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 !
0
 
LVL 11

Expert Comment

by:Jokra_the_Barbarian
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.
0
Get Database Help Now w/ Support & Database Audit

Keeping your database environment tuned, optimized and high-performance is key to achieving business goals. If your database goes down, so does your business. Percona experts have a long history of helping enterprises ensure their databases are running smoothly.

 
LVL 26

Accepted Solution

by:
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
    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
 
LVL 11

Expert Comment

by:Jokra_the_Barbarian
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?
-Jokra
0
 
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.

Alan
0
 
LVL 50

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.

/gustav
0
 
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...


Alan
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Creating and Managing Databases with phpMyAdmin in cPanel.
Read about achieving the basic levels of HRIS security in the workplace.
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…

751 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