List Box to a Table

mlr0911

Registered User.
Local time
Today, 02:49
Joined
Oct 27, 2006
Messages
155
How can I get my list box values to a table?
I am using the list box "Row Source Type" as a "Value List" that is being passed information from code.

I need to take the information that is selected from this list box and transfer it to my table1.

Any thoughts?
 
Is your forms recordsource based on your table?
 
No, my form and listbox is unbound.
 
in the afterupdate event of the listbox you can do

table.fieldname= me.listboxname

* i think
 
That makes sense, however, since I am passing the information through vba, when I implement that particular piece of code (table.field=mylistbox; I even tried writing a sql statment) it is telling me "Invalid use of Null--Object Required" where mylistbox is null. It's like it isn't reading the information that is being displayed there. I have no clue on how to get this to work. I have been round and round with it. I am sure it's something simple that I am missing.

Thanks
 
Make sure you do a Me.Refresh before you call the code. This forces the listboxes values to be stored.

IE.

Me.Refresh
Table.Field = MyListBox
 
Thanks for your help;

However, I am getting an object variable not set error when adding this code. Here is what I have:

Private Sub Combo1_AfterUpdate()
Dim db As DAO.Database
Dim fld As DAO.Field
Dim strTable As String


Set db = CurrentDb()

strTable = Me!Combo1

For Each fld In db.TableDefs(strTable).Fields
'Debug.Print fld.Name

Me!List9.AddItem fld.Name


'I have even placed the example code here as well but doesn't work correctly.

Next fld

Me.Refresh
Table1.Field = fld.Name

End Sub
 
You never set your Table1 variable. But I think you need a little help in coding what you need accomplished.

When do you want to add data to the table? When you click on the listbox, or when you double click it. Or do you just want all values in the listbox to go to the table?

You should also be using the dot operator as opposed to the bang operator (period instead of exclamation point)

Your code should probably look something like this:
Code:
    [color=Blue]Dim [/color]db              [color=Blue]As [/color]DAO.Database
    [color=Blue]Dim [/color]rs              [color=Blue]As [/color]DAO.Recordset
    
    [color=Blue]Dim [/color]fld             [color=Blue]As [/color]DAO.Field
    [color=Blue]Dim [/color]strTable        [color=Blue]As [/color][color=Blue]String[/color]
    
    strTable = Me.Combo1
    [color=Blue]Set [/color]db = CurrentDb
    [color=Blue]Set [/color]rs = db.OpenRecordset(strTable, dbOpenDynaset)
    
    
  '[color=green]If showing heads change the 0 to a 1[/color]
    [color=Blue]For [/color]i = 0 [color=Blue]To [/color]Me.List9.ListCount - 1
        [color=Blue]With [/color]rs
            .Addnew
                .Fields("ID") = Me.List9.ItemData(i)
            .Update
        [color=Blue]End [/color][color=Blue]With[/color]
    [color=blue]Next[/color]
 
Last edited:
Have all variables goto the table after the list refreshes.

Thanks for your help.
 
I used the code, but it didn't place the field names into the table.

Thanks
 
Manually create the table. This puts the values in the table, it does not create the table.
 

Users who are viewing this thread

Back
Top Bottom