473,394 Members | 1,701 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Requerying Recordset Based on Combobox

124 100+
I am trying to requery a recordset so that a form changes record based on a combobox. In the form's Open event I'm using this code:

Expand|Select|Wrap|Line Numbers
  1.     Dim cn As New ADODB.Connection
  2.     Dim rs As ADODB.Recordset
  3.     Dim strSQL As String
  4.  
  5.     cn.Open CurrentProject.Connection
  6.  
  7.     Set rs = New ADODB.Recordset
  8.  
  9.     strSQL = "SELECT * FROM StdDegReq WHERE PersonId = " & Nz(Me.txtPersonId, 0)  
  10.  
  11.     rs.Open strSQL, cn, adOpenKeyset, adLockOptimistic
  12.  
  13.     Set Me.Recordset = rs
  14.  
  15. Exit_Procedure:
  16.     cn.Close
  17.     Set rs = Nothing
  18.     Exit Sub
  19.  
What I'm trying to figure out is how to requery the recordset in the AfterUpdate event of the combobox without having to repaste all my code from the OnOpen event (if possible). I tried this but it doesn't work:

Expand|Select|Wrap|Line Numbers
  1.     Dim rs As ADODB.Recordset
  2.  
  3.     Set rs = Me.Recordset
  4.     rs.Requery
  5.  
I get the error message: "Operation is not allowed when the object is closed". What object is closed? Any suggestions?
Jul 7 '10 #1

✓ answered by nico5038

Oops, typo, this needs to be:
Expand|Select|Wrap|Line Numbers
  1. Me.RecordSource = Me.RecordSource
  2.  
Sorry.

Nic;o)

10 3991
nico5038
3,080 Expert 2GB
Did you try:
Expand|Select|Wrap|Line Numbers
  1. Me.Recordset = Me.Recordset 
  2.  
Nic;o)
Jul 7 '10 #2
bullfrog83
124 100+
@nico5038
How do you mean? Just have that code in the module?

Also, I added the the error message that I get if I just execute the code above in my OP: "Operation is not allowed when the object is closed".
Jul 8 '10 #3
nico5038
3,080 Expert 2GB
This command will requery your form after activation.

The error messages does look like you're having some code to close the form. Check your code against the event sequence to solve this.

When stuck attach (part of) the .mdb so I can have a look.

Nic;o)
Jul 8 '10 #4
bullfrog83
124 100+
@nico5038
If I only have Me.Recordset = Me.Recordset in the AfterUpdate event I get "Error 450: Wrong number of arguments or invalid property assignment." Perhaps I've misunderstood how you want me to include this in my code?
Jul 8 '10 #5
nico5038
3,080 Expert 2GB
Oops, typo, this needs to be:
Expand|Select|Wrap|Line Numbers
  1. Me.RecordSource = Me.RecordSource
  2.  
Sorry.

Nic;o)
Jul 8 '10 #6
bullfrog83
124 100+
@nico5038
I think that does the trick as I get no error but the problem is if Me.txtPeopleId is null it errors out. So, I put Nz(Me.txtPeopleId,0) so I don't get that error. However, from checking the code in the Immediate window it appears that the Nz function is returning 0 for any id I select. So, if I select id 1489 it's executing the code as PersonId = 0 instead of 1489. If not Nz, then what can I use to handle the variable if it's null?
Jul 8 '10 #7
nico5038
3,080 Expert 2GB
First I would "move" your code from the Open event to the OnCurrent event, thus making sure it's executed every time there's a change of record.

Next I would use this code to see or the combo is filled and issue a warning when no selection has been made:
Expand|Select|Wrap|Line Numbers
  1. IF Len(NZ(Me.txtPeopleId)) > 0 then
  2.    Me.RecordSource = Me.RecordSource
  3. else
  4.    Msgbox "Please select a Person"
  5. endif
  6.  
Nic;o)
Jul 8 '10 #8
bullfrog83
124 100+
@nico5038
If I move my code to the OnCurrent event I get an unexpected error has occurred and Access quits. This happens when the code gets to the line: Me.Recordset = rs (see OP).
Jul 8 '10 #9
nico5038
3,080 Expert 2GB
When it's a bound combo and filled when the form opens, then I guess a loop will occur.
So we'll need to use the OnOpen of the form and the AfterUpdate event of the combo.

Nic;o)
Jul 8 '10 #10
bullfrog83
124 100+
@nico5038
Now it's working. Thanks!
Jul 8 '10 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Annette Massie | last post by:
I have a bunch of images stored in a folder on the harddrive. Is there a way for Access 2000 to read the directory and write a record for the images found? Eventually the records need to be...
1
by: VivN | last post by:
I am using the following code to open a recordset returned by a SQL server stored procedure: sConn = "Provider='SQLOLEDB';Data Source='Aph-Ework-Dev';" & _ "Initial...
2
by: info | last post by:
I can successfully open a recordset based upon an Excel sheet in Access, but I can't work out how to copy all the records to an Access table. Any pointers?
8
by: Krul | last post by:
I like to have a blank entry at top of the combobox list, so the user is able to clear the combobox. Right now, the form displays a empty entry on load, but after chosen a value once, it is not...
2
by: ajspacemanspiff | last post by:
I currently have a solution that requires 2 sub queries, where each of them is convereted into a crosstab query and then I join the crosstab queries to a result. I would like to make this more...
3
by: Julie Barnet | last post by:
Does anyone have or know of a download location for a custom combobox web control for a combobox that allows: databinding typing as well as selecting a listitem limit to list functionality...
0
by: CanFlightSim | last post by:
I use combo boxes and a great little piece of code to fill a form with a record set. For example I want to search by Lastname and fill the form or by company and fill the form, I will start typing...
1
by: David Patz | last post by:
Hi, Im currently building a database for users to edit or manipulate train maintanence parts. For one of my forms - I display the inactive parts in stock. I've designed the form to have a...
3
tuxalot
by: tuxalot | last post by:
I am trying to move to the first record in a form where a value is false. The underlying form uses a query as it's record source. I am getting an "invalid argument" error where indicated below in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.