Thursday, April 12, 2012

SQL - Count Character Occurrence in a String


Want to count the number of times a specific character appears within a string? The line below will count the number of times the letter 'a' appears within the string value returned from the column 'description'.  I display the column for description and a column for the count side by side.

select description, len(description)-len(replace(description,'a','')) as 'count' from table_items

VB.NET - Margin Outside of Printable Area

Problem: You are trying to print something in Microsoft Word through VB.NET but get stopped with a message telling you the margins are outside of the printable area and you have to click to continue printing.  You do not want this message to come up but instead go straight to printing.

Solution: You must turn off the the error messaging and backgroud printing, then print the document, then you can turn everything back on the way it was.  Here is the code snippet to show you how.



Dim objWordApp As Word.Application
Dim blnWordAlerts As Boolean


'............
'code
'............

' Store current Background Printing setting.
blnWordAlerts = objWordApp.Options.PrintBackground

' Turn off error messaging and Background Printing.
objWordApp.Application.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone
'objWordApp.wdAlertsNone()
objWordApp.Options.PrintBackground = False

' Print the active document.
objWordApp.ActiveDocument.PrintOut(False) 'false allows the print to finish before it moves to any other code

' Turn on error messaging and restore Background Printing
' to original setting.
objWordApp.Application.DisplayAlerts = Word.WdAlertLevel.wdAlertsAll
objWordApp.Options.PrintBackground = blnWordAlerts