Results 1 to 10 of 10
  1. #1
    kleaverjr is offline Advanced Beginner
    Windows 7 64bit Access 2013 64bit
    Join Date
    Apr 2022
    Posts
    53

    Need Help Creating a ListBox in Module


    OK, here is the relevant part of the program I am working on:

    For X = 1 To 10

    XAsString = X

    If X < 10 Then

    XAsString = Chr(32) + XAsString

    End If

    Debug.Print XAsString


    Form_Register.Controls("lstbxItem1").AddItem (XAsString)

    Next X


    So when I look at the debug printout it shows the variable XAsString has the space before the single digit. However, when it's added to the ListBox the space is deleted.

    What I want to do is have the single digits Right Justified. I'm picky when it comes to how things are displayed on the screen. I don't want to use format with "@@" as it inserts a zero as the first digit for digits 1-9 and I don't want that either. I want it to have a space there. So how do I force the Listbox to keep the space when it's added to the list? Thanks again to all for your patience and help!

    Ken L

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    I tried chr(160) and it still was removed.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  4. #4
    davegri's Avatar
    davegri is online now Excess Access
    Windows 11 Access 2019
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,413
    Here's the simplest method I could devise

    Code:
    Private Sub Form_Load()
        Dim X As Long, XAsString As String
        For X = 1 To 10
            XAsString = X
            If X < 10 Then
                XAsString = Chr(34) & Space(2) & XAsString & Chr(34)
            Else
                XAsString = Chr(34) & XAsString & Chr(34)
            End If
            Debug.Print XAsString
            Form_Register.Controls("lstbxItem1").AddItem Trim((XAsString))
        Next X
    End Sub

  5. #5
    kleaverjr is offline Advanced Beginner
    Windows 7 64bit Access 2013 64bit
    Join Date
    Apr 2022
    Posts
    53
    Perfect! thank you. Now sure why why I used adding CHR(32) didn't work. Doesn't matter as this solution works. Thanks again!

    Ken L.

  6. #6
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    Code:
        Dim i As Integer, strOut As String
        
        Me.lstBx1.RowSource = ""
    
    
        For i = 1 To 10
            If Len(CStr(i)) = 1 Then
                Me.lstBx1.AddItem Chr(34) & Space(2) & i & Chr(34)
            Else
                Me.lstBx1.AddItem CStr(i)
            End If
        Next i
    Attached Thumbnails Attached Thumbnails lbx.jpg  
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  7. #7
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Chr(32) is a space. Chr(34) is quote mark. Enclosing string in quotes forces combobox to retain spaces. Works whether numbers or alphas. Can demonstrate with manual input of list in RowSource:

    1;2;3
    " 1";" 2";" 3"

    Each is treated different.

    Also, need to use non-proportional font to get correct alignment.

    However, this means user must type a space to move through items of listbox if didn't want to click for listbox set to single selection. Might not be intuitive for users.

    I don't see any real benefit.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  8. #8
    kleaverjr is offline Advanced Beginner
    Windows 7 64bit Access 2013 64bit
    Join Date
    Apr 2022
    Posts
    53
    OK, this solution works great for listbox's, so thank you for that. But when I tried this trick in a regular textbox, the quotes are showing. Here is the relevant part of the code. What am I doing wrong?

    Form_Register.Controls("txtSalesOrderNumber").Valu e = Chr(34) & Space(4) & SalesOrderNumberAsString & Chr(34)

    And the reason I use .value and not .text is on this form i want the textbox locked and not enabled and using .text requires the text box to be unlocked and enabled, but with .value I can change the value without making the other changes.

    Thanks again!

    Ken L

  9. #9
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Value is the default property, so you can leave it out altogether.
    Why are you using a complete form reference? if this code is in that form then Me would be sufficient.

    That code to me, would indicate you are updating some form other than the one that the code is running in?

    Textboxes have an Align property, so that code is not even needed?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  10. #10
    davegri's Avatar
    davegri is online now Excess Access
    Windows 11 Access 2019
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,413
    Here's code that will work without the quotes appearing

    Code:
    Private Sub Form_Load()
        Dim X As Long, XAsString As String
        Me.txtTest = ""
        For X = 1 To 10
            XAsString = X
            If X < 10 Then
                XAsString = Chr(160) & XAsString
                txtTest = txtTest & Chr(160) & XAsString & vbCrLf
            Else
                XAsString = XAsString
                txtTest = txtTest & XAsString & vbCrLf
            End If
            Debug.Print XAsString
            Form_Register.Controls("lstbxItem1").AddItem Trim((XAsString))
        Next X
    End Sub
    Click image for larger version. 

Name:	boxes.png 
Views:	25 
Size:	9.1 KB 
ID:	50550

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

Similar Threads

  1. Creating a Table using VBA code in a module
    By JohnLouisWood in forum Modules
    Replies: 28
    Last Post: 03-23-2023, 06:43 PM
  2. Replies: 4
    Last Post: 11-21-2018, 04:06 PM
  3. ListBox Module
    By Jllera in forum Modules
    Replies: 4
    Last Post: 10-30-2014, 07:19 AM
  4. Replies: 3
    Last Post: 08-16-2012, 11:16 AM
  5. Creating Macro from Module
    By Harley Guy in forum Modules
    Replies: 1
    Last Post: 11-08-2010, 07:44 AM

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