Results 1 to 5 of 5

Thread: [RESOLVED] Commas in Combobox problem

Hybrid View

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2004
    Posts
    30

    Resolved [RESOLVED] Commas in Combobox problem

    Here's a quick example of my problem.
    Code:
    (1) People's Lawyers
    (2) Frank's Lawfirm
    (3) Smith
    Smith
    Smith and Paul
    (4) ABC Law
    Here's how it should look
    Code:
    (1) People's Lawyers
    (2) Frank's Lawfirm
    (3) Smith, Smith, Smith and Paul
    (4) ABC Law
    The combobox is populated by code from a database, so it is set up as ValueList. What can I do to get around the issue of the listbox taking the comma and seperating the data? Heres my code for the AddItem routine.
    vb Code:
    1. Function PopulateOrganization()
    2.     Dim db As Database
    3.     Dim rstORG As DAO.Recordset
    4.     Dim SQL
    5.     Dim i As Long
    6.    
    7.     Set db = CurrentDb()
    8.     SQL = "SELECT org_id, org_desc FROM ORGANIZATIONS ORDER BY org_desc ASC"
    9.    
    10.     Set rstORG = db.OpenRecordset(SQL)
    11.     rstORG.MoveFirst
    12.     rstORG.MoveNext
    13.    
    14.     i = 0
    15.     Do While i < cmbOrganization.ListCount
    16.         cmbOrganization.RemoveItem (i)
    17.     Loop
    18.     rstORG.MoveFirst
    19.     Do While Not rstORG.EOF
    20.         cmbOrganization.AddItem "(" & rstORG!org_id & ") " & (Trim(rstORG!org_desc))
    21.         rstORG.MoveNext
    22.     Loop
    23. End Function

    Any ideas would be greatly appriciated! Thanks in advanced.

    ~Chris

  2. #2
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: Commas in Combobox problem

    I haven't seen that problem with commas before. There are often errors with quotes. Try a Replace() on the commas with 2 commas. Just an idea.
    Tengo mas preguntas que contestas

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jul 2004
    Posts
    30

    Resolved Re: Commas in Combobox problem

    Well,
    The replace function does work, but not the way you intended it to. I would have to use another character besides a comma in order for it to display. For example a period, but that doesn't look right and I would still prefer displaying a comma. Your idea of replaceing with two commas just makes more rows.

    Anyone else have any suggestions on the issue?

    EDIT: I ended up solving my own problem. What I ended up doing was utilizing the replace function to replace the comma with another comma. Except it was a different character. To help explain better heres my replacement code.

    vb Code:
    1. Do While Not rstORG.EOF
    2.         orgTEXT = (Trim(rstORG!org_desc))
    3.         If InStr(1, (Trim(rstORG!org_desc)), ",") > 0 Then
    4.             orgTEXT = Replace(rstORG!org_desc, ",", Chr(130))
    5.         End If
    6.         cmbOrganization.AddItem (Trim(orgTEXT)) & " (" & rstORG!org_id & ")"
    7.         rstORG.MoveNext
    8.     Loop
    What it does is replace the character 44 with character 130. Which are both commas. But because the combobox/listbox takes the character 44 as a row seperater, this is a workaround. Hope this helps anyone out.

    Thanks,
    ~Chris
    Last edited by ChrisR873; Apr 5th, 2007 at 01:20 PM. Reason: Resolved

  4. #4
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: [RESOLVED] Commas in Combobox problem

    I just created a database and did what you did, but using ADO. I couldn't duplicate your results. The
    Code:
    Private Sub Form_Activate()
    Dim cn As ADODB.Connection
    Dim rs As ADODB.Recordset
    
      Set cn = New ADODB.Connection
      cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\db1.mdb;Persist Security Info=False"
      Set rs = New ADODB.Recordset
      rs.Open "Select * from Table1", cn
      Do While Not rs.EOF
        Combo1.AddItem (rs.Fields(0).Value) & "   (" & rs.Fields(1).Value & ")"
        rs.MoveNext
      Loop
      rs.Close
      Set rs = Nothing
      cn.Close
      Set cn = Nothing
      If Combo1.ListCount > 0 Then Combo1.ListIndex = 0
    
    End Sub
    Name:  Combo.jpg
Views: 226
Size:  13.6 KB
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jul 2004
    Posts
    30

    Re: [RESOLVED] Commas in Combobox problem

    I don't think VB6 has that issue, which is why I was so confused. I am using MS Access 2002 to create my database app, and the data comes via DAO. But it didn't matter how the data came when populating the control, it did it no matter what. If you have access 2002 or 2000 try it there. Glad you looked into the issue tho. I did noticed when researching how to fix it before I posted that other people had the same issue, but none were pulling from a database and were able to fix it another way. My fix works and thats one problem out of the way.

    ~Chris

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width