473,839 Members | 1,358 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 #1
33 9832
Scott Price
1,384 Recognized Expert Top Contributor
Hi Cintury,

Is this an Access db problem? If so, it should probably be in the Access forum instead of the VB6 forum (especially since there are numerous differences between the VBA that Access uses and full-bore VB)

Assuming that this IS Access:

One work-around that is probably simpler to implement with your situation is to create a 'test form' with your second date field in it, that you open as Hidden in the AfterUpdate event of your Date control (combo/text whatever box). The code for opening a hidden form looks like this:
Expand|Select|Wrap|Line Numbers
  1. DoCmd.OpenForm "tfrmIng", , , , , acHidden
MAKE SURE to close this form with DoCmd.Close once you are done using it, as open hidden forms can make for headaches as you develop further (said from experience :-)

Then the solution is as simple as comparing the two date fields and popping up your message box when appropriate.

As an aside, you should probably rename your 'Date' field, as Date is a reserved word in MS Access/VBA/VB. You'll run into problems sooner or later using it as is...

To research recordsets more, do a search on this site using the .recordset keyword.

Regards,
Scott
Aug 11 '07 #2
Scott Price
1,384 Recognized Expert Top Contributor
Here is a link to a thread discussing how to highlight a specific record in a listbox using recordset/.findfirst

Not exactly what you're looking for, but may help you understand recordsets a bit more...

http://www.thescripts.com/forum/thread689835.html

Regards,
Scott
Aug 11 '07 #3
Killer42
8,435 Recognized Expert Expert
Ignore me, I'm just registering an interest in this thread so I'll be notified of updates.
Aug 12 '07 #4
Cintury
81 New Member
Hi Cintury,

Is this an Access db problem? If so, it should probably be in the Access forum instead of the VB6 forum (especially since there are numerous differences between the VBA that Access uses and full-bore VB)

Assuming that this IS Access:

One work-around that is probably simpler to implement with your situation is to create a 'test form' with your second date field in it, that you open as Hidden in the AfterUpdate event of your Date control (combo/text whatever box). The code for opening a hidden form looks like this:
Expand|Select|Wrap|Line Numbers
  1. DoCmd.OpenForm "tfrmIng", , , , , acHidden
MAKE SURE to close this form with DoCmd.Close once you are done using it, as open hidden forms can make for headaches as you develop further (said from experience :-)

Then the solution is as simple as comparing the two date fields and popping up your message box when appropriate.

As an aside, you should probably rename your 'Date' field, as Date is a reserved word in MS Access/VBA/VB. You'll run into problems sooner or later using it as is...

To research recordsets more, do a search on this site using the .recordset keyword.

Regards,
Scott
Hi Scott,

Thanks for the help. This IS access btw (i forgot to mention it i guess). There are a few questions I have about your solution.

The form that needs to be checked is a single form and I understand how I would check that against another single form, but the form that I need opened which would be hidden is a main form with a sub-form that needs to be searched. The main form to be searched is sorted by a store ID while the sub-form that needs to be searched is sorted by receipt ID. There is no store ID field in the form that I would be searching from. In order to get to the correct subform I'd have to go thru some complex linkages to get to get the matching record where as I thought that searching the table against the form via recordset would be much simpler.

Anyone with thoughts on this please comment.


Thank you!
Aug 13 '07 #5
Scott Price
1,384 Recognized Expert Top Contributor
I'm not sure if you are understanding the solution I was referring to, so I'll explain the idea a little more. If it doesn't match your situation, go ahead and disregard!

When you enter a date to be matched, you need to match it against something. The solution you were attempting at first was from one main form to another form's subform... Fine. What I was suggesting you do, is instead of comparing the two forms, create a 3rd form that has the date (and whatever else field you wish to match on) in it. This 3rd form is the one you open as hidden. Then the comparison is quite simple, and the resulting actions are also more simpler. I.e., Compare Form1.CheckDate against hidden Form2.Validatio nDate then DoAction to Form3.SubForm.R eceiptID

As for recordsets, I would say you need to use DAO recordsets since ADO means ActiveX Data Objects (the supposedly new and improved and M$ preferred advancement to DAO, which means Data Access Object, and refers to the original Jet container for all database objects in M$ Access, ActiveX references are notoriously difficult because of version incompatibility , while DAO still seems to work without any problems).

