Solved

Access MAX function only finds single digit numbers

Posted on 2010-11-26
9
516 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 75
ID: 34219304
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 55

Expert Comment

by:Huseyin KAHRAMAN
ID: 34219308
run a query to get the max

select max(TopRecord) as NewInvoiceNumber from InvoiceDetails
0
 

Author Comment

by:dizzycat
ID: 34219341
I have removed the where clause and I am still getting 9 as the MAX record number when it should be 10
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 75
ID: 34219362
What is the Data Type of TopRecord?  If ... it's Text as opposed to Numeric, that might be the problem.

mx
0
 
LVL 39

Expert Comment

by:Aaron Tomosky
ID: 34219374
It should be int. It's probably char or varchar.
0
 
LVL 55

Expert Comment

by:Huseyin KAHRAMAN
ID: 34219388
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 - Microsoft MVP, Access and Data Platform) earned 500 total points
ID: 34219393
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
ID: 34219407
DMax("[RecordNumber]","Table5")  Returns 9 ... from that table in the image:


Capture1.gif
0
 

Author Closing Comment

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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

696 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