Printing Reports
-
Hello, We have several reports on the web that require printing. Now as I am sure all of you know it can be a pain to write all the code required to format the lines correctly so they dont wrap. There is one report that I am thinking of that has caused me much pain in the past. It prints out all the operations and instructions to create a part. Well the operation descriptions can be quite long so I needed a way to accuratly print and count lines of text. I had written a class that would formatt the information breaking at the correct length for printing but the problem was the ammount of time it was costing me when the report would render. So I thought to my self.. Self.. wouldn't it be great if I had a function that would formatt the information for me so when I pulled it from the database all I had to do was display and count? So I wrote a little function that does just that:
--------------------------------------------------------------------------- CREATE FUNCTION F_FormatRevisionDescForPrint(@Desc VARCHAR(1000), @DisplayLenghOfString INT) RETURNS VARCHAR(2000) AS BEGIN DECLARE @CharacterCounter INT ,@DescLen INT ,@WhenToBreak INT ,@FormattedDesc VARCHAR(2000) ,@NewLine VARCHAR(2) ,@Space VARCHAR(1) ,@NextCharacter VARCHAR(1) ,@PreviousCharacter VARCHAR(1) ,@CurrentCharacter VARCHAR(1) ,@NewString VARCHAR(2000) ,@NumberOfCharactersToCut INT -- SELECT @CharacterCounter = 0 ,@DescLen = LEN(@Desc) ,@WhenToBreak = 0 ,@FormattedDesc = '' ,@NewString = '' ,@NewLine = CHAR(13) + CHAR(10) ,@Space = ' ' -- IF (@DescLen > 0) BEGIN WHILE @CharacterCounter <= @DescLen BEGIN -- SELECT @FormattedDesc = @FormattedDesc + SUBSTRING(@Desc,@CharacterCounter,1) ,@CurrentCharacter = SUBSTRING(@Desc,@CharacterCounter,1) ,@NextCharacter = SUBSTRING(@Desc,(@CharacterCounter+1),1) ,@PreviousCharacter = SUBSTRING(@Desc,(@CharacterCounter-1),1) ,@WhenToBreak = @WhenToBreak + 1 -- IF (@WhenToBreak = @DisplayLenghOfString) BEGIN -- I need to check and make sure that I am not breaking on -- a word. IF (@CurrentCharacter = @Space) BEGIN -- There is a space here so it is ok to break -- I know that I am not in the middle of a word SELECT @FormattedDesc = @FormattedDesc + '' ,@WhenToBreak = 0 ,@CharacterCounter = @CharacterCounter + 1 -- END ELSE IF (@CurrentCharacter != @Space) BEGIN -- houston we have a problem I am somewher