473,670 Members | 2,551 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to retrieve value for the next record in a query if the current record value is 0

66 New Member
In a query i want to retrieve the value in such a way that if the value for the current record for a given column is 0 then it should go
to the next record & take the value.

for ex-I 've column Payment received in the query.I want to retrieve the value in such a way that if the current record value for this column is 0 then it should go to the next record value..
Dec 11 '09 #1
7 10277
Delerna
1,134 Recognized Expert Top Contributor
There is no inbuilt way in SQL to select values from the next record based on the value of a field in the current record.
It takes thought and tricky query design to get a query to do that sort of thing.
You don't have enough detail for me to give you a suggestion.
One method is to write 2 queries and provide sequence numbers in each.
The trick is to arrange the second query so that the next record you mention has a sequence number that is 1 greater than the sequence number in the first query.

You then use a third query based off those 2 to select your records accordingly

Its hard to write in words but I hope that gives you an Idea.
This sort of thing has been asked many times in the past.
I suggest do some you searches here and see what you come up with.

In the meantime, I have some in my subscriptions list.
I will post links here when I find them and I have the time to look.

Don't wait for me to do your search though. Let us know if you find something so we don't waste time looking for an answer and you already have one.
Dec 11 '09 #2
shalskedar
66 New Member
Thanks !!Even i m still working on it...Will surely revert u back if anything clicks
Dec 11 '09 #3
shalskedar
66 New Member
can any one suggest me some vba code to check whether the current record value for a given column in a query is 0 & if it is 0 then
it should rerieve the next value in the query
Dec 12 '09 #4
Delerna
1,134 Recognized Expert Top Contributor
first
From the code window goto Tools/referenses and ensure that the "microsoft DAO 3.6" type library is checked.


Here is some general code for manipulating DAO recordsets in VBA
It's not meant as a working example but as a guide for you.

Expand|Select|Wrap|Line Numbers
  1.    'Create a DAO database object
  2.    Dim DB As DAO.Database 
  3.  
  4.    'And a DAO recordset object
  5.    Dim rst As DAO.Recordset 
  6.  
  7.    'connect the database object to a database . In this case the current one
  8.    Set DB = CurrentDb 
  9.  
  10.    'retrieve a recordset into the recordset object
  11.    Set rst = DB.OpenRecordset("Select Fields From YourTable",dbOpenDynamic) 
  12.    'dbOpenDynamic lets you move backwards as well as forwards through the recordset
  13.  
  14. Do While Not rst.EOF 
  15.    'you can refer to a field by its ordinal position...
  16.    'Good for looping through each field and applying a piece of code to each in turn
  17.    n=rst.fields(0)       'n = the value of the first field of the current record
  18.    'or you can also use the fields name. Makes code more readable
  19.    n=rst.fields("FieldName")  
  20.    rst.MoveNext        'Moves to the next record
  21.    rst.MovePrevious  'Moves to the previous record
  22.  
  23.    'Obviously since you are in a loop that repeats until the end of the recorset is reached.
  24.    'You will need to ensure that that occurs at some point
  25. Loop 
  26.  
  27.  
Be warned...these techniques can become quite slow !!!!!!
Always try to do something with SQL before resorting to this.

There is nothing wrong with doing it this way, its just if you can do it with SQL it will almost always be much faster
Dec 13 '09 #5
shalskedar
66 New Member
Thanks!!I tried this out but getting an error as "Invalid argument:Runtim e error 3001".

Sorry!! but Can u Plz suggest me an example with my Queryname as "Query10" & column name as "PayAmount1 " using the above code..
Dec 14 '09 #6
Delerna
1,134 Recognized Expert Top Contributor
Can you post your code and indicate the line where the error is occuring
Dec 14 '09 #7
shalskedar
66 New Member
Dim DB As DAO.Database
Dim rst As DAO.Recordset

Set DB = CurrentDb

Set rst = DB.OpenRecordse t("Select rst![PayAmount1] From [Query10] Group by [Job Reference]",dbOpenDynamic )

Do While Not rst.EOF
'n=rst.fields(0 )
n=rst.fields("P ayAmount1")
if n.value=0 then
rst.MoveNext
debug.print n
else
debug.print n
end if


Loop


This code shows the error "Invalid argument"...
Dec 15 '09 #8

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

Similar topics

4
12835
by: Gene | last post by:
When entering a record in a form, I would like a value in a field of the previous record to be entered automatically into a different field of the current record. Which way should I go? Is it also possible to do this based on criteria from the previous record?
3
14532
by: William Wisnieski | last post by:
Hello Again, I'm really stuck on this one.....so I'm going to try a different approach to this problem. I have a query by form that returns a record set in a datasheet. The user double clicks on a row and a main form (pop up) opens bound to a table with a continuous subform bound to another table. On the main form is a field called . It is vital that this is
8
12090
by: Zlatko Matić | last post by:
There is a form (single form) and a combobox. I want that current record of the form is adjusted according to selected value in the combobox. Cuurrent record should be the same as the value in the combobox. What is the solution? Thank you in advance.
2
3997
by: MLH | last post by:
Fields in MyTable: PostID PostDate RollQtyXfer RollDenomination RollCount37 RollCount23
1
1997
by: phpmaet | last post by:
Hi I have viewed the record from mysql table. But I need to retrieve the next record id while reading the each an every record. Could someone explain how I retrieve the next record id in a table(Note. WHILE READING THE CURRENT RECORD)? Thanks
13
3424
by: kev | last post by:
Hi all, I have created a database for equipments. I have a form to register the equipment meaning filling in all the particulars (ID, serial, type, location etc). I have two buttons at the end of the form which is submit and cancel. After i have clicked submit, the information is stored directly into my corresponding database table. My problem here is i need to retrieve back the information submitted to display all the data that the...
4
3165
by: Simon Gare | last post by:
Hi all, I am trying to retrieve a count of booking entries made 30 days ago, below is the end of the query I am having problems with. dbo.booking_form.TimeOfBooking = DATEADD(day, -30, GetDate()) GROUP BY dbo.booking_form.TimeOfBooking") When I use the = sign the error reads
7
3444
by: rockdc1981 | last post by:
i have audit trail module using this code... Option Compare Database Option Explicit Public Function AuditTrail() On Error GoTo Err_Audit_Trail 'ACC2000: How to Create an Audit Trail of Record Changes in a Form 'http://support.microsoft.com/default.aspx?scid=kb;en-us;197592
1
4097
maxamis4
by: maxamis4 | last post by:
Hello folks, Here is the backgroup. I am creating an agent that can find a user in LDAP and return the last logon date. Now i am not sure if with active directory you can user the SAMAccountName to retrieve the last computer the users logged on to. But i am looking for a place where i can learn this information. I was hoping someone out there could point me in the right direction. Below is my code which i have been working on. It can...
0
8384
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
8901
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
8813
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
8591
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
8659
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
7412
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...
0
4388
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2037
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1791
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.