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?
-
Private Sub txtDistributionDate_LostFocus()
-
Dim rs As DAO.Recordset
-
Dim DB As DAO.Database
-
Dim tempDate As Date
-
Dim tempFieldID, strSQL As String
-
-
Set DB = CurrentDb()
-
-
Me.cboFoodDonationsID.SetFocus
-
tempFieldID = Me.cboFoodDonationsID.Text
-
MsgBox tempFieldID, vbOKOnly
-
-
-
Set rs = DB.OpenRecordset("SELECT Date FROM tblFoodDonations WHERE [tblFoodDonations]![FoodDonationsID] ='" & tempFieldID & " ' ")
-
rs.MoveFirst
-
-
Do While Not rs.EOF
-
rs.MoveNext
-
'TEST to see the current seached record
-
MsgBox rs("FoodDonationsID").Value + rs("Date").Value, vbOKOnly
-
-
Loop
-
tempDate = rs("Date").Value
-
-
-
-
If Me.txtDistributionDate > tempDate Then
-
MsgBox "Please enter a date that is after the product was Donated!", vbExclamation, "Date Check"
-
-
-
-
Me.txtDistributionDate.SetFocus
-
-
End If
-
-
rs.Close
-
-
End Sub
-
Thank you in advanced for your help!
33 9832
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: - 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
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
Ignore me, I'm just registering an interest in this thread so I'll be notified of updates.
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: - 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!
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?
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. -
Private Sub txtDistributionDate_AfterUpdate()
-
Dim tempFieldID As String
-
-
Me.cboFoodDonationsID.SetFocus
-
tempFieldID = Me.cboFoodDonationsID.Value
-
-
DoCmd.OpenForm "frmFoodDonationbyDate", , , , , acHidden
-
-> frmFoodDonationbyDate.Filter = "[FoodDonationsID]" & tempFieldID
-
frmFoodDonationbyDate.FilterOn = True
-
-
If frmFoodDonationbyDate.txtDonDate.Value > txtDistributionDate.Value Then
-
MsgBox "Please enter a Delivery date that is after that of the date of Donation!", vbOKOnly, "Out of Range Date Exception"
-
txtDistributionDate.SetFocus
-
End If
-
-
DoCmd.Close acForm, "frmFoodDonationbyDate"
-
End Sub
-
Thank you in advance for the advice
Instead of this:
DoCmd.OpenForm "frmFoodDonatio nbyDate", , , , , acHidden
-> frmFoodDonation byDate.Filter = "[FoodDonationsID]" & tempFieldID
frmFoodDonation byDate.FilterOn = True
Try this: - 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
Instead of this:
Try this: - 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. -
Private Sub txtDistributionDate_AfterUpdate()
-
Dim tempFieldID As String
-
Dim frmFoodDonationbyDate As Form
-
-
Me.cboFoodDonationsID.SetFocus
-
tempFieldID = Me.cboFoodDonationsID.Value
-
-
-> DoCmd.OpenForm "frmFoodDonationbyDate", , , , "[FoodDonationsID]=" & tempFieldID, acHidden
-
-
-
If frmFoodDonationbyDate.txtDonDate > txtDistributionDate Then
-
MsgBox "Please enter a Delivery date that is after that of the date of Donation!", vbOKOnly, "Out of Range Date Exception"
-
End If
-
-
DoCmd.Close acForm, "frmFoodDonationbyDate"
-
End Sub
-
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. -
Private Sub txtDistributionDate_AfterUpdate()
-
Dim tempFieldID As String
-
Dim frmFoodDonationbyDate As Form
-
-
Me.cboFoodDonationsID.SetFocus
-
tempFieldID = Me.cboFoodDonationsID.Value
-
-
-> DoCmd.OpenForm "frmFoodDonationbyDate", , , , "[FoodDonationsID]=" & tempFieldID, acHidden
-
-
-
If frmFoodDonationbyDate.txtDonDate > txtDistributionDate Then
-
MsgBox "Please enter a Delivery date that is after that of the date of Donation!", vbOKOnly, "Out of Range Date Exception"
-
End If
-
-
DoCmd.Close acForm, "frmFoodDonationbyDate"
-
End Sub
-
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
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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.
|
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...
|
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.
|
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...
| |
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.
|
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:
|
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
|
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%’)
|
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...
|
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,...
| |
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...
|
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...
|
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();...
|
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...
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |