I am working in ms-access (office 365, v1808), and today my code suddenly broke down. I have not been able to pinpoint the problem specifically nor have I been able to find useful information on the internet.
I'm retrieving a recordset with an ADODB.Recordset object from an external SQL-server table. This is the code that causes Access to crash without any useful information:
Private Sub Command_Click()
Dim rs As ADODB.Recordset
Dim strSQL As String
Dim i As Long
strSQL = "SELECT TOP 100 *" & _
" FROM myTable;"
Call OpenMyRecordset(rs, strSQL)
Set Me.myListbox.Recordset = rs
Me.myListbox.Value = Me.myListbox.ItemData(0)
rs.Close
Set rs = Nothing
End Sub
Usually the line 'Me.Listbox...' causes the crash (after experimenting with a break and stepping through the code)
I have tried this with no success:
i = Me.myListbox.ItemData(0)
Me.myListbox.Value = i
Sometimes the code also breaks down if I only use:
Debug.print Me.myListbox.Value
The weirdest part is that the errors are not consistent: sometimes it runs a once or twice and then crashes, while other times it crashes immediately.
Additional information gathered so far:
- The rs that is returned is O.K. (and non-empty)
- If I only use 'Set me.myListbox.Recordset = rs' and do not inquire/alter the .Value property all is good
- Using the .Value property without loading a recordset is also no problem
- Recreating the SQL table does not help
- Using a different table/database does not help
- Restarting Access/My computer/Creating a new Access file with only relevant code does not help
EDIT:
The field types for the recordset are int, int, nvarchar(255), nvarchar(255).
The code for OpenMyRecordset is copied and adjusted accordingly from http://accessexperts.net/blog/2011/01/21/easy-adodb-recordsets-and-commands-in-access/