1

I am new to MS Access and can't find the problem here. My code worked from the beginning, but now I keep getting this error when trying to delete a data record. Here is my code:

Private Sub cmdLoeschen_Click()
    If MsgBoxYesNo(CancelOrderConfirmPrompt) Then
        If [Prozessspezifikationen].Delete(Me![Prozessspezifikation ID]) Then
            MsgBoxOKOnly CancelOrderSuccess
            eh.TryToGoToRecord acNewRec
        Else
            MsgBoxOKOnly CancelOrderFailure
        End If
    End If
End Sub

The Delete function used:

Function Delete(ProzessspezifikationID As Long) As Boolean
    Dim rsw As New RecordsetWrapper
    If rsw.OpenRecordset("Prozessspezifikationen", "[Prozessspezifikation ID] = " & ProzessspezifikationID) Then
        Delete = rsw.Delete
    End If
End Function

where:

  • [Prozessspezifikation ID] is the primary key in the underlying table
  • [Prozessspezifikationen] is an autoincrement long integer
  • the table contains several foreign keys

I use the following query to get the data needed in my Formular:

SELECT Prozessspezifikationen.*, Products.Bezeichnung, Products.ZeichnungsNr 
FROM Products
INNER JOIN Prozessspezifikationen 
   ON Products.ArtikelID = Prozessspezifikationen.ArtikelID;

As I am new to MS Access and it doesn't seem to be a syntax error, I am a bit lost.

RecordsetWrapper (excerpt):

Option Compare Database
Option Explicit

Private m_rs As DAO.Recordset2

Public Function GetRecordsetClone(rs As DAO.Recordset2) As DAO.Recordset2
    If Not m_rs Is Nothing Then
        Debug.Assert False
    Else
        Set m_rs = rs.Clone
        Set GetRecordsetClone = m_rs
    End If
End Function

OpenRecordset Function

Public Function OpenRecordset(Domain As String, _
                              Optional Criteria As String = "1=1", _
                              Optional OrderBy As String, _
                              Optional RecordsetType As DAO.RecordsetTypeEnum = dbOpenDynaset, _
                              Optional RecordsetOptions As DAO.RecordsetOptionEnum _
                              ) As Boolean
    
    
    If Not m_rs Is Nothing Then
        CloseRecordset
    End If
    
    Dim strSQL As String
    strSQL = "SELECT * FROM [" & Domain & "] WHERE " & Criteria
    
    If OrderBy <> "" Then
        strSQL = strSQL & " ORDER BY " & OrderBy
    End If
    
    On Error GoTo ErrorHandler
    Set m_rs = CurrentDb.OpenRecordset(strSQL, RecordsetType, RecordsetOptions)
    OpenRecordset = True

Done:
    Exit Function
ErrorHandler:
    Debug.Assert m_rs Is Nothing
    
    If eh.LogError("RecordsetWrapper.OpenRecordset", "strSQL = " & Chr(34) & strSQL & Chr(34)) Then Resume
End Function

Delete Function

Public Function Delete() As Boolean
    On Error GoTo ErrorHandler
    
    m_rs.Delete
    Delete = True
    
Done:
    Exit Function
ErrorHandler:
    If eh.LogError("RecordsetWrapper.Delete") Then Resume
End Function

CloseRecordset Function

Public Function CloseRecordset() As Boolean
    On Error GoTo ErrorHandler
    
    m_rs.Close
    CloseRecordset = True
    
Done:
    Set m_rs = Nothing
    Exit Function
ErrorHandler:
    If eh.LogError("RecordsetWrapper.CloseRecordset") Then Resume
End Function


Public Property Get Recordset() As DAO.Recordset2
    Set Recordset = m_rs
End Property


Private Sub Class_Terminate()
    If Not m_rs Is Nothing Then
        m_rs.Close
        Set m_rs = Nothing
    End If
End Sub

I hope you can help me. I can give you more information if needed.

Parfait
  • 104,375
  • 17
  • 94
  • 125
