Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Requerying a recordset 1

Status
Not open for further replies.

ColinM

Programmer
Jun 29, 2000
189
0
0
TH
Hi,<br>I am trying to requery a recordset so that a form changes record based on a combo box.<br>I thought I should be able to use a requery rather than run the whole set recordset code again.<br><br>I am using this code:<br><br>Sub SetRS(FormName As String, strSQL As String)<br>Set rst = New ADODB.Recordset<br>rst.Open strSQL, CurrentProject.Connection, , , adCmdText<br>Set Forms(FormName).Recordset = rst<br>End Sub<br><br>Private Sub Form_Open(Cancel As Integer)<br>Call RS.SetRS(Me.Name, &quot;Select * from Customers<br>where name = '&quot; & NameCombo.Value & &quot;'&quot;)<br>End Sub<br><br>I thought I would be able to then use this:<br><br>Private Sub NameCombo_Change()<br>Form.Recordset.Requery<br>End Sub<br><br>Anybody see where I'm going wrong?<br>
 
You cannot bind a form to an ADO recordset. A form can only be bound to a local table, linked table, query, or recordset to one of the preceeding. <br><br>You can bind a form to a Pass Through Query however. OR<br>Leave the form unbound and fill in the values on Open of the form, for example:<br><br><font color=red>Private Sub Form_Open()<br>&nbsp;&nbsp;&nbsp;Dim rst as ADODB.Recordset<br>&nbsp;&nbsp;&nbsp;Dim strSQL as String<br><br>&nbsp;&nbsp;&nbsp;strSQL = &quot;SELECT * FROM Customers WHERE name = '&quot; & Me.NameCombo & &quot;'&quot;<br>&nbsp;&nbsp;&nbsp;Set rst = New ADODB.Recordset<br>&nbsp;&nbsp;&nbsp;rst.Open strSQL, CurrentProject.Connection, <b>adOpenForwardOnly</b>, , adCmdText<br>&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;If rst.Recordcount &gt; 0 Then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;With rst<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Me.Field1 = !Field1<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Me.Field2 = !Field2<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Me.Field3 = !Field3<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;etc.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;End With<br>&nbsp;&nbsp;&nbsp;End If<br><br>&nbsp;&nbsp;&nbsp;rst.close<br>&nbsp;&nbsp;&nbsp;Set rst = Nothing<br>End Sub</font><br><br>Now put a submit button on your form. When they submit changes, do the same thing, exept you would&nbsp;&nbsp;write to the table instead of read from it. <br><br><br><font color=blue>Private Sub cmdSubmit_Click()<br>&nbsp;&nbsp;&nbsp;Dim rst as ADODB.Recordset<br>&nbsp;&nbsp;&nbsp;Dim strSQL as String<br><br>&nbsp;&nbsp;&nbsp;strSQL = &quot;SELECT * FROM Customers WHERE name = '&quot; & Me.NameCombo & &quot;'&quot;<br>&nbsp;&nbsp;&nbsp;Set rst = New ADODB.Recordset<br>&nbsp;&nbsp;&nbsp;rst.Open strSQL, CurrentProject.Connection, <b>adOpenDynamic</b>, , adCmdText<br>&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;If rst.Recordcount &gt; 0 Then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;With rst<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Edit<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;!Field1 = Me.Field1<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;!Field2 = Me.Field2<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;!Field3 = Me.Field3<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;etc.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Update<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;End With<br>&nbsp;&nbsp;&nbsp;End If<br><br>&nbsp;&nbsp;&nbsp;rst.close<br>&nbsp;&nbsp;&nbsp;Set rst = Nothing<br>End Sub</font><br><br>Class modules are a good way to handle this, except it would take way to long to explain how to use them. I would check into them. There is a good example in the <b>Access 97 Developers Handbook</b> by Litwin, Getz, & Gilbert, Chapter 3. <p>Jim Lunde<br><a href=mailto:compugeeks@hotmail.com>compugeeks@hotmail.com</a><br><a href= Application Development
 
This line of code works:<br>Set Forms(FormName).Recordset = rst<br><br>binding the form to the recordset.<br>Perhaps you can't do this in an mdb file? I don't know.<br><br>It was just the other bit where the combo box is changed that didn't work.<br>I can change the recordset by sending another query. I thought that I could requery it and it would be more efficient, rather than closing the old recordset and creating a new one.<br><br>
 
I have never seen a <font color=red>Recordset</font> property for a form. I know there is a <font color=blue>Record Source</font> property, but <font color=red>Recordset</font> is new to me.<br><br>What do you mean by &quot;Perhaps you can't do this in an mdb file?&quot;, aren't you using an .mdb file?<br>Is this a read-only form? Let me explain:<br><br>In an Access database, a bound form means that the fields on the form are basically connected to the fields in the table. When you update them, the changes are automatically written to the table. I simply can't believe that you could do this with an ADO recordset. When you key information into the field on the form, the ADO recordset is not a direct link to the table, it is a set of information that you have requested from SQL Server. When you try to write information back to the table, it would require the .Edit, and .Update methods of the Recordset object. If it is a view-only form, it may work, but binding any Client-Server applications is not the best practice. <p>Jim Lunde<br><a href=mailto:compugeeks@hotmail.com>compugeeks@hotmail.com</a><br><a href= Application Development
 
Apologies!<br>You are correct, it wasn't bound at all.<br><br>I was using the wrong terminology.<br>What I should of said was that the form displays the recordset that you assign, but is NOT bound, as it can't be updated ( I had been using the form to only look at data ).<br>I am now using RecordSource to achieve what I want, thanks!<br><br>As far as the .mdb file bit goes, I have been using .adp (Access 2000 project file) which is just a front end to SQL server.<br><br>I don't know if you've had any experience of Access 2000 project files.<br><br>In your last sentence you say that binding forms is not a good idea in Client/Server. I assume this is because of concurrency issues. Is there any other reasons for not using bound forms. (They seem slightly faster to me)<br><br>I recently read that bound forms in A2K projects would only lock the data for the duration of the fetch, anybody know any more about this?<br><br>
 
Unfortunately, I have not had any experience with .adp's. The job I am at now uses Oracle, so I will not be using them in the near future either.<br><br>The main reason for not binding forms in Client-Server is performance. The whole idea of having a seperate server is to let the server do the processing for you, and not the client machine. Bound forms do not allow this. I can't speak for .adp files, but I know in .mdb files, the only way to bind a form to a server is to attach the tables, then bind to the attached table. BAD Idea. It defeats the whole purpose of the Server processing.<br><br>We had a 100 user Access - Access system using bound forms, and having record counts of several hundred thousand records. The performance soon became too bad to continue using that method. So while that was going on, we converted the back end to SQL Server, and unbound everything. All data retrieval, was done with Pass through queries, and ADO. All writing was done with ADO. The performance difference when we were done was astonishing! Processes that took 15 - 25 seconds before, took less than 2 seconds.<br><br>Another reason for not binding forms is that the updates that users do to the tables are instantaneous. Data is harder to control than having them make changes, then submit them, and have them verify that they want to save them. It is also easier to create audit trail tables this way.<br><br>I am not saying that a bound application cannot be effective. Currently we have over 20 applications that use this technology on a national network. However, the performance is not what it could be if it was an unbound Client - Server application. I do accept the fact that an unbound application takes longer to develop, but I believe it is worth it in the long run. <p>Jim Lunde<br><a href=mailto:compugeeks@hotmail.com>compugeeks@hotmail.com</a><br><a href= Application Development
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top