Mel Brooks
asked on
Cannot navigate a form connected using ADODB
A have an Access form and have connected it to an sql server table using ADODB by setting the form's recordset property to the ADO recordset. The first record comes up fine, but when I navigate to the next record, my ADO recordset property goes to a state of 0. While the form displays the next record in the set, the form's OnCurrent event won't fire.
What is closing my recordset? I'm not doing any requering or anything?
What is closing my recordset? I'm not doing any requering or anything?
Can you show the code you're using to set the Form's Recordset, and the code you're using to create the Recordset? You have to create that recordset correctly, or you can run into some issues.
ASKER
The connection variable and recordset variables are saved in a public module
Public cnContacts as New Adodb.connection
Public rsContacts as New Adodb.recordset
In the Main form Open Event:
cnContacts.ConnectionStrin g = strdbConnection
cn.Contacts.CursorLocation = adUseClient
cnContacts.OPen
On a combobox after update event:
str = "select * from Contacts where ClientID = " & ComboboxName
Set frm = Contacts_SubForm.Form
If rsContacts.STATE = 1 Then
rsContacts.Close
Set rsContacts = Nothing
End If
rsContacts.Open str, cnContacts, adOpenDynamic, adLockPessimistic
Set frm.Recordset = rsContacts
The subform populates with the records, but when navigating to the next record , rsContacts state changes to 0 even though the form displays the data of the next record
Public cnContacts as New Adodb.connection
Public rsContacts as New Adodb.recordset
In the Main form Open Event:
cnContacts.ConnectionStrin
cn.Contacts.CursorLocation
cnContacts.OPen
On a combobox after update event:
str = "select * from Contacts where ClientID = " & ComboboxName
Set frm = Contacts_SubForm.Form
If rsContacts.STATE = 1 Then
rsContacts.Close
Set rsContacts = Nothing
End If
rsContacts.Open str, cnContacts, adOpenDynamic, adLockPessimistic
Set frm.Recordset = rsContacts
The subform populates with the records, but when navigating to the next record , rsContacts state changes to 0 even though the form displays the data of the next record
Looks like a variable scope issue.
Where are they declared ?
Please, post the full code of your form's open and combobox's after update events, there are not enough informations in those snipets.
Where are they declared ?
Please, post the full code of your form's open and combobox's after update events, there are not enough informations in those snipets.
Have you seen this article about using an ADO recordset with an Access form:
https://docs.microsoft.com/en-us/office/vba/access/concepts/activex-data-objects/bind-a-form-to-an-ado-recordset
That article mentions two caveats:
-- The underlying ADO recordset must be updatable via ADO.
-- The recordset must contain one or more fields that are uniquely indexed, such as a table's primary key.
Does your recordset adhere to those caveats?
https://docs.microsoft.com/en-us/office/vba/access/concepts/activex-data-objects/bind-a-form-to-an-ado-recordset
That article mentions two caveats:
-- The underlying ADO recordset must be updatable via ADO.
-- The recordset must contain one or more fields that are uniquely indexed, such as a table's primary key.
Does your recordset adhere to those caveats?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
re: "This wasn't the solution I wanted as its tedious and time consuming to write the code for this"
Which is why, on your other post, several of us recommended using linked tables and bound forms.
Dale
Which is why, on your other post, several of us recommended using linked tables and bound forms.
Dale
ASKER
I thank everyone for their comments. I just want to mention that my form was originally setup using linked tables, but we recently found that a memo field in the table would not take more than 8000 characters even the though the sql column is set as varchar(max). We're not sure if this has always been the case or a recent driver update started causing the problem.
What SQL Server driver are you using?
ASKER
Not totally sure because we have well over 100 machines, mostly Windows 10. I believe the majority are using SQLNCL11, but some may be using SQLNCL10. I know that on my development machine I'm using SQLNCL11, and I got the ODBC error when trying to paste more than 8000 characters into memo field with the ODBC link.
Try adding a filter or a sort, and you will likely see what I mean.