474,044 Members | 3,015 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Querying a recordset with VBA Variables

Cintury
81 New Member
Ok the problem originally started with me wanting to compare the dates entered on one form with the dates entered on a different form's subform (I hope that wasn't too confusing). Thru numerous trials and error, I discovered that it was not possible to do it this way since 1) the other subform had to be open and 2) the main form of the searchee subform had to be on the correct record already (headache!).

Then I stumbled onto recordsets. I think I've found the solution. However I have a few concerns.
  • Do I use a DAO recordset or an ADO recordset (just trying to access a table and compare its values to some VB variables)
  • Is my syntax correct for the query
  • Why is it coming back with an empty recordset?

Expand|Select|Wrap|Line Numbers
  1. Private Sub txtDistributionDate_LostFocus()
  2. Dim rs As DAO.Recordset
  3. Dim DB As DAO.Database
  4. Dim tempDate As Date
  5. Dim tempFieldID, strSQL As String
  6.  
  7.  Set DB = CurrentDb()
  8.  
  9.   Me.cboFoodDonationsID.SetFocus
  10.   tempFieldID = Me.cboFoodDonationsID.Text
  11.   MsgBox tempFieldID, vbOKOnly
  12.  
  13.  
  14.   Set rs = DB.OpenRecordset("SELECT Date FROM tblFoodDonations WHERE [tblFoodDonations]![FoodDonationsID] ='" & tempFieldID & " ' ")
  15.     rs.MoveFirst
  16.  
  17.     Do While Not rs.EOF
  18.     rs.MoveNext
  19. 'TEST to see the current seached record
  20.     MsgBox rs("FoodDonationsID").Value + rs("Date").Value, vbOKOnly
  21.  
  22.     Loop
  23.     tempDate = rs("Date").Value
  24.  
  25.  
  26.  
  27.   If Me.txtDistributionDate > tempDate Then
  28.   MsgBox "Please enter a date that is after the product was Donated!", vbExclamation, "Date Check"
  29.  
  30.  
  31.  
  32.   Me.txtDistributionDate.SetFocus
  33.  
  34.   End If
  35.  
  36.   rs.Close
  37.  
  38. End Sub
  39.  
Thank you in advanced for your help!
Aug 10 '07
33 9867
Cintury
81 New Member
Three things I notice here:

Number one is that you have declared your tempFieldID as String. This should be Integer or Long because you are comparing it (presumably that is, unless you have made your ID fields alphanumeric) to a number field later on.

Number two is that you are missing a space between [FoodDonationsID] and the = sign.

Number three is you still need to correctly rename your txtDistribution Date field to Me.txtDistribut ionDate

Make these changes and try again. (btw I hope you are remembering to compile your changes, save your changes and recheck compile status during this process. If you don't, you likely won't notice the warning signs of corruption until it's almost too late!)

Regards,
Scott
Thanks for all your help so far Scott, I really really do appreciate it because this thing has been bugging me for a week and a half.

So I've fixed number 2 and 3 but the tempField is a string because although we would rather not some of our receipts are alphanumeric due to our printing company giving us rerun numbers of our paper receipts. This isn't caught until data entry time so we're forced to add a letter to the end of the FoodDonationsID on the receipt (both on paper and on screen for consistency).

Expand|Select|Wrap|Line Numbers
  1. Private Sub txtDistributionDate_AfterUpdate()
  2. Dim tempFieldID As String
  3. Dim frmFoodDonationbyDate As Form
  4.  
  5. Me.cboFoodDonationsID.SetFocus
  6. tempFieldID = Me.cboFoodDonationsID.Value
  7.  
  8. (A)-> DoCmd.OpenForm "frmFoodDonationbyDate", , , "[frmFoodDonationbyDate]![FoodDonationsID] =" & tempFieldID, , acHidden
  9. DoCmd.Requery
  10.  
  11. (B)->If [frmFoodDonationbyDate]![txtDonDate] > Me.txtDistributionDate Then
  12.  MsgBox "Please enter a Delivery date that is after that of the date of Donation!", vbOKOnly, "Out of Range Date Exception"
  13.  Me.txtDistributionDate.SetFocus
  14.  End If
  15.  
  16. DoCmd.Close acForm, "frmFoodDonationbyDate"
  17. End Sub
  18.  
I'm no longer receiving an error at arrow A but there is a pop-up box that requests this member when the comparison runs: [frmFoodDonation byDate]![FoodDonationsID]

I'm only testing one receipt number = 56846 and I've found it on the query and on the form. I'm wondering if it is not being found because the form is hidden?

The issue at arrow B is that when I mouse over it in the VBA code I'm not seeing a value for it like I should. I of course see the correct value for txtDistribution Date.
Aug 14 '07 #11
Scott Price
1,384 Recognized Expert Top Contributor
Let's take care of A first, and then see if B still is a problem.

In your DoCmd.OpenForm statement, you have already referred to the form you want to open, so later in your Where clause, you don't need to refer to it again... It's sufficient to simply point to the field you are comparing. I.e. instead of "[frmFoodDonation byDate]![FoodDonationsID] =", it's enough to just put in: "[FoodDonationsID] =" &...

If both fields are string values, then you shouldn't have any trouble comparing them the way you have it set up. The trouble starts if you try to compare a string field with a number field.

One quick question that occurs: is the ReceiptID field the primary key of your table? If so, have you taken steps to make sure that it is, in fact, unique (i.e. there can never be duplicates in this field?)

You will end up with potentially inaccurate data if your answer to question two is no :-) What can happen is that if you are telling the db to open this hidden test form looking for a particular ReceiptID, and there are multiple instances of it, you will unknowingly be testing your date against what is possibly the wrong ReceiptID/Date combination because the test date will be the date entered for the FIRST record that is returned. This is because the other records will be on the form, but in single form view you can only see one record at a time...

