SQL code highlighting in Richtextbox

Posted on 2002-06-12
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
Question by:Catouch
  • 4
  • 2
  • 2

Expert Comment

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.

Expert Comment

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.


Author Comment

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?

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

LVL 43

Expert Comment

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.

Author Comment

ID: 7072585
yes, that's it!

Author Comment

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

Accepted Solution

TimCottee earned 100 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 "
                        .SelColor = vbBlue
                    Case "3 "
                        'System Function
                        .SelColor = vbGreen
                    Case "5 "
                        .SelColor = RGB(75, 75, 75)
                    Case "6 "
                        .SelColor = vbRed
                    Case "7 "
                        .SelColor = vbCyan
                    Case "8 "
                        .SelColor = vbMagenta
                    Case "10"
                        'Standard Text
                        .SelColor = vbBlack
                    Case Else '2, 4, 9
                    End Select
                End With
'            End If
        End If
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

Author Comment

ID: 7072602
thanks a lot

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
add text to end of existing text in file 16 70
String manipulation in Visual Basic 7 62
MsgBox 2 54
how to know if my Checkbox is True in VB6.0? 9 53
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

813 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now