Mega Code Archive

 
Categories / Delphi / Strings
 

TStrings == TMemoField == TStrings

Title: TStrings == TMemoField == TStrings Question: whenever you need a MemoField in a database you'll need to somehow display the info, there's no direct way to do it (that I know of =o|)... Answer: so, here's a little trick to work around, plus we'll see a little how to work with the useful but not so used Streams //declare these functions in another unit (make reusable code!) //I have my functions in the MyDBUtils //this first function will return the memofield into a TString Procedure MemoFieldToStrings(FromTable:TTable; Const FieldName:String; GetLines:TStrings); Var aBlobStream:TBlobStream; Begin GetLines.Clear; aBlobStream:=nil; //this just to eliminate the warning With FromTable Do Begin Try aBlobStream := TBlobStream.Create(TMemoField(FieldByName(FieldName)), bmRead); GetLines.LoadFromStream(aBlobStream); Finally aBlobStream.Free End End End; //this function takes 3 parameters, the table where you're gonna get the data from, the field and a TStrings where it'll return back your data we can still wrap this function so we look up the index, then return the info... something like this: Procedure GetUsersForGroup(FromTable:TTable; Const GroupName:String; UsersInGroup:TStrings); Begin UsersInGroup.Clear; With FromTable Do If (FindKey([GroupName])) Then MemoFieldToStrings(FromTable, 'GroupMembers', UsersInGroup) //Else you can raise an exception or do whatever you want to let the user know that the group was not found End; //in my case I have a table with the group names and a TMemoField with the users, so I look up the group and return a list with the users an example of call to the wrapper function would be: GetUsersForGroup(UsersTable, Edit1.Text, ListBox1.Items) ------------------- 'til here we can load from our database to the screen... now let's see the other way, when we need to make changes to that MemoField //this function will do it Procedure StringsToMemoField(Table:TTable; Const FieldName:String; NewLines:TStrings); Begin //all exceptions are handled outside and there's nothing to free here, so no need for a Try...Finally If (NewLines.Count0) Then //if linecount=0 don't botter With Table Do Begin Edit; TMemoField(FieldByName(FieldName)).Assign(NewLines); Post End End; //now let's wrap it so is more useful //In my case I used this last function to save the changes to the groups, adding or removing users Procedure SetUsersForGroup(FromTable:TTable; Const GroupName:String; UsersInGroup:TStrings); Begin If (UsersInGroup.Count0) Then With FromTable Do If (FindKey([GroupName])) Then Try StringsToMemoField(FromTable, 'GroupMembers', UsersInGroup); Except //handle the exception as you need End End; an example of call to this last one: SetUsersForGroup(UsersTable, Edit1.Text, ListBox1.Items) that's it for now keep up coding salu2 EberSys