473,406 Members | 2,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

How About the MAX() function

i have a field by name IDN in the access table that is of field type autonumber!
so when the form loads, i want the next automatically generated id to appear in the textbox named CIDN.text(enabed=false). this was the code i tried out ::::

Expand|Select|Wrap|Line Numbers
  1. IdRs.Open "SELECT MAX(IDN) as NewId from expenses", DB, adOpenDynamic, adLockOptimistic
  2. NewId = NewId + 1
  3. CIDN.Text = NewId
The thing is that im always getting the number"1" in the textbox eventhough the recordset has more than 30records. It is not returning the highest IDN,

Can anybody suggest me what the problem is and do tell me the mistake i have done and also the best way of doing such a thing.
Apr 27 '07 #1
4 2527
ansumansahu
149 100+
i have a field by name IDN in the access table that is of field type autonumber!
so when the form loads, i want the next automatically generated id to appear in the textbox named CIDN.text(enabed=false). this was the code i tried out ::::

Expand|Select|Wrap|Line Numbers
  1. IdRs.Open "SELECT MAX(IDN) as NewId from expenses", DB, adOpenDynamic, adLockOptimistic
  2. NewId = NewId + 1
  3. CIDN.Text = NewId
The thing is that im always getting the number"1" in the textbox eventhough the recordset has more than 30records. It is not returning the highest IDN,

Can anybody suggest me what the problem is and do tell me the mistake i have done and also the best way of doing such a thing.
If you simply execute the SQL query in the query analyser does it return you the record.

-ansuman sahu
Apr 27 '07 #2
cmrhema
375 256MB
If you simply execute the SQL query in the query analyser does it return you the record.

-ansuman sahu
Do try out as ansuman sahu said Usually this works well in SQL Query but I faced the same problem when I had to do that type of coding
I prefer you try out as below
Expand|Select|Wrap|Line Numbers
  1. IdRs.Open "SELECT MAX(IDN)  from expenses", DB, adOpenDynamic, adLockOptimistic
  2. CIDN.Text = IdRs.Fields(0)+1
This worked for me.
Apr 27 '07 #3
debasisdas
8,127 Expert 4TB
Dear rave3050


The reason it did't work for u is so simple
u made some basic mistake

in your code
NewId is a column alias in SQL
but the NewId you are using in the next line is a VB variable.
and default value is 0
and when u say +1

it returns 1 .
the code teturns u, what u r asking it to do.

use the code as suggested by CMRHEMA

It will definately work for u.
Apr 27 '07 #4
hey thnx to u and anusmansahu, this works for me too!

thnx a lot!
Apr 27 '07 #5

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

Similar topics

2
by: Aakash Jain | last post by:
Can someone explain me the difference between the following C++ macro and the function MAX: #define MAX(a, b) (((a) > (b)) ? (a) : (b)) template <class T> const T& MAX(const T& a, const T& b)...
2
by: Pink Panther | last post by:
Using the following SQL can the results be explained? Using A97 (with the SP2 for Jet 3.5) or A2002 CREATE TABLE Test (PK Number CONSTRAINT PK_TEST PRIMARY KEY, ParentID Number, Child...
3
by: John A Grandy | last post by:
does .NET have a Max() function ? something like MyMaxValue = Max(Value1,Value2,Value3,Value4)
3
by: ncsthbell | last post by:
I am trying to run a query in access using the 'max' function. For example, I have many rows of data, one for each state and each state has a column for 'Miles'. I want to get the highest number of...
2
by: king imran | last post by:
I am giving u complete code. It works properly But add max and mean functions so that it can work properly. * /LinkList.h/ * #include "Node.h" * /* The LinkList class declaration*/ class**...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...
0
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...
0
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...

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.