Jeff:
Iterate through the items in the list box and execute an SQL statement for
each to insert a row into the table. Rather than emptying the table
afterwards do so before inserting the rows. The code, which could go in the
Click event procedure of a button on the form for instance, would go like
this:
Dim cmd As ADODB.Command
Dim strSQL As String
Dim n As Integer
Dim ctrl As Control
Set ctrl = Me.[Current Accounts]
Set cmd = New ADODB.Command
cmd.ActiveConnection = CurrentProject.Connection
cmd.CommandType = adCmdText
' empty the table
strSQL = "DELETE * FROM [Acct Temp]"
cmd.CommandText = strSQL
cmd.Execute
' loop through list box's items
' and insert row into table for each
For n = 0 To ctrl.ListCount - 1
strSQL = "INSERT INTO [Acct Temp]" & _
"([Account Number]) " & _
"VALUES(" & ctrl.ItemData(n) & ")"
cmd.CommandText = strSQL
cmd.Execute
Next n
I've assumed the column in [Acct Temp] is called [Account Number] and is
number data type. If its text wrap the value in quotes:
strSQL = "INSERT INTO [Acct Temp]" & _
"([Account Number]) " & _
"VALUES(""" & ctrl.ItemData(n) & """)"
You don't really need to fill a temporary table for this, however. You
could base the report on a query which returns all rows from the main
accounts table(s) and open it from a button on the form filtered to the
selected accounts, like so:
Dim ctrl As Control
Dim strAcctList as String
Dim strFilter As String
Dim n as Integer
Set ctrl = Me.[Current Accounts]
' loop through list box's items
' and build a value list of account numbers
For n = 0 To ctrl.ListCount - 1
strAcctList = strAcctList & "," & ctrl.ItemData(n)
Next n
' remove leading comma
strAcctList = Mid(strAcctList,2)
strFilter = "[Account Number] In (" & strAcctList & ")"
' open report in print preview
DoCmd.OpenReport "YourReport", _
View:=acViewPreview, _
WhereCondition:=strFilter
Again If the account number column is text wrap the value in quotes:
strAcctList = strAcctList & ",""" & ctrl.ItemData(n) & """"
Ken Sheridan
Stafford, England
jas580 said:
I have a form with two list boxes, and add/remove buttons. The user moves
Account Numbers from the first box [Accounts] to the second [Current
Accounts].
Once users have selected the accounts they want in a report, I need those
account numbers to pass to a table [Acct Temp]. The report queries are based
on this table. I need to know how to retrieve those account numbers from the
list box and place them in the table. And, how to remove those account
numbers from the table after a report has been run.
Thanks,
Jeff