474,044 Members | 7,264 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 9868
Scott Price
1,384 Recognized Expert Top Contributor
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.  
No worries, mate :-)

Looks good so far, just go ahead and add the WHERE clause to your query. You can do this easier in design view rather than SQL view. Just add the line referring to your main form in the criteria grid of your design view window. i.e. Forms![YourFormName].[YourControlName]

Run the query to make sure it works, then we can return to the code to open the hidden form!

Regards,
Scott
Aug 15 '07 #21
Cintury
81 New Member
No worries, mate :-)

Looks good so far, just go ahead and add the WHERE clause to your query. You can do this easier in design view rather than SQL view. Just add the line referring to your main form in the criteria grid of your design view window. i.e. Forms![YourFormName].[YourControlName]

Run the query to make sure it works, then we can return to the code to open the hidden form!

Regards,
Scott
Code is changed and the query works when I input a value in the pop-up for Forms![frmAgencyDonati on].[cboFoodDonation sID]

Expand|Select|Wrap|Line Numbers
  1. SELECT tblFoodDonations.FoodDonationsID, tblFoodDonations.Date
  2. FROM tblFoodDonations
  3. WHERE (((tblFoodDonations.FoodDonationsID)=[Forms]![frmAgencyDonation]![cboFoodDonationsID]));
  4.  
However I'm just realizing that I never set the hidden form's FoodDonationsID = to anything so the query won't work automatically until I do. (GIANT D'OH Moment)

Expand|Select|Wrap|Line Numbers
  1. DoCmd.OpenForm "frmFoodDonationbyDate", , , , , acHidden
  2.  
So in the above statement I now need to set frmFoodDonation byDate.FoodDona tionsID = tempFieldID in order for the query to work. I just need to know how to set control criteria in that do cmd statement.

Thanks!
Aug 15 '07 #22
Scott Price
1,384 Recognized Expert Top Contributor
Now all you need to do is go into your form in design view. It should have only two text boxes, one named FoodDonationsID the other named DonationDate (or whatever). Make sure the text boxes are bound to the right fields from your query, and that should be all there is to it!

Because the query is only going to return the one record, there is no need to set any criteria in your DoCmd.OpenForm command. Once it opens, it's set with the data you need to compare against.


Regards,
Scott
Aug 15 '07 #23
Cintury
81 New Member
Now all you need to do is go into your form in design view. It should have only two text boxes, one named FoodDonationsID the other named DonationDate (or whatever). Make sure the text boxes are bound to the right fields from your query, and that should be all there is to it!

Because the query is only going to return the one record, there is no need to set any criteria in your DoCmd.OpenForm command. Once it opens, it's set with the data you need to compare against.


Regards,
Scott
Hmm ok as follows for the hidden form:
  • Food DonationsID Control Source: [qryFoodDononati onDates]![FoodDonationsID]
  • Date Control source: [qryFoodDononati onDates]![Date]

The problem is that at this line i'm still getting the object not set error:
Expand|Select|Wrap|Line Numbers
  1. If frmFoodDonationbyDate.txtDonDate > Me.txtDistributionDate Then
  2.  
I've verified that the control name is correct so right now I'm at a lost as to what is causing it.
Aug 15 '07 #24
Scott Price
1,384 Recognized Expert Top Contributor
Try this:

Expand|Select|Wrap|Line Numbers
  1. If Forms!frmFoodDonationbyDate.txtDonDate > Me.txtDistributionDate Then
Instead of this:

Expand|Select|Wrap|Line Numbers
  1. If frmFoodDonationbyDate.txtDonDate > Me.txtDistributionDate Then

Regards,
Scott
Aug 15 '07 #25
Cintury
81 New Member
Try this:

Expand|Select|Wrap|Line Numbers
  1. If Forms!frmFoodDonationbyDate.txtDonDate > Me.txtDistributionDate Then
Instead of this:

Expand|Select|Wrap|Line Numbers
  1. If frmFoodDonationbyDate.txtDonDate > Me.txtDistributionDate Then

Regards,
Scott
Arrrg... Runtime error 2447 invalid use of the .(dot) or ! operator or invalid parenthesis.
Aug 15 '07 #26
Scott Price
1,384 Recognized Expert Top Contributor
Arrrg... Runtime error 2447 invalid use of the .(dot) or ! operator or invalid parenthesis.
Using the code I gave you gives you this error? What version of Access are you using? I use Access 2003, and everything I've given you is tested and workable in VBA 6.0... If you are using 2003, check your references to see if there are any marked as missing: in the VBA editor window, click the Tools menu, the very first menu item is References. Click it and scroll through to see if any show up as missing. If so, uncheck/check their check boxes, close the menu, Compile, Save, Compact/Repair and close the db. Restart your db and try the code again.

If you are using 2007 I'll have to call in someone else, since I have no idea what M$ has changed between the two versions of VBA.

Regards,
Scott
Aug 16 '07 #27
Killer42
8,435 Recognized Expert Expert
Also, it might be helpful to know exactly what VBA pointed at in the code, when it reported the error.
Aug 16 '07 #28
Cintury
81 New Member
Also, it might be helpful to know exactly what VBA pointed at in the code, when it reported the error.
I'm running Access 2003 with the Office 2003 sp3 installed and VB6/VBA is the language. I checked the references and none turned up as missing. The error turned up on the very line that Scott gave me.
Aug 16 '07 #29
Scott Price
1,384 Recognized Expert Top Contributor
I'm running Access 2003 with the Office 2003 sp3 installed and VB6/VBA is the language. I checked the references and none turned up as missing. The error turned up on the very line that Scott gave me.
Hi Cintury,

In your references, make sure the DAO reference is version 3.6, also that the ActiveX reference is 2.1 (the ActiveX one shouldn't have anything to do with our problem, but check it anyway)

Also: Humor me on this one :-) Try going to this site and clicking on the Check for Office Upates button on the right hand side of the screen. http://office.microsoft.com/en-us/do...s/default.aspx

I'm coming up blank on any other reason for your error except that you've got some corrupted files/references in your Office installation, or that you have a corrupted database file. Check out this link for Allen Browne's recommendations on how to recover from a corrupted db file: http://allenbrowne.com/ser-47.html

The code I gave you, as I said, is tested and working on MS Access 2003 in one of my databases.

Regards,
Scott
Aug 16 '07 #30

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
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...
0
10310
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
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
7869
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...
0
6837
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
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.