Results 1 to 5 of 5

Thread: [RESOLVED] Add New Item to ListBox

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2007
    Location
    Melbourne, Australia
    Posts
    362

    Resolved [RESOLVED] Add New Item to ListBox

    using Access only, no VB or other front ends used in this app.

    i would like to know how to add a new item to a listbox with two columns at the moment my code looks like this:

    Code:
        If txtDate = "" Then
            MsgBox "You must enter a Date for the Operation First, and Click Add.", vbOKOnly, "Error - Missing Date"
            Exit Sub
        Else
            If txtTime = "" Then
                MsgBox "You must enter a Time for the Operation First, and Click Add.", vbOKOnly, "Error - Missing Time"
                Exit Sub
            Else
                lstOperationSummary.AddItem ("Date: " & txtDate)
                lstOperationSummary.AddItem ("Time: " & txtTime)
            End If
        End If
    however this only writes the "("Date: " & txtDate)" to one column, i wish to have them split so "Date:" goes in the first and txtDate goes in the second.

    thanks

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Add New Item to ListBox

    Quote Originally Posted by aztrix
    using Access only, no VB or other front ends used in this app.

    i would like to know how to add a new item to a listbox with two columns at the moment my code looks like this:

    Code:
        If txtDate = "" Then
            MsgBox "You must enter a Date for the Operation First, and Click Add.", vbOKOnly, "Error - Missing Date"
            Exit Sub
        Else
            If txtTime = "" Then
                MsgBox "You must enter a Time for the Operation First, and Click Add.", vbOKOnly, "Error - Missing Time"
                Exit Sub
            Else
                lstOperationSummary.AddItem ("Date: " & txtDate)
                lstOperationSummary.AddItem ("Time: " & txtTime)
            End If
        End If
    however this only writes the "("Date: " & txtDate)" to one column, i wish to have them split so "Date:" goes in the first and txtDate goes in the second.

    thanks
    For multiple-column lists, use semicolons to delimit the strings for each column (for example, "1010;red;large" for a three-column list). If the Item argument contains fewer strings than columns in the control, items will be added starting with the left-most column. If the Item argument contains more strings than columns in the control, the extra strings are ignored.

    for example in the above code

    Code:
    lstOperationSummary.AddItem "Date: ;" & txtDate.Value
    Same goes for the second line
    Last edited by Siddharth Rout; May 31st, 2008 at 01:50 AM.
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2007
    Location
    Melbourne, Australia
    Posts
    362

    Re: Add New Item to ListBox

    sweet thanks very much!

    now to add to this question, say i have a table that i want to save the listbox data in, how would i then send the contents of the listbox to the table?

    my code so far looks like this:

    Code:
    Dim lngCounter As Long
    lngCounter = 0
    
        For lngCounter = 0 To lstOperationSummary.ListCount - 1
                Me.Recordset.AddNew
                Me.Recordset.Fields("Detail") = lstOperationSummary.ItemData(lngCounter)
                Me.Recordset.Update
            If lstOperationSummary.Selected(lngCounter) Then
    
            End If
            Next lngCounter
        
    
        MsgBox "Operation Details Saved", vbOKOnly, "Saved"
    however, this writes blank fields to my database table

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2007
    Location
    Melbourne, Australia
    Posts
    362

    Re: Add New Item to ListBox

    ok thanks to you koosid, i now have data being written to my table, it was writting the second column of the listbox, and without your code, i had nothing in the second column only the first, hence creating blank records in my table.

    so............how do i get it to write the first column and second column?
    Last edited by aztrix; May 31st, 2008 at 02:15 AM.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2007
    Location
    Melbourne, Australia
    Posts
    362

    Re: [RESOLVED] Add New Item to ListBox

    Never mind worked it out myself

    here it is:

    Code:
    Dim lngCounter As Long
    lngCounter = 0
    
        For lngCounter = 0 To lstOperationSummary.ListCount - 1
                Me.Recordset.AddNew
                Me.lstOperationSummary.BoundColumn = 1
                Me.Recordset.Fields("Code") = lstOperationSummary.ItemData(lngCounter)
                Me.lstOperationSummary.BoundColumn = 2
                Me.Recordset.Fields("Detail") = lstOperationSummary.ItemData(lngCounter)
                Me.Recordset.Update
            If lstOperationSummary.Selected(lngCounter) Then
    
            End If
            Next lngCounter
        
    
        MsgBox "Operation Details Saved", vbOKOnly, "Saved"

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