Results 1 to 11 of 11
  1. #1
    sheusz is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    May 2015
    Posts
    151

    Talking Strip first 4 characters from names in combo box

    Hello Inmates



    I am not sure if what I want to achieve is actually possible, but here goes.

    I have a form that contains a number of combo boxes. Each of the combo boxes lists all the reports in my DB whose report name starts with a particular prefix.

    I have given the reports a specific prefix based on the type of report. All the report prefixes are the same length (4 characters). In the example below the reports displayed in this combo box are Customer related.

    This system works really well from an end user point of view, however I would like to tidy up the listing in the combo box by not having the prefix (ie first 4 characters) of the report name displayed, so the report cus_COD Customers would be shown in the combo box as COD Customers

    The code is on the Row Source control of the combo box, and the Row Source Type is defined as Table/Query.

    Is this even possible?

    Code:
    SELECT MSysObjects.Name FROM MsysObjects WHERE (Left$([Name],1)<>"~") And (Left$([Name],4)="cus_") And (MSysObjects.Type)=-32764 ORDER BY MSysObjects.Name;
    Click image for larger version. 

Name:	Capture2.JPG 
Views:	19 
Size:	52.1 KB 
ID:	41498

    Thank you in advance for your suggestions.
    Attached Thumbnails Attached Thumbnails Capture.JPG  

  2. #2
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    Why not just iterate through the reports collection and include a column using the Mid() on the name property?
    I usually try to avoid using system tables.

    Using a value list combo use the .additem method (2 columns with 0;1 widths)
    Something like

    Code:
        Dim rpt As Variant
        For Each rpt In CurrentProject.AllReports
            If Left(rpt.Name, 4) = "cus_" Then
                Debug.Print Mid(rpt.Name, 5)
                'YourComboName.AddItem rpt.name & ";" & Mid(rpt.Name, 5) 
            End If
        Next

  3. #3
    sheusz is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    May 2015
    Posts
    151
    Thanks Moke123 for the reply. This seems like a good solution.

    I've added your code to the OnOpen Event of the form. Is that correct?

    Forgive my ignorance. How do I set the combo box row property to a value list in this case?

    Never mind. I had a brain f**t. I'll play around with this and advise.

  4. #4
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    heres an example

    rpts.zip

  5. #5
    sheusz is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    May 2015
    Posts
    151
    Hi Moke123

    Thank you very much for the sample. I did achieve the result after your initial post once I put my brain in to gear

    BTW, I've been trying to alphabetically order the combo box. Any ideas on how to do this?

  6. #6
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    you could try an array, then sort it, then add it to the combo. I dont have a sample sort procedure however.

  7. #7
    sheusz is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    May 2015
    Posts
    151
    Thanks. As always 3 steps forward, 1 step back.

    I like your overall solution over my original approach. I'll have to research the sort order issue. Never ventured in to arrays before, so time to roll up the sleeves.

    Thanks again for your help.

  8. #8
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,424
    if you keep to your sql solution change

    SELECT MSysObjects.Name

    to

    SELECT mid(MSysObjects.Name,5) as RName....

    you can then order by it as well

  9. #9
    sheusz is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    May 2015
    Posts
    151
    Hi Ajax

    Thanks for the input. I'm not sure what you mean by RName.. however I have changed my original code to;

    Code:
    SELECT mid(MSysObjects.Name,5) FROM MsysObjects WHERE (Left$([Name],1)<>"~") And (Left$([Name],4)="cus_") And (MSysObjects.Type)=-32764 ORDER BY MSysObjects.Name;
    and this has given me the sorted list I'm after, which is great, but now I am a bit torn.

    Of the two versions (mine and moke123's) which is the a more preferred option in terms of more robust application? Your suggestion gives me the correct sort order, but I'm not experienced enough to further develop moke123's suggestion.

  10. #10
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,424
    RName is an alias - Name is a reserved word and should not be used as a field name - MS do defy the rules however so you just have to live with it with regards their tables. Since this is for a rowsource you don't need an alias unless you are showing headers. run it as an ordinary query, you will see what I mean. It's just a good habit to get into

    Also with this part of your where clause

    WHERE (Left$([Name],1)<>"~") And (Left$([Name],4)="cus_")

    you only need this bit (Left$([Name],4)="cus_") because any other name will be excluded

    Which is more robust? probably the same. Go with the one you are most comfortable with.

  11. #11
    sheusz is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    May 2015
    Posts
    151
    Hi Ajax

    Thanks for the explanation. Now I understand

    I really appreciate both your and Moke123's input and solutions. The project is working as I would like it and looks "polished" for the end user.

    Once again the generosity of members of this forum seems to know no bounds.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 01-05-2018, 10:02 PM
  2. strip space if after a comma
    By auerdl in forum Modules
    Replies: 4
    Last Post: 07-16-2016, 08:51 PM
  3. Replies: 1
    Last Post: 05-02-2013, 10:57 AM
  4. Replies: 1
    Last Post: 04-17-2013, 04:38 PM
  5. function to strip characters
    By Kirsti in forum Queries
    Replies: 2
    Last Post: 11-01-2012, 07:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums