Hi Gicu,
Thanks for your suggestion. I have just tried this. Please see the code below.
Only now when I run the application I am getting Access error 3061 - Too few parameters expected three.
I don't really understand this because I am running the query in code and supplying the inputs. I also tried adding parameters in the code but that did not work and so I have commented these out.
I would appreciate if you know what is going on here?
Many thanks.
Code:
Private Sub cmdOK_Click()
On Error GoTo Err_cmdOK
Dim db As DAO.Database, qdf As DAO.QueryDef, strSQL As String, intReport As Integer
Set db = CurrentDb
'Append values to table tblReportsWorkspaces
strSQL = "INSERT INTO tblReportsWorkspaces ( [Report ID], [Workspace ID], Version ) " _
& "SELECT [Forms]![frmViewReportVersions]!lstReports AS Report_ID, " _
& "Me.lstWorkspace AS Workspace_ID, [Forms]![frmEditReportVersion]!txtVersion AS Version"
' Debug.Print strSQL
Set qdf = db.CreateQueryDef("", strSQL)
' qdf.Parameters(0) = [Forms]![frmViewReportVersions]!lstReports
' qdf.Parameters(1) = Me.lstWorkspace
' qdf.Parameters(2) = [Forms]![frmEditReportVersion]!txtVersion
With qdf
.SQL = strSQL
.Execute (dbFailOnError)
End With
If IsOpen("frmEditReportVersion") Then
Forms!frmEditReportVersion!lstWorkspaces.Requery
End If
If IsOpen("frmEditReportVersion") Then
Forms!frmEditReportVersion!lstWorkspaces.Requery
End If
gfAddWS = True
DoCmd.Close acForm, "frmSelectWorkspaceToAdd"
Exit_cmdOK:
If Not qdf Is Nothing Then 'Only close object if it is open
qdf.Close
Set qdf = Nothing
End If
Set db = Nothing
Exit Sub
Err_cmdOK:
Select Case Err.Number
Case 3075
Call MsgBox("You must select a workspace to add", vbCritical, "Reports Log Database")
Case Else
MsgBox Err.Number
Dim strMsg As String
strMsg = "Access Error " & Err.Number & vbCrLf _
& Err.Description & vbCrLf & vbCrLf & "New workspace record was not added to the database"
Call MsgBox(strMsg, vbCritical, "Reports Log Database")
End Select
End Sub