Create table from Listbox selection

  • Thread starter KHogwood-Thompson
  • Start date
K

KHogwood-Thompson

Hi All,

I have a form with a listbox that collects user selection and when a command
button is clicked, a query is created based on that selection.

I would like a table created based on the selection rather than a query, can
someone help modify the following code:

Private Sub cmdOK_Click()
On Error GoTo Err_cmdOK_Click

Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim strSQL As String, strWhere As String
Dim i As Integer

Set db = CurrentDb

'*** create the query based on the information on the form
strSQL = "SELECT BLAKEJOBTRANS.* FROM BLAKEJOBTRANS"
strWhere = " Where [JOBREF] IN( "
For i = 0 To lbJobRef.ListCount - 1
If lbJobRef.Selected(i) Then
strWhere = strWhere & "'" & lbJobRef.Column(0, i) & "', "
End If
Next i
strWhere = Left(strWhere, Len(strWhere) - 2) & ");"
strSQL = strSQL & strWhere
MsgBox strSQL
'*** delete the previous query
db.QueryDefs.Delete "SELECTEDJOBS"
Set qdf = db.CreateQueryDef("SELECTEDJOBS", strSQL)

'*** open the query
'DoCmd.OpenQuery "SELECTEDJOBS", acNormal, acEdit

Exit_cmdOK_Click:
Exit Sub

Err_cmdOK_Click:
If Err.Number = 3265 Then '*** if the error is the query is missing
Resume Next '*** then skip the delete line and resume on
the next line
Else
MsgBox Err.Description '*** write out the error and exit the sub
Resume Exit_cmdOK_Click
End If
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top