Results 1 to 14 of 14
  1. #1
    JB510's Avatar
    JB510 is offline Advanced Beginner
    Windows 11 Office 365
    Join Date
    Jan 2023
    Location
    Ontario
    Posts
    44

    how to add txt to a row in a Listbox?


    I have a listbox with values in it (One column, 6 rows).
    If I want a simple one liner to add text into the 4th row, how is it done?

    Me.Listbox1.ItemData(4) = "Text to enter"

    This does not work.
    How do I do it?

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    add the value to the table. Listbox/Combo boxs are based on query/table.

    if your combo/List is based on VALUE LIST, (listbox.RowSourceType) then
    lstbox.Rowsource = "red;blue;Text To Enter"

  3. #3
    JB510's Avatar
    JB510 is offline Advanced Beginner
    Windows 11 Office 365
    Join Date
    Jan 2023
    Location
    Ontario
    Posts
    44
    Quote Originally Posted by ranman256 View Post
    add the value to the table. Listbox/Combo boxs are based on query/table.

    if your combo/List is based on VALUE LIST, (listbox.RowSourceType) then
    lstbox.Rowsource = "red;blue;Text To Enter"
    Thank you for the quick reply.. I have no errors now but listbox2 will not populate with anything. Is there a trick to getting the list to update? (Listbox1 does populate and Rowsource does count up and the "NOS" values does execute in the if statement properly to make Listbox2.Rowsource = ".." etc properly.) Listbox2 just does not update at all. It remains blank.

    Dim str As String
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    Dim LOSN As Integer
    Dim NOS As Integer
    Dim SellableP As Boolean
    Dim Rowsource As Integer

    On Error GoTo ErrorHandler

    Listbox1.Enabled = True
    Listbox2.Enabled = True

    Set db = CurrentDb

    'UnLock controls
    Listbox1.Enabled = True
    Listbox2.Enabled = True
    txtIIRNumber.Enabled = True

    'Update Listbox1
    Forms!Build1F!Listbox1.Requery

    'Go through part numbers in listed BOM and check for SNs - if Main PN = Shipable, put XXXX in IIR #
    For Rowsource = 0 To Listbox1.ListCount - 1
    With Listbox1
    str = ("SELECT * FROM PartsT WHERE EngineeringPN = " & "'" & Me.Listbox1.ItemData(Rowsource) & "'")
    End With
    Set rs = db.OpenRecordset(str)
    If rs.RecordCount = 1 Then

    With rs
    LOSN = Nz(!LOSN, 0)
    NOS = Nz(!NOS, 0)
    SellableP = !SellablePart
    End With

    With Listbox2
    If NOS > 0 And SellableP = True Then Listbox2.Rowsource = "XXXXX" 'Shippable parts get xxx in List2
    If LOSN = 0 And NOS = 0 Then Listbox2.Rowsource = "...."
    If LOSN > 0 And NOS = 1 Then Listbox2.Rowsource = "."
    If LOSN > 0 And NOS = 2 Then Listbox2.Rowsource = ".."
    If LOSN > 0 And NOS = 3 Then Listbox2.Rowsource = "..."
    End With

    End If
    Next Rowsource

    Me.Listbox2.Requery

    'UnLock controls
    txtIIRNumber.Enabled = True
    txtIIRNumber.SetFocus

    Screen.MousePointer = vbDefault
    Exit Sub

    ErrorHandler:

    Screen.MousePointer = vbDefault
    Set rstTrumcardII = Nothing
    m = MsgBox(Err.Description, vbInformation, "Error Choosing PartNumber to Build")

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    You are overwriting rowsource on each iteration?
    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

  5. #5
    JB510's Avatar
    JB510 is offline Advanced Beginner
    Windows 11 Office 365
    Join Date
    Jan 2023
    Location
    Ontario
    Posts
    44
    Quote Originally Posted by Welshgasman View Post
    You are overwriting rowsource on each iteration?
    I don't know the commands and I am confused when looking it up.

    Ex: How would I write "ABCD" in line 3 of a listbox?

  6. #6
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 10 Access 2016
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,614
    Can you post what you have as the Row Source property of your combo
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  7. #7
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    Quote Originally Posted by JB510 View Post
    I don't know the commands and I am confused when looking it up.

    Ex: How would I write "ABCD" in line 3 of a listbox?
    Go with a table and a sort order. SO much easier and more flexible.
    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

  8. #8
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    There seems to be so much wrong with that code.
    - Never use a reserved word for any object (Rowsource).
    - This is in 2x (ok, not harmful)
    Listbox1.Enabled = True
    Listbox2.Enabled = True
    - First iteration of your loop creates a sql string which you then create a rs from, then set LOSN, NOS to some number that comes from rs fields. On next iteration, if sql returns a different set of records, you overwrite LOSN, NOS values until in the end, you end up with whatever the last recordset returns. Makes no sense.
    - One sets the rowsource property to a table or query name, a sql statement or semicolon separated values. You're setting it to be a number of dots or X's??

    You're complicating this with all that code. Can you not base the listbox on a table or query as per post 2?
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  9. #9
    JB510's Avatar
    JB510 is offline Advanced Beginner
    Windows 11 Office 365
    Join Date
    Jan 2023
    Location
    Ontario
    Posts
    44
    Quote Originally Posted by Bob Fitz View Post
    Can you post what you have as the Row Source property of your combo
    The following command worked on the older version of access:

    Listbox2.List(4) = "...."
    I assume I cannot use .List(X) anymore to choose the row in the listbox?

    This does not work anymore.
    The row source is from the if statement in the code above. (I guess I can't use 'rowsource' as a variable as I misinterpreted shown above.)

    I set LOSN and NOS variables (shown above) from a Recordset dependent of Listbox1. It works and get the values each time as it cycles down.
    Based on these values of LOSN and NOS, I want it to up date Listbox2. (Only one column)

    Ex.

    Listbox1 ListBox2
    PartNumber1 ..
    PartNumber2 .
    PartNumber3 ....
    PartNumber4 ....
    PartNumber5 ....
    PartNumber6 ....

  10. #10
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    I wonder if Bob meant to ask what your rowsourcetype property is? If it's not a value list I don't think you can do what you're trying to do. That might be the difference - 1 is a value list, 2 is not?
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  11. #11
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    Quote Originally Posted by JB510 View Post
    The following command worked on the older version of access:

    Listbox2.List(4) = "...."
    I assume I cannot use .List(X) anymore to choose the row in the listbox?

    This does not work anymore.
    The row source is from the if statement in the code above. (I guess I can't use 'rowsource' as a variable as I misinterpreted shown above.)

    I set LOSN and NOS variables (shown above) from a Recordset dependent of Listbox1. It works and get the values each time as it cycles down.
    Based on these values of LOSN and NOS, I want it to up date Listbox2. (Only one column)

    Ex.

    Listbox1 ListBox2
    PartNumber1 ..
    PartNumber2 .
    PartNumber3 ....
    PartNumber4 ....
    PartNumber5 ....
    PartNumber6 ....
    Yes, but you are doing it the most complicated way?

    If you insist on a value list, perhaps research a little?
    https://www.google.com/search?q=repl...hrome&ie=UTF-8
    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

  12. #12
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    see if this helps. I could find nothing that suggests what you say worked before can be done. If you have a reference can you post it?

    https://learn.microsoft.com/en-us/of...istbox.additem
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  13. #13
    JB510's Avatar
    JB510 is offline Advanced Beginner
    Windows 11 Office 365
    Join Date
    Jan 2023
    Location
    Ontario
    Posts
    44
    Thank you!!!! Thank you all for your time. I fixed it by subbing in the following:

    If LOSN = 0 And NOS = 0 Then Me.Listbox2.AddItem Item:="...." & ", Index:=X"
    If LOSN > 0 And NOS = 1 Then Me.Listbox2.AddItem Item:="." & ", Index:=X"
    If LOSN > 0 And NOS = 2 Then Me.Listbox2.AddItem Item:=".." & ", Index:=X"
    If LOSN > 0 And NOS = 3 Then Me.Listbox2.AddItem Item:="..." & ", Index:=X"

    Where X was the line I wanted it on..

  14. #14
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    Glad to see you have a solution now. Maybe mark this one as solved then?
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

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

Similar Threads

  1. Replies: 3
    Last Post: 12-03-2020, 05:11 PM
  2. Replies: 1
    Last Post: 01-31-2015, 09:03 PM
  3. Replies: 2
    Last Post: 03-23-2014, 06:50 AM
  4. Replies: 3
    Last Post: 12-13-2012, 04:40 AM
  5. Replies: 1
    Last Post: 09-10-2012, 11:21 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