I have an Input form with an AutoNumber. The user enters a new record and a popup form appears with a multi select list box. The user then selects the approporiate "SubProjects" and clicks a button to run the code below.
[bold]The idea is to create ONE line for EACH SubProject. There is no join required. [/bold]
Selected in the list box. I have something similar to this but it goes against a static predetermined table but I now need to have the users perform the List box selection.
I call a procedure which creates a Disconnected Recordset which is new to me but seems to work. I actually debug.Print the recordset values just before I run my SQL and they appear to still exist.
[bold]The problems is when I run the SQL I receive an error that JET cannot find the recordset (rst)!
'##############################
'RunTime Error '3078'
'The microsoft jet Database engine cannot find the input table 'rst'
'Make sure it exists and that the name is spelled correct.
'#######################
How do I use an existing recordset in a SQL query?
[/bold]
[bold]The idea is to create ONE line for EACH SubProject. There is no join required. [/bold]
Selected in the list box. I have something similar to this but it goes against a static predetermined table but I now need to have the users perform the List box selection.
I call a procedure which creates a Disconnected Recordset which is new to me but seems to work. I actually debug.Print the recordset values just before I run my SQL and they appear to still exist.
[bold]The problems is when I run the SQL I receive an error that JET cannot find the recordset (rst)!
'##############################
'RunTime Error '3078'
'The microsoft jet Database engine cannot find the input table 'rst'
'Make sure it exists and that the name is spelled correct.
'#######################
How do I use an existing recordset in a SQL query?
[/bold]
Code:
Private Sub cmdAssignSubProjects_Click()
Dim VarItem As Variant
Dim intNewID As Integer
Dim SQL As String
[COLOR=green]'READ UNIQUE IDENTIFIER FROM FORM [/color]
intNewID = Forms!tblSpecNoticeMaster.intID
[COLOR=green]'CREATE RECORDSET (rst)[/color]
Call CreateRSTfromListBox
[COLOR=GREEN]'DEBUG.PRINT CONTENTS OF RECORDSET TO MAKE SURE IT STILL EXISTS WHEN RETURNED TO THIS PROCEDURE.[/COLOR]
rst.MoveFirst
Do Until rst.EOF
Debug.Print rst.Fields(0)
rst.MoveNext
Loop
[COLOR=GREEN]'2 lines are printed out so the recordset exists with two lines of data[/color]
SQL = "INSERT INTO tblVSRSubProject( intID,txtSubProject ) " _
& "SELECT " & intNewID & ", rst.fields(0) " _
& "FROM rst ;"
Debug.Print SQL
[COLOR=green]
When I paste the SQL from the Debug window into a blank query the RST appears like a table but without any fields.
'ERROR OCCURS HERE when I attempt to run the SQL.
'##############################
'RunTime Error '3078'
'The microsoft jet Database engine cannot find the input table 'rst'
'Make sure it exists and that the name is spelled correct.
'#######################[/color]
DoCmd.RunSQL SQL
End Sub
---------------------------------------------------
Sub CreateRSTfromListBox()
Dim varItemSel As Variant
Set rst = New ADODB.Recordset
rst.Fields.Append "txtSubProject", advarChar, 10
rst.Open
For Each varItemSel In Me.lstboxSubProject.ItemsSelected
rst.AddNew
rst!txtSubProject.Value = Me.lstboxSubProject.ItemData(varItemSel)
rst.Update
Next varItemSel
rst.MoveFirst
Do Until rst.EOF
Debug.Print rst.Fields(0)
rst.MoveNext
Loop
End Sub