You should also form your query outside of VBA and store it as a saved query. Then refer to it in your recordset definition (the reasons are explained here in this link: Stored Query vs SQL Statement.

Go ahead and look through the articles in the Articles-Access section. You will find some very good information there regarding recordsets, DAO vs ADO, etc.

If you continue having problems, or there is something in the articles you don't understand, post back here and we'll do our best to help you!

Regards,
Scott

p.s. Killer, would you move this across to the Access forum, please?
Aug 13 '07 #6
Cintury
81 New Member
I'm not sure if you are understanding the solution I was referring to, so I'll explain the idea a little more. If it doesn't match your situation, go ahead and disregard!

When you enter a date to be matched, you need to match it against something. The solution you were attempting at first was from one main form to another form's subform... Fine. What I was suggesting you do, is instead of comparing the two forms, create a 3rd form that has the date (and whatever else field you wish to match on) in it. This 3rd form is the one you open as hidden. Then the comparison is quite simple, and the resulting actions are also more simpler. I.e., Compare Form1.CheckDate against hidden Form2.Validatio nDate then DoAction to Form3.SubForm.R eceiptID
Ok I've changed my approach as you've suggested to use the hidden form. The problem I'm having now is that I can't access the hidden form. This is the error I get:

[ERROR]
Compile Error: Variable not Defined
[/ERROR]

This is the code with an arrow (->)next to the stopping point.

Expand|Select|Wrap|Line Numbers
  1. Private Sub txtDistributionDate_AfterUpdate()
  2. Dim tempFieldID As String
  3.  
  4. Me.cboFoodDonationsID.SetFocus
  5. tempFieldID = Me.cboFoodDonationsID.Value
  6.  
  7. DoCmd.OpenForm "frmFoodDonationbyDate", , , , , acHidden
  8. -> frmFoodDonationbyDate.Filter = "[FoodDonationsID]" & tempFieldID
  9. frmFoodDonationbyDate.FilterOn = True
  10.  
  11. If frmFoodDonationbyDate.txtDonDate.Value > txtDistributionDate.Value Then
  12.  MsgBox "Please enter a Delivery date that is after that of the date of Donation!", vbOKOnly, "Out of Range Date Exception"
  13.  txtDistributionDate.SetFocus
  14.  End If
  15.  
  16. DoCmd.Close acForm, "frmFoodDonationbyDate"
  17. End Sub
  18.  
Thank you in advance for the advice
Aug 14 '07 #7
Scott Price
1,384 Recognized Expert Top Contributor
Instead of this:
DoCmd.OpenForm "frmFoodDonatio nbyDate", , , , , acHidden
-> frmFoodDonation byDate.Filter = "[FoodDonationsID]" & tempFieldID
frmFoodDonation byDate.FilterOn = True
Try this:

Expand|Select|Wrap|Line Numbers
  1. DoCmd.OpenForm "frmFoodDonationbyDate", , ,"[FoodDonationsID] =" & tempFieldID, , acHidden
Also in your lines 11 & 13 you'll need to change txtDistribution Date.Value to: Me!txtDistribut ionDate (no need to add the .Value, as that is the default setting for referring to a control)

Let me know how it goes!

Regards,
Scott
Aug 14 '07 #8
Cintury
81 New Member
Instead of this:


Try this:

Expand|Select|Wrap|Line Numbers
  1. DoCmd.OpenForm "frmFoodDonationbyDate", , ,"[FoodDonationsID] =" & tempFieldID, , acHidden
Also in your lines 11 & 13 you'll need to change txtDistribution Date.Value to: Me!txtDistribut ionDate (no need to add the .Value, as that is the default setting for referring to a control)

Let me know how it goes!

Regards,
Scott
Ok these changes have decreased the code size substantially and it looks like the solution is within sight, but I'm still receiving an error. I get a runtime error 2501 Open Form was cancelled. Now I know for a fact that receipt ID is correct because it is drawn from the tblFoodDonation s using a combobox so it has to find the Id in the form query. I just don't know why it's cancelling the form open.

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. -> DoCmd.OpenForm "frmFoodDonationbyDate", , , , "[FoodDonationsID]=" & tempFieldID, acHidden
  9.  
  10.  
  11. If frmFoodDonationbyDate.txtDonDate > 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.  End If
  14.  
  15. DoCmd.Close acForm, "frmFoodDonationbyDate"
  16. End Sub
  17.  
Aug 14 '07 #9
Scott Price
1,384 Recognized Expert Top Contributor
Ok these changes have decreased the code size substantially and it looks like the solution is within sight, but I'm still receiving an error. I get a runtime error 2501 Open Form was cancelled. Now I know for a fact that receipt ID is correct because it is drawn from the tblFoodDonation s using a combobox so it has to find the Id in the form query. I just don't know why it's cancelling the form open.

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. -> DoCmd.OpenForm "frmFoodDonationbyDate", , , , "[FoodDonationsID]=" & tempFieldID, acHidden
  9.  
  10.  
  11. If frmFoodDonationbyDate.txtDonDate > 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.  End If
  14.  
  15. DoCmd.Close acForm, "frmFoodDonationbyDate"
  16. End Sub
  17.  
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
Aug 14 '07 #10

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

Similar topics

4
6158
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
4383
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
2309
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
2080
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
4367
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
6559
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
4437
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
3519
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
5522
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
9855
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
9697
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
10587
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
10295
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
7018
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
5682
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
4487
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
4064
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3136
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.