J
Jeff Sievenpiper
Good day,
I am trying to use a DAO Recordset with a runtime SQL expression. However,
when I try and code the Openrecordset model, instead of 'source' (where the
SQL string goes) I get 'Name as String'. When I run the code I get an error
stating the object "<my SQL expression>" could not be found.
I have referenced the Microsoft DAO 3.6 and it is only one of three
references for this database, the other two being VBA for application and the
Access 11.0 references.
I would greatly appreciate any assistance on getting this SQL expression to
work for this recordset.
Code snippet is as follows:
Option Compare Database
Public rst As DAO.Recordset, dbs As DAO.Database, strSQL As String
Private Sub NewItem_Click()
On Error GoTo Err_NewItem_Click
'DoCmd.GoToRecord , , acNewRec
Set dbs = CurrentDb
'On Error Resume Next
strSQL = "SELECT * from PIMBody WHERE PIMNumber = """ &
Forms![PIM]![PIMNumber].Value & """"
Set rst = dbs.OpenRecordset(strSQL, dbOpenTable)
If rst.RecordCount = 0 Then
On Error GoTo Err_NewItem_Click
Set rst = dbs.OpenRecordset("PIMBody", dbOpenTable)
rst.AddNew
rst![PIMNumber].Value = Forms![PIM].[PIMNumber].Value
rst![ItemNumber].Value = 1
rst![Status].Value = "A"
rst.Update
Forms![PIM]![PIMBodyQuery subform].Requery
Else
rst.MoveFirst
rst.MoveLast
Forms![PIM]![PIMBodyQuery subform].[ItemNumber].Value =
(rst![ItemNumber].Value + 1)
Forms![PIM]![PIMBodyQuery subform].[ItemNumber].SetFocus
End If
Exit_NewItem_Click:
Exit Sub
Err_NewItem_Click:
MsgBox Err.Description
Resume Exit_NewItem_Click
End Sub
I am trying to use a DAO Recordset with a runtime SQL expression. However,
when I try and code the Openrecordset model, instead of 'source' (where the
SQL string goes) I get 'Name as String'. When I run the code I get an error
stating the object "<my SQL expression>" could not be found.
I have referenced the Microsoft DAO 3.6 and it is only one of three
references for this database, the other two being VBA for application and the
Access 11.0 references.
I would greatly appreciate any assistance on getting this SQL expression to
work for this recordset.
Code snippet is as follows:
Option Compare Database
Public rst As DAO.Recordset, dbs As DAO.Database, strSQL As String
Private Sub NewItem_Click()
On Error GoTo Err_NewItem_Click
'DoCmd.GoToRecord , , acNewRec
Set dbs = CurrentDb
'On Error Resume Next
strSQL = "SELECT * from PIMBody WHERE PIMNumber = """ &
Forms![PIM]![PIMNumber].Value & """"
Set rst = dbs.OpenRecordset(strSQL, dbOpenTable)
If rst.RecordCount = 0 Then
On Error GoTo Err_NewItem_Click
Set rst = dbs.OpenRecordset("PIMBody", dbOpenTable)
rst.AddNew
rst![PIMNumber].Value = Forms![PIM].[PIMNumber].Value
rst![ItemNumber].Value = 1
rst![Status].Value = "A"
rst.Update
Forms![PIM]![PIMBodyQuery subform].Requery
Else
rst.MoveFirst
rst.MoveLast
Forms![PIM]![PIMBodyQuery subform].[ItemNumber].Value =
(rst![ItemNumber].Value + 1)
Forms![PIM]![PIMBodyQuery subform].[ItemNumber].SetFocus
End If
Exit_NewItem_Click:
Exit Sub
Err_NewItem_Click:
MsgBox Err.Description
Resume Exit_NewItem_Click
End Sub