Regards,
Scott
Aug 14 '07 #12
Cintury
81 New Member
Oh man sorry for potentially confusing the issue Scott. The receipt ID is the FoodDonationsID I was just referring to it with a generic term. Anyway the FoodDonationsID is alphanumeric as is the tempFieldID.

The table from which the query for the hidden form draws data (tblFoodDonatio ns) has the FoodDonationID as the unique identifier so there are NO duplicates.

That was the issue before. When I referred to it as just [FoodDonationsID] I received a runtime error 2501. But when I put in [frmFoodDonation byDate]![FoodDonationsID] I did not receive this error, I just did not receive a value for the txtDonDate.
Aug 14 '07 #13
Scott Price
1,384 Recognized Expert Top Contributor
Oh man sorry for potentially confusing the issue Scott. The receipt ID is the FoodDonationsID I was just referring to it with a generic term. Anyway the FoodDonationsID is alphanumeric as is the tempFieldID.

The table from which the query for the hidden form draws data (tblFoodDonatio ns) has the FoodDonationID as the unique identifier so there are NO duplicates.

That was the issue before. When I referred to it as just [FoodDonationsID] I received a runtime error 2501. But when I put in [frmFoodDonation byDate]![FoodDonationsID] I did not receive this error, I just did not receive a value for the txtDonDate.

No problem! I wasn't too confused, anyway :-) So is it working now?

Just to clarify, in your DoCmd statement to open the form, you need to refer to the Form control name, not the Table field name: i.e. txtFoodDonation sID > FoodDonationsID .

Regards,
Scott
Aug 14 '07 #14
Scott Price
1,384 Recognized Expert Top Contributor
Just went back and looked, and I see you were talking about subforms earlier!

Perchance is the txtDistribution Date control that you are referring to part of the subform?

If so, you need to refer to it by it's full name: i.e. Forms![YourMainFormNam e]![YourSubFormName].[ControlNameOnSu bForm]

If, also your cboFoodDonation sID is part of the same subform, it also needs to be referred to by it's full name, as described above.
See the following changes I've made to your code.

Expand|Select|Wrap|Line Numbers
  1.       Private Sub txtDistributionDate_AfterUpdate()
  2.       Dim tempFieldID As String
  3.       Dim frmFoodDonationbyDate As Form
  4.  
  5.       Me.cboFoodDonationsID.SetFocus
  6.       tempFieldID = Me.cboFoodDonationsID.Value
  7.  
  8.       (A)-> DoCmd.OpenForm "frmFoodDonationbyDate", , , "[FoodDonationsID] =" & tempFieldID, , acHidden
  9.       DoCmd.Requery
  10.  
  11.       (B)->If [frmFoodDonationbyDate]![txtDonDate] > Forms![YourMainFormName]![YourSubformName].txtDistributionDate Then
  12.        MsgBox "Please enter a Delivery date that is after that of the date of Donation!", vbOKOnly, "Out of Range Date Exception"
  13.        Me.txtDistributionDate.SetFocus
  14.        End If
  15.  
  16.       DoCmd.Close acForm, "frmFoodDonationbyDate"
  17.       End Sub
