funny loop question

i have a loop that reads from a database and it gets a name from a database field. i need some way to make it where when the names start to change i add the letter the name starts with

EXMAPLE
J
john doe
john smith

K
Keith willams
Kim Loving



etc............
LVL 1
Chris JonesLead Application Web DeveloperAsked:
Who is Participating?
 
Pieter JordaanConnect With a Mentor Senior Systems Administrator - Web hosting and Network Security.Commented:

I would loop from a to z, and then check which entries start with the current alphabet character.

Look at the attached code.

Create a form with a textbox called TextBox1, and add the following code to Form1_Load
        Dim a As New ArrayList
        a.Add("john doe")
        a.Add("john smith")
        a.Add("Keith willams")
        a.Add("Kim Loving")

        Dim alphabet As String = "abcdefghijklmnopqrstuvwxyz"
        Dim CH As Char = ""

        For abc As Integer = 1 To alphabet.Length
            CH = Mid(alphabet, abc, 1)
            TextBox1.Text += vbCrLf & UCase(CH) & vbCrLf

            For Each s As String In a
                If Mid(LCase(s), 1, 1) = CH Then
                    TextBox1.Text += s & vbCrLf
                End If
            Next
        Next

Open in new window

0
 
scgstuffCommented:
What are you doing with the loop?  Are you adding it to a dataset?  Are you just displaying the info?  

dim curLetter as string = ""
dim newLetter as string = ""

for each name in ______________
     newLetter = left(name, 1)

     if curLetter <> newLetter then
          'Do something with the letter
     end if

     'Do whatever with the name

     curLetter = newLetter

Next
0
 
Carl TawnSystems and Integration DeveloperCommented:
Well normally you would do something like the following psuedo-code:
char lastStartLetter = ''c;

while ( not_at_end_of_table )
{
    if ( first_letter_of_current_record != lastStartLetter)
    {
         output_letter_to_screen
    }

    output_row_data

    lastStartLetter = first_letter_of_current_record

    read_next_record
}

Open in new window

0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Chris JonesLead Application Web DeveloperAuthor Commented:
i am displaying the letter to the screen with the names i pul from the database
0
 
Chris JonesLead Application Web DeveloperAuthor Commented:
BitFreeze: this will take a long time and the sql statement i am using puts everything in order for me
0
 
Pieter JordaanSenior Systems Administrator - Web hosting and Network Security.Commented:

Use SQL to pull a list of all user names that start with a, then b, then c etc.
0
 
ZhaolaiConnect With a Mentor Commented:
Try this:
strNames is an array of names pulled from database.

        For Each strName As String In strNames
            If Not strName.StartsWith(sFirstLetter, System.StringComparison.CurrentCultureIgnoreCase) Then
                sFirstLetter = strName.Substring(0, 1).ToUpper
                'display the first letter here
            End If
            'display name here
        Next

Open in new window

0
 
Chris JonesLead Application Web DeveloperAuthor Commented:
Zhaolai: do you think the code below will run faster than storeing it into a data array



            ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            temp = name.Substring(0, 1)

            If temp IsNot temp Then
                'Insert a paragraph at the beginning of the document.
                oPara1 = oDoc.Content.Paragraphs.Add
                oPara1.Range.Text = temp
                oPara1.Range.Font.Bold = True
                oPara1.Format.SpaceAfter = 24    '24 pt spacing after paragraph.
                oPara1.Range.InsertParagraphAfter()
            End If

            '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Open in new window

0
 
ZhaolaiConnect With a Mentor Commented:
Oooop, complete code here:


        Dim sFirstLetter As String = " "
        For Each strName As String In strNames
            If Not strName.StartsWith(sFirstLetter, System.StringComparison.CurrentCultureIgnoreCase) Then
                sFirstLetter = strName.Substring(0, 1).ToUpper
                'display the first letter here
            End If
            'display name here
        Next

Open in new window

0
 
Pieter JordaanSenior Systems Administrator - Web hosting and Network Security.Commented:
It depends on the latency between server and client, also processing power, because you are switching the processing between server pulling a, b, c separately,  or client pulling everything then separating it.
0
 
Chris JonesLead Application Web DeveloperAuthor Commented:
it may help if i display the sql statemnt that i am using this may help show the steps i am using to get the data.
        sqlstmt = "SELECT * FROM TPerson AS P INNER JOIN TDIR AS L ON L.TDIR_PIDM = P.TPERSON_PIDM" & _
                 " INNER JOIN TAccounts AS S ON S.Acct = L.TDIR_Acct" & _
                 " INNER JOIN TCables AS A ON A.DN = L.TDIR_DN " & _
                 " INNER JOIN TBuildings AS B ON B.Bldg3d = A.Bldg3d WHERE COALESCE( L.TDIR_DN, '') NOT IN ('n/a', '')  AND  S.dateend IS NULL AND A.dateend IS NULL ORDER BY P.TPERSON_Name"

Open in new window

0
 
Pieter JordaanSenior Systems Administrator - Web hosting and Network Security.Commented:

So the server will either build and sort the entire list in one go, or do it in 26 parts.

The difference should be insignificant.
0
 
Chris JonesLead Application Web DeveloperAuthor Commented:
ok i got this to work using an exmaple in the post
            temp = name.Substring(0, 1)
            If temp <> temp2 Then

                'Insert a paragraph at the beginning of the document.
                oPara1 = oDoc.Content.Paragraphs.Add
                oPara1.Range.Text = temp
                oPara1.Range.Font.Bold = True
                oPara1.Format.SpaceAfter = 0    '24 pt spacing after paragraph.
                oPara1.Range.InsertParagraphAfter()
            End If
     temp2 = name.Substring(0, 1)

Open in new window

0
 
LowfatspreadCommented:
why not just add the Letters into the result set from the database....e.g. select firstname+' '+lastName as Name, Lastname from yourtableUnion Select Substring(Alpha,y.number,1) as Name, Substring(Alpha,y.number,1) as Lastname from (Select 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' as Alpha, (Select number from master..spt_values where type='P' and number between 1 and 26) as x   ) as yOrder By lastnamethen you can just display the data....
0
 
Chris JonesLead Application Web DeveloperAuthor Commented:
Awesome greatr work guys
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.