Solved

Access MAX function only finds single digit numbers

Posted on 2010-11-26
9
511 Views
Last Modified: 2012-06-22
Hi experts
Using vb.net 2008 i am trying to select the highest record number in a table in my access database, the problem is that the MAX function only finds single digit record numbers when they are the highest record number, when the record number reaches 10 and I run the code again the MAX function only finds number 9 as the highest record number, any ideas anyone ?
Dim dbadp As New System.Data.OleDb.OleDbDataAdapter("SELECT MAX(RecordNumber) as TopRecord FROM InvoiceDetails where Invoicedetails.ID = ? ", "PROVIDER=Microsoft.ACE.OLEDB.12.0;Data Source = C:\myDB.accdb")

        Dim TopRecord As Integer



        dbadp.SelectCommand.Parameters.AddWithValue("InvoiceDetails.ID", txtSearchInvoice.Text) 

        





        dbadp.Fill(ds, "InvoiceDetails")



        For Each myDataRow In ds.Tables("InvoiceDetails").Rows

            



            TopRecord = myDataRow.Item("TopRecord")



            



        Next

        txtRecordNo.Text = (TopRecord + 1)

Open in new window

0
Comment
Question by:dizzycat
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 75

Expert Comment

by:DatabaseMX (Joe Anderson - Access MVP)
Comment Utility
Well, Max() does not have any such limitation, so ... something else is going on, possibly with the criteria.  As a test, remove the WHERE clause and see what you get.

"SELECT MAX(RecordNumber) as TopRecord FROM InvoiceDetails where Invoicedetails.ID = ? ",
     
0
 
LVL 51

Expert Comment

by:HainKurt
Comment Utility
run a query to get the max

select max(TopRecord) as NewInvoiceNumber from InvoiceDetails
0
 

Author Comment

by:dizzycat
Comment Utility
I have removed the where clause and I am still getting 9 as the MAX record number when it should be 10
0
 
LVL 75

Expert Comment

by:DatabaseMX (Joe Anderson - Access MVP)
Comment Utility
What is the Data Type of TopRecord?  If ... it's Text as opposed to Numeric, that might be the problem.

mx
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 38

Expert Comment

by:Aaron Tomosky
Comment Utility
It should be int. It's probably char or varchar.
0
 
LVL 51

Expert Comment

by:HainKurt
Comment Utility
look at your code

dbadp.Fill(ds, "InvoiceDetails")
For Each myDataRow In ds.Tables("InvoiceDetails").Rows
  TopRecord = myDataRow.Item("TopRecord")
Next

it gets the last TopRecord value, where as you want max right?

so try this:

dbadp.Fill(ds, "InvoiceDetails")
TopRecord=0
For Each myDataRow In ds.Tables("InvoiceDetails").Rows
  TopRecord = iif(TopRecord < myDataRow.Item("TopRecord"), myDataRow.Item("TopRecord"), TopRecord)
Next
txtRecordNo.Text = (TopRecord + 1)
 
0
 
LVL 75

Accepted Solution

by:
DatabaseMX (Joe Anderson - Access MVP) earned 500 total points
Comment Utility
I just confirmed that if ... You have a Text data type, then 9 will be returned instead of 10 ... using Max()

So, pretty sure that is your issue.

mx
0
 
LVL 75

Expert Comment

by:DatabaseMX (Joe Anderson - Access MVP)
Comment Utility
DMax("[RecordNumber]","Table5")  Returns 9 ... from that table in the image:


Capture1.gif
0
 

Author Closing Comment

by:dizzycat
Comment Utility
With the select clause removed and the data-type changed to integer instead of number, it works.
Thanks for your help.
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now