Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    burrina's Avatar
    burrina is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2012
    Location
    Freeport,Texas
    Posts
    1,383

    need help with sql statement

    I need to turn this: & "#, tblEvent.newid = " & NextEventIDCounter & ", " _




    Into this: = Nz(DMax("[NextCustomEventIDCounter]", "EventCounterTable"), 0) + 1

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    Presuming you mean the NextEventIDCounter value, just replace it.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    burrina's Avatar
    burrina is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2012
    Location
    Freeport,Texas
    Posts
    1,383
    YES, that's what I need. I am lost with this sql, I do good to try and hang in there with vba.

  4. #4
    alcapps is offline Competent Performer
    Windows 8 Access 2010 32bit
    Join Date
    Jan 2012
    Posts
    292
    ok.. write a function.. I thought we had this settled..

    Public Function GetNextID(tblName as string, idfieldName as string) as Long
    dim db as DAO.database
    dim rst as DAO.recordset
    dim lngOut as Long

    LngOut = 0
    set db = currentdb()

    Set rst = db.OpenRecordset("select Max([" & idfieldName & "]) as ID from [" & tblName & "]", dbOpenSnapshot)

    if rst.recordcount > 0 then
    LngOut = rst!ID + 1
    end if
    rst.Close

    GetNextID = LngOut

    end function

    haven't tested this code.. but that should be good..

  5. #5
    burrina's Avatar
    burrina is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2012
    Location
    Freeport,Texas
    Posts
    1,383
    Is that something I put in a module? If so, how do I call it? Or add to the existing code already behind the command button on the form?

    Here is my final edited version of what you sent. I put this is a module named ModNextEventCounter
    Public Function GetNextID(EventCounterTable As String, NextCustomEventIDCounter As String) As Long
    Dim db As DAO.Database
    Dim rst As DAO.Recordset
    Dim lngOut As Long

    lngOut = 0
    Set db = CurrentDb()

    Set rst = db.OpenRecordset("select Max([" & NextCustomEventIDCounter & "]) as ID from [" & EventCounterTable & "]", dbOpenSnapshot)

    If rst.RecordCount > 0 Then
    lngOut = rst!id + 1
    End If
    rst.Close

    GetNextID = lngOut

    End Function

    _________________________ OR ADD TO THIS:
    Public Function GetNextID(EventCounterTable As String, NextCustomEventIDCounter As String) As Long
    Dim db As DAO.Database
    Dim rst As DAO.Recordset
    Dim lngOut As Long

    lngOut = 0
    Set db = CurrentDb()

    Set rst = db.OpenRecordset("select Max([" & NextCustomEventIDCounter & "]) as ID from [" & EventCounterTable & "]", dbOpenSnapshot)

    If rst.RecordCount > 0 Then
    lngOut = rst!id + 1
    End If
    rst.Close

    GetNextID = lngOut

    End Function

  6. #6
    alcapps is offline Competent Performer
    Windows 8 Access 2010 32bit
    Join Date
    Jan 2012
    Posts
    292
    ok in a sql statement you can

    Select GetnextID("EventCounterTable","NextCustomEventIDCounter") as ID, "Hello" as field1 from table

    or in an update query..
    in the set ID = GetNextID("EventCounterTable","NextCustomEventIDCounter")

    in vba

    X = GetNextID("EventCounterTable","NextCustomEventIDCounter")

    or

    rst!ID = GetNextID("EventCounterTable","NextCustomEventIDCounter")

    in the immediate window..try

    ?GetNextID("EventCounterTable","NextCustomEventIDCounter")

    the code was setup so you could pass any table name and it's id field and it would return the max ID value back + 1. So you can use this for more than just one table.

    hope this helps

  7. #7
    burrina's Avatar
    burrina is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2012
    Location
    Freeport,Texas
    Posts
    1,383
    Thanks, but I am afraid that did not help. I have code already in place that I need to edit or add to and this only confuses me.

  8. #8
    alcapps is offline Competent Performer
    Windows 8 Access 2010 32bit
    Join Date
    Jan 2012
    Posts
    292
    ok..

    if you are in a query graphically..

    in the name field try..

    NextID: GetNextID("EventCounterTable","NextCustomerEventID Counter")

    run it and see if doesn't bring back the next id..

    assuming that you have a table named EventCounterTable and an Id field names nextCustomerEventIDCounter.. which has the id's in it your are trying to increment.

    if you looked at the sql statement it would be

    select GetnextID("EvnetCounterTable","NextCustomerEventID Counter") as NextID

    if you don't get this then I guess you will stay confused.. Sorry..

  9. #9
    burrina's Avatar
    burrina is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2012
    Location
    Freeport,Texas
    Posts
    1,383

    Tried to implement code, error

    Ok, I tried to implement what you wrote: Here is what I got! Revision, was able to run query, here is the end result.
    Attached Thumbnails Attached Thumbnails SelectCaseErrorMessageInCode.jpg   GetCodeError.jpg   qryfinallyRanResults.jpg  
    Last edited by burrina; 02-01-2013 at 09:42 PM. Reason: tried running qry

  10. #10
    burrina's Avatar
    burrina is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2012
    Location
    Freeport,Texas
    Posts
    1,383
    I give up. Thanks Anyway! Mark as Solved!

  11. #11
    alcapps is offline Competent Performer
    Windows 8 Access 2010 32bit
    Join Date
    Jan 2012
    Posts
    292
    you give up to easy..

    been busy..

    but you have the case messed up

    Case = GetnextID(".......... like you have it) remove the as nextid thats for a query.. not a call
    is NextCustomerEventID counter you field name?

    this only works for a real table entry not a query..

    this update a table...

    what I gave you is for a table..
    and you wanted to add a new record using an select sql.

    what you are doing is a batch. so you would need to have a base number maybe from a table using the code I sent you can use. but in a query you will need to start with that number and increment from there.. then send that output to a table.

    if you want a query to work.. you have to do this..
    SELECT t.[idfield], GetNextID("YourTableNameHere","yourtableIDfieldnam ehere") AS BaseNextID,((select count([idfield]) from [Master Cable Code Table (TRI)] where [idfield] <= t.[idfield]) + GetNextID("YourTableNameHere","yourtableIDfieldnam ehere") ) as newIDnumber from [YourTableNameHere] t

    anyway it is frustrating because it is a lot of code ..

    understand if you give up

  12. #12
    burrina's Avatar
    burrina is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2012
    Location
    Freeport,Texas
    Posts
    1,383
    Here is what I am trying right now. Me.newid = DMax("EventCounterTable", "NextCustomEventIDCounter") + 1
    If Me.Dirty Then Me.Dirty = False 'Force save record.
    EventCounterTable is my table. and NextCustomEventIDCounter is the field in the table set to number, no PK but no duplicates.
    It says it cant find NextustomEventIDCounter

  13. #13
    alcapps is offline Competent Performer
    Windows 8 Access 2010 32bit
    Join Date
    Jan 2012
    Posts
    292
    if you are going to use Dmax(expression , domain)

    so it would be dmax("[field]","[Table]") + 1

  14. #14
    alcapps is offline Competent Performer
    Windows 8 Access 2010 32bit
    Join Date
    Jan 2012
    Posts
    292
    this will work..
    change the "table name" and "field name" in this to point to the table and the field you are trying to get a max value from.
    me.newid = getNextID("table name", "field name")


    put this in module.. don't change anything in this function...its taking variables so you can name the table and field..
    Public Function GetNextID(tblName As String, idfieldName As String) As Long
    Dim db As DAO.Database
    Dim rst As DAO.Recordset
    Dim lngOut As Long


    lngOut = 0
    Set db = CurrentDb()


    Set rst = db.OpenRecordset("select Max([" & idfieldName & "]) as ID from [" & tblName & "]", dbOpenSnapshot)


    If rst.RecordCount > 0 Then
    lngOut = rst!ID + 1
    End If
    rst.Close


    GetNextID = lngOut


    End Function

  15. #15
    burrina's Avatar
    burrina is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2012
    Location
    Freeport,Texas
    Posts
    1,383
    Here is what happens with that code. It appends the already edited record that has a newid of 1001 back to 1000 since the table EventCounterTable and it's field NextCustomEventIDCounter do not get updated, it stays the same, NextCustomEventIDCounter has a value of 999 so all items added afterwards get a value of 1000. However it stopped adding records after the first one?

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. if statement in sql statement, query builder
    By 54.69.6d.20 in forum Access
    Replies: 4
    Last Post: 09-11-2012, 07:38 AM
  2. Replies: 7
    Last Post: 08-17-2011, 01:49 PM
  3. If Statement
    By MFS in forum Programming
    Replies: 13
    Last Post: 06-16-2011, 08:54 PM
  4. Help with an Iif statement please
    By 10 Gauge in forum Programming
    Replies: 4
    Last Post: 04-05-2011, 06:02 AM
  5. for each statement in vba
    By tuyo in forum Access
    Replies: 0
    Last Post: 03-22-2011, 05:42 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums