Results 1 to 12 of 12
  1. #1
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142

    Can an Autonumber Field Generate a Value of zero?


    Can an autonumber field generate a value of zero? And what about negative numbers?

  2. #2
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,402
    Not to my knowledge.

  3. #3
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,522
    no. counting begins with 1 and goes up.

  4. #4
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142
    So in a VBA function with a Long return type that returns a record's Primary Key that is of AutoNumber data type, it's perfectly safe for my function to return zero or even a negative number in the event of an error (or not found, etc.) and I won't inadvertently be returning a "false negative" error to my calling sub?

  5. #5
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,402
    A function that returns a long can certainly return zero or a negative number. Depends on the function's calculations. The function will return whatever you tell it to return. If you return an error value, that's not a record's primary autonumber key, is it?
    Explain what the function does, and what you want it to return, post it's code. We can evaluate what you are trying to achieve.

  6. #6
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,776
    not disagreeing with the suggestion to post the function, etc. but why not have the function return a variant if it can only find or not find the autonumber value? If the return value is a long, then you have the number you need. If not, you make it return Null. Then you can deal with the return value right away - in the calling part of the code. Like

    If IsNull (MyFunction) Then
    do number not found stuff, like exit sub
    Else
    do number found stuff
    End If
    Of course, you can reverse the steps, but hopefully you get the idea.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  7. #7
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142
    Quote Originally Posted by davegri View Post
    A function that returns a long can certainly return zero or a negative number. Depends on the function's calculations. The function will return whatever you tell it to return. If you return an error value, that's not a record's primary autonumber key, is it?
    I think you've misunderstood the question. I wondering if it would be safe to use a return value of zero as error code to the calling sub with out risking returning a false error in the following scenario

    Explain what the function does, and what you want it to return, post it's code. We can evaluate what you are trying to achieve.
    See the following dummy code:

    Code:
    Function get_id_by_name( name as string) as Long
    On error goto ErrHandler
      'do work
      get_id_by_name = results from work above or 0 if not found
    
    ExitHandler:
      Exit Function
    
    ErrHandler:
      get_id_by_name = 0
      resume ExitHandler
    
    End Function
    
    Sub testing()
      Dim id as Long
      id = get_id_by_name("Frank")
      if id = 0 then
        'frank wasn't found or there was an error so do something about it
      else
        'do work
      end if
    End Sub
    If an AutoNumber field could ever equal zero this would be a problem for the dummy code posted above, a "false negative". The calling sub would think it's an error code but it really was the id that it was looking for. So, that's why I ask if an AutoNumber field can ever generate a zero (or even a negative).

  8. #8
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,409
    Can an autonumber field generate a value of zero? And what about negative numbers?
    the autonumber can be set to random rather than incremental. Something I have done in the past to prevent users from assigning any meaning to the PK. When set to random, it will generate negative numbers but so far as I am aware, not a zero.

    note you can populate an autonumber field with a zero using an append query

  9. #9
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,776
    if it would be safe to use a return value of zero as error code
    It's not an error code per se, and you're not using it as one either. You're using the numerical result of the function to decide on subsequent code execution, thus it's not an error at all - it's a result. So yes, it's safe since you're not trying to update the autonumber with the return value, nor create another record using that value as a PK or FK. You could have just as easily decided on what to do using "apples" vs "oranges". I still think returning a variant makes the most sense since you don't have to declare the id variable or assign it to anything.
    Last edited by Micron; 02-21-2018 at 04:59 PM. Reason: spelin and gramur

  10. #10
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142
    Quote Originally Posted by Ajax View Post
    the autonumber can be set to random rather than incremental. Something I have done in the past to prevent users from assigning any meaning to the PK. When set to random, it will generate negative numbers but so far as I am aware, not a zero.

    note you can populate an autonumber field with a zero using an append query
    Ah okay. So definitely don't do what I was suggesting then. I'll continue using variant return types as Micron suggested. It would just be slightly nicer and more readable if I could do like this:

    Dim rslt as Long
    rslt = my_function()
    if rslt then
    'do work
    end if

    rather than doing like this:

    Dim rslt as Variant
    rslt = my_function()
    if not iserror( rslt ) then
    'do work
    end if

    Oh well. Thank you for the schooling, gents.

  11. #11
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,409
    you can get round it this way

    Code:
    Function get_id_by_name( strname as string, FoundID as Long) as Long
    On error goto ErrHandler
      'do work
      FoundID= results from work above
      get_id_by_name=1
    
    ExitHandler:
      Exit Function
      
    ErrHandler:
      get_id_by_name = 0
      resume ExitHandler
    
    End Function
    and call it this way

    Code:
    dim id as long
    if get_id_by_name("Jones",id)<>0 then
       debug.print id
    end if

  12. #12
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,776
    I'll continue using variant return types as Micron suggested. It would just be slightly nicer and more readable if I could do like this:
    As mentioned, you don't need a variable at all - depending on how you do this. But there's a lot missing from your function so I have to be very general
    Code:
    Function get_id_by_name(name as string) as Variant 
    On error goto ErrHandler
      'do work, get results
     if results not found then get_id_by_name = Null
     if results found then get_id_by_name = the result
    
    ExitHandler:
      Exit Function
    End Function
    The calling code example for this is in post 6. Not sure any more what the issue is about returning a number over a variant, or vice versa. The only reason I suggested the variant is because then you don't need a variable for the result - you can use the IsNull function to directly determine. Other than that, there's not much difference, unless you need to use the result of the function somehow. This is unclear.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. AutoNumber/ Generate specific ID Numbers
    By QA_Compliance_Advisor in forum Programming
    Replies: 4
    Last Post: 07-23-2014, 07:33 AM
  2. generate autonumber
    By raffi in forum Access
    Replies: 5
    Last Post: 03-27-2014, 11:41 AM
  3. Replies: 1
    Last Post: 12-10-2013, 06:27 AM
  4. Replies: 3
    Last Post: 06-09-2012, 09:49 PM
  5. Replies: 1
    Last Post: 06-09-2012, 05:13 PM

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