flox92
  • 11
  • 4
  • What is the error that you're getting? – kabirbaidhya Sep 24 '20 at 12:43
  • It says in the title: Access Run-Time error-2465. Can't find the field '1' referred to in your expression. – flox92 Sep 24 '20 at 14:09
  • Such convoluted code. :( You should at least specify which exact line triggers the error, and the call stack that leads to it. – Andre Oct 01 '20 at 21:43
  • @Andre `If [Prozessspezifikationen].Delete(Me![Prozessspezifikation ID]) Then` is triggering. Nothing else is in the callstack (it doesnt even call the delete method) – flox92 Oct 02 '20 at 12:23
  • [Debugging VBA Code](http://www.cpearson.com/excel/DebuggingVBA.aspx). Try adding Debug Watches for the relevant objects. It is unclear, what object `[Prozessspezifikationen]` actually is. The Delete() method in your code has no parameters, but you call it with an ID. – Andre Oct 02 '20 at 15:31
  • @Andre `[Prozessspezifikationen]` is my table. I don't really know how I should debug that? – flox92 Oct 05 '20 at 09:19
  • Maybe it doesn't find table as i have backend and frontend file? – flox92 Oct 05 '20 at 09:51
  • `[Prozessspezifikationen].Delete()` means you have an object `[Prozessspezifikationen]` of which you call a method Delete. How is this object declared and initialized? – Andre Oct 05 '20 at 10:06
  • As mentioned before, redefining / replacing / wrapping all the normal Access functions/methods with your own functions makes it very hard for anyone else to understand your code. Maintainability will be a nightmare. – Andre Oct 05 '20 at 10:07

1 Answers1

0

Please specify default values for your optional parameters here:

Public Function OpenRecordset(Domain As String, _
                              Optional Criteria As String = "", _
                              Optional OrderBy As String = "", _
                              Optional RecordsetType As DAO.RecordsetTypeEnum = DAO.RecordsetTypeEnum.dbOpenDynaset, _
                              Optional RecordsetOptions As DAO.RecordsetOptionEnum = DAO.RecordsetOptionEnum.dbConsistent _
                              ) As Boolean
    
    On Error GoTo ErrorHandler
    
    If Not m_rs Is Nothing Then
        CloseRecordset
    End If
    
    Dim strSQL As String
    strSQL = "SELECT * FROM [" & Domain & "]"
    
    If Criteria <> "" Then
        strSQL = strSQL & " WHERE " & Criteria
    End If    

    If OrderBy <> "" Then
        strSQL = strSQL & " ORDER BY " & OrderBy
    End If
    
    Set m_rs = CurrentDb.OpenRecordset(strSQL, RecordsetType, RecordsetOptions)
    OpenRecordset = True

Done:
    Exit Function
ErrorHandler:
    Debug.Assert m_rs Is Nothing
    
    If eh.LogError("RecordsetWrapper.OpenRecordset", "strSQL = " & Chr(34) & strSQL & Chr(34)) Then Resume
End Function

Also since you're not passing all parameters when calling the function, explicitly pass the parameter name

Function Delete(ProzessspezifikationID As Long) As Boolean
    Dim rsw As New RecordsetWrapper
    If rsw.OpenRecordset(Domain:="Prozessspezifikationen", Criteria:="[Prozessspezifikation ID] = " & ProzessspezifikationID) Then
        Delete = rsw.Delete
    End If
End Function

Please see the docs at https://learn.microsoft.com/en-us/office/vba/language/concepts/getting-started/understanding-named-arguments-and-optional-arguments

jbud
  • 694
  • 5
  • 7
  • I have updated the code, removing the default value of the `Criteria`, and introduced an additional `If` block. Let me know if this works for you. – jbud Oct 01 '20 at 16:44
  • Still doesn't work, but it seems like the delete function is not even called (see comment on post) – flox92 Oct 02 '20 at 12:41
  • Try adding a break point at the line causing the error to ensure that the functions are indeed not being called. Also what's the resulting value you get for the strSQL(print it to the immediate window using `Debug.Print`? – jbud Oct 02 '20 at 12:47
  • Already did that, indeed they are not beeing called before error is thrown... Therefore I cannot tell you whats the resulting value of strSQL :/ – flox92 Oct 02 '20 at 12:57
  • Try decompiling your project, then recompile it – jbud Oct 02 '20 at 13:05
  • how do i decompile project in access? – flox92 Oct 02 '20 at 13:14
  • Please look at this related answer https://stackoverflow.com/questions/3266542/how-does-one-decompile-and-recompile-a-database-application. `"C:\Program Files\Microsoft Office\Office\MSACCESS.EXE" "C:\users\tim\documents\Mydatabase.mdb" /decompile` – jbud Oct 02 '20 at 13:22
  • If you ment decompile via access, I already did that (and tried compress and repair) with no success – flox92 Oct 02 '20 at 13:22
  • When trying to compile the project I get "variable not found" error for variable `dbOpenDynaset` in `OpenRecordset` function (`Optional RecordsetType As DAO.RecordsetTypeEnum = dbOpenDynaset, _`). When removing `Option explicit` (as it says on Microsoft help page) I get the error "User defined type not defined" and it marks the whole `OpenRecordset` function. Do you have any idea on that? – flox92 Oct 02 '20 at 13:54
  • I have updated the code, see the parameter declarations of the `OpenRecordset` function. Can you let me know if it works for you now? – jbud Oct 02 '20 at 14:54
  • If it doesn't know DAO, check your VBA references for the `Microsoft Office xx.0 Access Database Engine Object Library` – Andre Oct 02 '20 at 15:36
  • @jbud no it's still not working. Thanks you Andre, at least I am able to compile now – flox92 Oct 05 '20 at 09:22
  • Maybe it doesn't find table as i have backend and frontend file? – flox92 Oct 05 '20 at 09:51
  • Do you have linked tables? And are seeing data through your forms? – jbud Oct 05 '20 at 10:42
  • @jbud yes I am, already tried refrerring to underlying tables directly aswell – flox92 Oct 05 '20 at 13:41
  • You could try to add `Option Explicit` in the header of all your modules, so you have a strict checking of all your variable definitions. You can also try to import all your objects into a new DB, just to make sure you don't have any corrupted objects in your project causing the issue. – jbud Oct 08 '20 at 15:02