SQL Statement in Access 2007 VBA Returning max value

Posted on 2011-10-12
Last Modified: 2012-05-12
I have a command button that when clicked, needs to look at a table and select the record with the highest "LotNo" number.  (which is the primary key).

Once I get that number, I'm going to look at how many pounds have been assigned to it and take some action accordingly.  But I think I'm ok on all of that, I am just fumbling in trying to get the highest number from the "tblLots" table using runcmd.sql statement.    Probably try to make it too complicated.

I've tried so many different things I wouldn't know what to post to show.... guess I'm putting the "" marks in the wrong spot.

Table name = tblLots
"LotNo", Number, Long Integer (need to return the highest number in the table)
LotDate, Date, Short (not really using this yet)
Lbs, Number, Long Integer (if the pounds are over 10,000, then I will create a new LotNo which will be the next sequential number)

Any help is appreciated.
Question by:TechGuise9
    LVL 75

    Accepted Solution


    DMax("[LotNo]","tblLots")  would seem to be what you need.

    LVL 75

    Expert Comment

    by:DatabaseMX (Joe Anderson - Access MVP)
    btw ... DoCmd.RunSQL is only for executing Action queries (update, append, delete make table).


    Author Closing Comment

    Thanks.   Had a feeling I was making it too hard.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
    Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
    Familiarize people with the process of utilizing SQL Server stored procedures 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 Micr…
    What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

    759 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

    12 Experts available now in Live!

    Get 1:1 Help Now