Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL code highlighting in Richtextbox

Posted on 2002-06-12
8
Medium Priority
?
3,292 Views
Last Modified: 2008-05-04
Hi all,

Is it possible to highlight code in a richtextbox in the same way as in the SQL Query analyzer?
I've looked in MSDN, and I think it's feasable, but I really didn't find how to do that.

Thanks for the help
0
Comment
Question by:Catouch
  • 4
  • 2
  • 2
8 Comments
 
LVL 4

Expert Comment

by:gencross
ID: 7072564
Here is a very basic example of hightlighting in a rich text box

rText.Text = "Don't highlight, highlight"
   
rText.SelStart = 17
rText.SelLength = 9


Hope it helps.
0
 
LVL 4

Expert Comment

by:gencross
ID: 7072569
Note, There are other properties that set attribs. for the selected text such as Color, bold, etc...  They all begin with Sel (SelColor) in the properties of the Rich text box.

0
 
LVL 1

Author Comment

by:Catouch
ID: 7072572
I know how to higlight, thanks.
I meant the SQL highlighting format.
eg: when you write "exec", it automatically turns to blue...

SQL syntax in higlighted in different colors.
Is this possible?

Thanks
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 43

Expert Comment

by:TimCottee
ID: 7072573
Do you mean that you want to use an RTB with SQL code and highlight it as SQL would? If so then I can certainly help here as I was involved in another thread that did exactly this a while ago.
0
 
LVL 1

Author Comment

by:Catouch
ID: 7072585
yes, that's it!
0
 
LVL 1

Author Comment

by:Catouch
ID: 7072586
yes, that's it!
0
 
LVL 43

Accepted Solution

by:
TimCottee earned 400 total points
ID: 7072591
Ok, just found it:

Add a textbox and a richtextbox on the form, add a reference to Microsoft SQL Parser Object Library 1.0 (SQLPARSE.DLL in \program files\microsoft office\office\ folder) Then run the application and start typing in the text box, the RTB should display the correctly highlighted SQL code.

Private Sub WriteToRTF(ByRef RTBox As RichTextBox, ByVal Statement As String)
    Dim intElement As Long
    RTBox.Text = ""
    Dim aryElements() As String
    aryElements = Split(Statement, "\cf ")
    For intElement = LBound(aryElements) To UBound(aryElements)
        If Trim(aryElements(intElement)) <> "" Then
'            If aryElements(intElement) = String(Len(aryElements(intElement)), " ") Then
'                RTBox.Text = RTBox.Text & aryElements(intElement)
'            Else
                strContent = Mid(Trim(aryElements(intElement)), 6, Len(Trim(aryElements(intElement))) - 5)
                With RTBox
                    .Text = .Text & String(Len(aryElements(intElement)) - Len(strContent) - 5, " ")
                    .Text = .Text & strContent
                    .SelStart = Len(.Text) - Len(strContent)
                    .SelLength = Len(strContent)
'                    Debug.Print .SelStart, .SelLength, strContent
                    Select Case Mid(Trim(aryElements(intElement)), 4, 2)
                    Case "1 "
                        'Keyword
                        .SelColor = vbBlue
                    Case "3 "
                        'System Function
                        .SelColor = vbGreen
                    Case "5 "
                        'Operator
                        .SelColor = RGB(75, 75, 75)
                    Case "6 "
                        'Text
                        .SelColor = vbRed
                    Case "7 "
                        'Number
                        .SelColor = vbCyan
                    Case "8 "
                        'Comment
                        .SelColor = vbMagenta
                    Case "10"
                        'Standard Text
                        .SelColor = vbBlack
                    Case Else '2, 4, 9
                    End Select
                End With
'            End If
        End If
    Next
End Sub

Private Sub Text1_Change()
    ParseContent Text1.Text
End Sub

Private Sub ParseContent(ByVal SQL As String)
   Dim osQL As MSSQLParser.vbSQLParser
   Dim sTemp As String
   Set osQL = New MSSQLParser.vbSQLParser
   sTemp = osQL.ParseSQLSyntax(SQL, vbSqlServerSyntax)
   WriteToRTF RichTextBox1, sTemp
   Debug.Print sTemp
   Set osQL = Nothing
End Sub
0
 
LVL 1

Author Comment

by:Catouch
ID: 7072602
thanks a lot
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

963 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