I am having issues with a form and its interaction with a combobox dropdown. The user can select an item from a list and vba is ran to check if the form is dirty and asks "Do you want to save" etc.
If the answer is no, frm.undo is ran, but my issue is with the next line where the combobox is looking for the item in the list but undo has wiped that clean.
Here is the code simplified [frm as form is passed to function] [cboGoToContact is my combo]
Dim rs As Object
Set rs = frm.Recordset.Clone
If frm.dirty Then
'Prompt to confirm the save operation.
answer = MsgBox("Do you want to save?", vbYesNoCancel vbQuestion, _
"Save Record")
If answer = vbNo Then
frm.Undo ' <<<<<< ERROR Caused by this
rs.FindFirst "[ID] = " & str(Nz(frm![cboGoToContact], 0)) '<<< Error Line
If Not rs.EOF Then frm.Bookmark = rs.Bookmark
'Disable all textbox by default
For Each Ctrl In frm.Controls
If TypeOf Ctrl Is TextBox Then
Ctrl.Enabled = False
End If
Next Ctrl
frm.Clients.Enabled = False
ElseIf answer = vbYes Then
'do stuff
ElseIf answer = vbCancel Then
'do stuff
End If
Else
'more stuff
End If
I have tried assigning this to a value and passing that along, but the error I keep getting is: Object invalid or no longer set
I tried things like this:
var = frm.cboGoToContact ' <<< returns an number of where the selection is in the list
frm.Undo
rs.FindFirst "[ID] = " & str(Nz(var, 0))
If Not rs.EOF Then frm.Bookmark = rs.Bookmark
CodePudding user response:
Try this using the RecordsetClone:
Dim rs As DAO.Recordset
If frm.dirty Then
'Prompt to confirm the save operation.
answer = MsgBox("Do you want to save?", vbYesNoCancel vbQuestion, _
"Save Record")
If answer = vbNo or answer = vbCancel Then
frm.Undo
Set rs = frm.RecordsetClone
rs.FindFirst "[ID] = " & Str(Nz(frm![cboGoToContact], 0))
If Not rs.NoMatch Then
frm.Bookmark = rs.Bookmark
End If
rs.Close
'Disable all textboxes by default
For Each Ctrl In frm.Controls
If TypeOf Ctrl Is TextBox Then
Ctrl.Enabled = False
End If
Next Ctrl
frm.Clients.Enabled = False
Else
'do stuff
End If
Else
'more stuff
End If