This is the entire code of the event.
All works well and as intended until the
Set rsAssetUpd = db.OpenRecordset(sSQL) code.
Option Compare Database
Option Explicit
Private Sub Command10_Click()
' Set variables & values
Dim cmbvalue As String 'For CCAN
Dim dateval As Date 'For date
Dim var_audit As Long 'For audit_#
Dim var_sub_audit As Long 'For audit_sub_#
Dim strSQL As String 'For retrieving the audit_#
Dim stDocName As String 'For menu changing
Dim stLinkCritaria As String 'For menu changing
'On Error GoTo ErrorHandler
cmbvalue = Me.Combo8.Value
dateval = Now()
stDocName = "menu_audit"
' Select the table tbl_audit_gener, add the new record, create the audit_# and close the form.
DoCmd.OpenTable "tbl_audit_gener"
DoCmd.GoToRecord , , acNewRec
ccan = cmbvalue
audit_generated_date = dateval
DoCmd.Save
DoCmd.Close
DoCmd.Close acForm, "audit_#_generator"
' Select the table tbl_audit_gener, retrieve the audit_# record based on the variables
Dim db As Database
Dim rsAudNum As Recordset
Set db = CurrentDb
Set rsAudNum = db.OpenRecordset("tbl_audit_gener")
var_audit = DLookup("[audit_#]", "[tbl_audit_gener]", "ccan='" & cmbvalue & "' AND audit_generated_date=#" & dateval & "#")
' Select the table tbl_audit, create a new record, and use the variables as default for the field
Dim rsAudit As Recordset
Set db = CurrentDb
Set rsAudit = db.OpenRecordset("tbl_audit")
rsAudit.AddNew
rsAudit!ccan = cmbvalue
rsAudit!audit_prepost_date = dateval
rsAudit!audit_no = var_audit
rsAudit!audit_status = "Pre-Posted"
rsAudit!audit_status_date = dateval
rsAudit.Update
' Select tbl_audit, retrieve the audit_sub_# and store in a variable to put into tbl_extra_asset
Dim rsAudSubNum As Recordset
Set db = CurrentDb
Set rsAudSubNum = db.OpenRecordset("tbl_audit")
var_sub_audit = DLookup("[audit_sub_#]", "[tbl_audit]", _
"ccan='" & cmbvalue & "' AND audit_no =" & var_audit & " AND audit_prepost_date=#" & dateval & "#")
' Select the table tbl_extra_asset, select all assets subject to the pre-audit generated
' then append these assets in the tbl_extra_assets where values are equal to the variables accumulated.
Dim rsAssetUpd As Recordset
Dim sSQL As String
sSQL = "INSERT INTO [tbl_extra_asset]"
sSQL = sSQL & vbLf
sSQL = sSQL & "([ccan], [audit_#], [audit_sub_#],[contract_id], [asset_id])"
sSQL = sSQL & vbLf
sSQL = sSQL & "SELECT"
sSQL = sSQL & " [tbl_customer].[ccan]"
sSQL = sSQL & ", [tbl_audit].[audit_no]"
sSQL = sSQL & ", [tbl_audit].[audit_sub_#]"
sSQL = sSQL & ", [tbl_contract].[contract_id]"
sSQL = sSQL & ", [tbl_asset].[asset_id]"
sSQL = sSQL & vbLf
sSQL = sSQL & "FROM [tbl_customer] "
sSQL = sSQL & "LEFT JOIN (([tbl_audit] "
sSQL = sSQL & "LEFT JOIN [tbl_contract]"
sSQL = sSQL & " ON [tbl_audit].[ccan] = [tbl_contract].[ccan])"
sSQL = sSQL & "LEFT JOIN [tbl_asset]"
sSQL = sSQL & " ON [tbl_contract].[contract_id] = [tbl_asset].[contract_id])"
sSQL = sSQL & " ON [tbl_customer].[ccan] = [tbl_audit].[ccan]"
sSQL = sSQL & vbLf
sSQL = sSQL & "WHERE ((([tbl_customer].[ccan]) ='" & 215330 & "')"
sSQL = sSQL & " AND (([tbl_audit].[audit_no]) =" & 92 & ")"
sSQL = sSQL & " AND (([tbl_audit].[audit_sub_#]) =" & 45 & ")"
sSQL = sSQL & " AND (([tbl_asset].[asset_id]) Is Not Null));"
Set rsAssetUpd = db.OpenRecordset(sSQL)
MsgBox "You have created an audit for CCAN:" & cmbvalue & "and the audit number is: " & var_audit & ". Take note of the audit number.", vbOKOnly, "Audit Number Confirmation"
rsAudNum.Close
rsAudit.Close
rsAudSubNum.Close
rsAssetUpd.Close
' Go to menu_audit
DoCmd.OpenForm stDocName, , , stLinkCritaria
ExitHere:
Exit Sub
'ErrorHandler:
'If Err.Number = 94 Then
'MsgBox "Please select a Customer Name and Audit Type before issuing an audit report", vbOKOnly, "Customer Select Error"
'Else
'MsgBox "Take note of the error number and description " & Err.Number & vbCrLf & Err.Description & " Contact XXX at XXX-XXX-XXXX with that information", vbOKOnly, "Unplanned Error"
'End If
'Resume ExitHere
End Sub
If I did not say it before, I really appreciate the time and expertise of the users on this forum.