Regards,
Scott
Aug 14 '07 #15
Cintury
81 New Member
No problem! I wasn't too confused, anyway :-) So is it working now?

Just to clarify, in your DoCmd statement to open the form, you need to refer to the Form control name, not the Table field name: i.e. txtFoodDonation sID > FoodDonationsID .

Regards,
Scott
No it's not working yet unfortunately. To clarify things further when I created the new third hidden form, I made it a (tabular?) form I believe, and the control name is the same name as the control source. Name: FoodDonationsID ; Control Source: FoodDonationsID .

I'm not sure if this frmAgencyDonati on is a subform or not because as it stands data is being passed thru the code as it is. When I mouse over it in debug VBA code I do see the correct data. However when I mouse over the code for the hidden form nothing displays.
Aug 15 '07 #16
Scott Price
1,384 Recognized Expert Top Contributor
No it's not working yet unfortunately. To clarify things further when I created the new third hidden form, I made it a (tabular?) form I believe, and the control name is the same name as the control source. Name: FoodDonationsID ; Control Source: FoodDonationsID .

I'm not sure if this frmAgencyDonati on is a subform or not because as it stands data is being passed thru the code as it is. When I mouse over it in debug VBA code I do see the correct data. However when I mouse over the code for the hidden form nothing displays.
Go into your hidden form in design view and change it to single form view.

Once in design view, double click on the little square on the upper left of the design view window, which brings up the Properties for your form. A few lines down on the All tab is the setting to change to Single form view.

If the data is being passed from your frmAgencyDonati on, then it isn't a subform :-)

Regards,
Scott
Aug 15 '07 #17
Cintury
81 New Member
Go into your hidden form in design view and change it to single form view.

Once in design view, double click on the little square on the upper left of the design view window, which brings up the Properties for your form. A few lines down on the All tab is the setting to change to Single form view.

If the data is being passed from your frmAgencyDonati on, then it isn't a subform :-)

Regards,
Scott
Ok I've finally managed to get a different error. Maybe this is where the ADO vs. DAO stuff comes into play (code changes will be underlined). The hidden form is in single form view.

[ERROR]
Run-Time error: 429
Active X object cannot be created
[/ERROR]

( -> ) will refer to error line.

Expand|Select|Wrap|Line Numbers
  1. Private Sub txtDistributionDate_AfterUpdate()
  2. Dim tempFieldID As String
  3. Dim frmFoodDonationbyDate As New Form
  4.  
  5. Me.cboFoodDonationsID.SetFocus
  6. tempFieldID = Me.cboFoodDonationsID.Value
  7.  
  8. -> DoCmd.OpenForm "frmFoodDonationbyDate", , , [frmFoodDonationbyDate]![FoodDonationsID] & "=" & tempFieldID, , acHidden
  9. DoCmd.Requery
  10.  
  11. If [frmFoodDonationbyDate]![txtDonDate] > Me.txtDistributionDate Then
  12.  MsgBox "Please enter a Delivery date that is after that of the date of Donation!", vbOKOnly, "Out of Range Date Exception"
  13.  Me.txtDistributionDate.SetFocus
  14.  End If
  15.  
  16. DoCmd.Close acForm, "frmFoodDonationbyDate"
  17. End Sub
  18.  
I removed the NEW from in front of Form at the top and now I'm just receiving the run-time error 91: Object variable or with not set. It occurs at the arrow still
Aug 15 '07 #18
Scott Price
1,384 Recognized Expert Top Contributor
If the form already exists there is absolutely no reason to dim it as a new form!

The reason you are getting the error you are getting now is that you've tried to recreate a form that already exists!

To backup a little, let's assume that you have based your hidden form on a query. The query looks a little like this:
Expand|Select|Wrap|Line Numbers
  1. SELECT tblFoodDonations.FoodDonationsID, tblFoodDonations.DonationDate FROM tblFoodDonations WHERE tblFoodDonations.DonationsID = Forms!frmFoodDonations.FoodDonationsID
Now you can do away with the where clause in your DoCmd.OpenForm line. It will then look something like this:
Expand|Select|Wrap|Line Numbers
  1. DoCmd.OpenForm "frmFoodDonationbyDate", , , , , acHidden
