474,045 Members | 43,403 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
Cintury
81 New Member
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
WoW that took a looooong time to figure out. It turns out that VBA did not like Forms! and wanted Form!. But now I'm getting the error 2465: can't find the field frmFoodDonation byDate referred to in your expression.

Do you think this is because it is hidden?
Expand|Select|Wrap|Line Numbers
  1. DoCmd.OpenForm "frmFoodDonationbyDate", , , , , acHidden
  2. DoCmd.Requery
  3.  
  4. -> If Form!frmFoodDonationbyDate.txtDonDate > Me.txtDistributionDate Then
  5.  
BTW the DAO and ActiveX are up to date, the only update not installed was VS 2005 sp1 and I just installed and rebooted.
Aug 16 '07 #31
Cintury
81 New Member
On a side-note I've just noticed that when I'm in VBA in the Project window to my left, my form does not show up. Does this mean something?
Aug 16 '07 #32
Scott Price
1,384 Recognized Expert Top Contributor
WoW that took a looooong time to figure out. It turns out that VBA did not like Forms! and wanted Form!. But now I'm getting the error 2465: can't find the field frmFoodDonation byDate referred to in your expression.

Do you think this is because it is hidden?
Expand|Select|Wrap|Line Numbers
  1. DoCmd.OpenForm "frmFoodDonationbyDate", , , , , acHidden
  2. DoCmd.Requery
  3.  
  4. -> If Form!frmFoodDonationbyDate.txtDonDate > Me.txtDistributionDate Then
  5.  
BTW the DAO and ActiveX are up to date, the only update not installed was VS 2005 sp1 and I just installed and rebooted.
This is a direct quote from the VBA help file regarding how to refer to forms:
A Form object refers to a particular Microsoft Access form.

Using the Form Object
A Form object is a member of the Forms collection, which is a collection of all currently open forms. Within the Forms collection, individual forms are indexed beginning with zero. You can refer to an individual Form object in the Forms collection either by referring to the form by name, or by referring to its index within the collection. If you want to refer to a specific form in the Forms collection, it's better to refer to the form by name because a form's collection index may change. If the form name includes a space, the name must be surrounded by brackets ([ ]).

Syntax Example
Forms!formname Forms!OrderForm
Forms![form name] Forms![Order Form]
Forms("formname ") Forms("OrderFor m")
Forms(index) Forms(0)


Each Form object has a Controls collection, which contains all controls on the form. You can refer to a control on a form either by implicitly or explicitly referring to the Controls collection. Your code will be faster if you refer to the Controls collection implicitly. The following examples show two of the ways you might refer to a control named NewData on the form called OrderForm:

' Implicit reference.
Forms!OrderForm !NewData

' Explicit reference.
Forms!OrderForm .Controls!NewDa ta

The next two examples show how you might refer to a control named NewData on a subform ctlSubForm contained in the form called OrderForm:

Forms!OrderForm .ctlSubForm.For m!Controls.NewD ata

Forms!OrderForm .ctlSubForm!New Data
As you can see, it includes examples that quite clearly indicate that the correct syntax in VBA 6.0 is Forms!, not Form!

If Form! is working for you (which as you say, it's NOT), then happy trails :-)

However, since the error message you mention is indicating that because you are referring to it by Form!... it thinks the frmDonationByDa te is a FIELD in a form name Form, not a form... And, correctly, it can't find any such field on any such form.

Check very closely to make sure that you are referring to the names of the forms and particularly the names of the text boxes/controls correctly! a small misspelling will result in the db not being able to find the form you want.

If you still can't figure it out, please email a copy of the db, I'll take a look and see what I can do for you (send me a PM if you want to go this route, and I'll enable my email address so you can send the db to me).

And no, it's not because the form is hidden that it can't be found! Hidden forms are only invisible to the users of the database, not to the database itself.

Regards,
Scott
Aug 16 '07 #33
Scott Price
1,384 Recognized Expert Top Contributor
On a side-note I've just noticed that when I'm in VBA in the Project window to my left, my form does not show up. Does this mean something?
Not really. The hidden form will only show in your VBA editor window if you have opened a module for it (by clicking on the ellipsis next to an event and choosing Code Builder.

This doesn't mean that it's not accessible to the database, just that you haven't entered any VBA code for the particular form

Regards,
Scott
Aug 16 '07 #34

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
10546
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
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...
1
12023
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
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
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...
2
4944
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3971
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.