It looks like you've been making changes unnecessarily when one or another thing hasn't worked! It's good to try different things in most cases, but unfortunately you've only been managing to introduce other opportunities for error :-( I commend you for working at this, and I appreciate that you're willing to try different approaches. Don't give up yet!

There is no reason to change the DoCmd.OpenForm syntax from what I have posted... The syntax isn't the problem at this point.

Let me know what you have based your hidden form on, and let's go from there.

Regards,
Scott
Aug 15 '07 #19
Cintury
81 New Member
...
It looks like you've been making changes unnecessarily when one or another thing hasn't worked! It's good to try different things in most cases, but unfortunately you've only been managing to introduce other opportunities for error :-( I commend you for working at this, and I appreciate that you're willing to try different approaches. Don't give up yet!

There is no reason to change the DoCmd.OpenForm syntax from what I have posted... The syntax isn't the problem at this point.

Let me know what you have based your hidden form on, and let's go from there.

Regards,
Scott
Thanks for sticking it out with me up to this point Scott, I really and truly appreciate it. As you can tell I haven't worked to heavily in the forms and controls side of Access, I'm usually tinkering with the raw tables and queries.

Anyway the hidden form is based on a simple query to the foodDonations table.

Expand|Select|Wrap|Line Numbers
  1. SELECT tblFoodDonations.FoodDonationsID, tblFoodDonations.Date
  2. FROM tblFoodDonations;
  3.  
Aug 15 '07 #20

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

Similar topics

4
6173
by: Matt Young | last post by:
I've been tasked with integrating an older management system based on DBF files with my snappy new ASP application to provide users of the ASP application with real-time data from the management system. I figure with DBF files, I should use either the DBase drivers or the FoxPro drivers to connect to the database. The integration has gone quite smoothly until yesterday. This means that my connection strings work well based on the...
5
4395
by: Bruno Alexandre | last post by:
Hi guys, withou using SP, I want to be able to add a Parameter to the SQL Query and retrive the Recordset so I can use the Paging property under the recorset object.... how can I do this? I'm stuck here.
8
2319
by: dmiller23462 | last post by:
My brain is nuked....Can anybody tell me right off the bat what is wrong with this code? Along with any glaring errors, please let me know the syntax to display a message (Response.Write would be fine I think) that will say "I'm sorry but the data you requested cannot be found" or something along those lines.... This code is on an archive page I have on my company's intranet....The end result is to show 3 records at a time pulled from an...
1
2087
by: tabonni | last post by:
Hi All I connected MS Exchange Server to MS Access database using Access link table function. I can open the database table and see the fields and data inside the link table. However, the table seems cannot be queried by SQL statement. My situation is: I'm building an intranet. I have a ASP login page for all staff in the company to login. Other people can't register or login the intranet.
9
4384
by: Dom Boyce | last post by:
Hi First up, I am using MS Access 2002. I have a database which records analyst rating changes for a list of companies on a daily basis. Unfortunately, the database has been set up (by my predecessor, I hasten to add) so that each day it creates a copy of the record for each company, changes the date to today's date, and prompts the user for any changes of ratings on that day. The resulting data table grows by approx 600 records per...
6
6583
by: lenny | last post by:
Hi, I've been trying to use a Sub or Function in VBA to connect to a database, make a query and return the recordset that results from the query. The connection to the database and the query works fine, but passing the resulting recordset back to the sub's caller is not working out.
7
4446
by: Bill R via AccessMonster.com | last post by:
I get the error msg "Object variable or With block variable not set" when closing the "persistent" recordset I opened when the application opened. I have the rs variable declared in the declarations section of a module: Public rsAlwaysOpen As DAO.Recordset Then, when the switchboard form loads I set the variable:
2
3527
by: Jim M | last post by:
I rarely deal with recordsets directly with code, since I usually use Access queries, so be patient with this question. I want to open a recordset with various default variables used by my program. I tried: Public Sub OpenDefaults() Dim db As dao.Database Dim globalRst As dao.Recordset Set db = CurrentDb() Set globalRst = db.OpenRecordset("tblDefaults") End Sub
2
5540
by: wallconor | last post by:
Hi, I am having a problem using Dreamweaver CS3 standard recordset paging behavior. It doesn’t seem to work when I pass parameter values from a FORM on my search page, to the recordset on my results page. - Recordset Paging works if no parameters are used in the recordset sql code (ie. simple sql code): SELECT * FROM db_name WHERE (db_field1 LIKE ‘%text1%’ OR db_field2 LIKE ‘%text2%’)
0
10337
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
12140
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
11602
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
11141
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
8698
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7868
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6652
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
5416
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4944
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.