Results 1 to 15 of 15

Thread: Auto Increment Value problem

  1. #1
    Join Date
    Jun 2004
    Location
    Edmonton Alberta
    Posts
    19

    Unhappy Auto Increment Value problem

    I know that multiple threads discuss similar situations but I am currently at a loss to find the correct solution.

    I am trying to update a table and related subform (tblEvidenceItems) that tracks Computer Security incidents which includes the tracking of evidence items.

    What I need to do is automatically create an incremental number for each piece of evidence based on the IncidentID.

    For example:

    If IncidentID 07-0067 has 6 pieces of evidence, I would have to show Item 001, Item 002, ... Item 006, etc.

    If the next IncidentID has 12 pieces of evidence, I need to show Item 001, Item 002, ... Item 012.

    The fieldname for the Evidence Items in tblEvidenceItems is ItemDetailNo.

    Can anyone provide me with some assistance??

  2. #2
    Join Date
    May 2006
    Posts
    407
    Here is a sample of how I have done this in the past:
    Code:
      NextSONumber = CSng(Int(Nz(DMax("SortOrder", "tblPlanElevOption", "ProjectID=" & ProjectID))) + 1)
    In this code I retrieved the current Maximum "SortOrder" number for the ProjectID I'm currently working with. I use the Nz() function to change a Null (a null is returned if there are as of yet no records for this ProjectID) into a 0. Then that 0, or the number returned, I make sure is an integer with the Int() function, and then I change that Integer into a Single (CSng() function). After all that, I then add a 1 so I have then next number.

    HTH,

  3. #3
    Join Date
    Jun 2004
    Location
    Edmonton Alberta
    Posts
    19
    Not to sound like a complete newbie BUT where do I put this code do I have to do anything else like declare variables etc.?

  4. #4
    Join Date
    May 2006
    Posts
    407
    I used the BeforeInsert event to create a number for the SortOrder field.

    Is this a number that the user really needs to use and see? In my case it is. They can use this number to rearrange the list as they so desire. But in your case, is this number just being used to give you a unique key value? If so, I would suggest using AutoNumber.

    By the way, there is nothing wrong with being a Newbie! We all where, and sometimes still are.

  5. #5
    Join Date
    Jul 2007
    Location
    Kent, England
    Posts
    3

    Automatically Incremental

    Quote Originally Posted by Joseppi
    I know that multiple threads discuss similar situations but I am currently at a loss to find the correct solution.

    I am trying to update a table and related subform (tblEvidenceItems) that tracks Computer Security incidents which includes the tracking of evidence items.

    What I need to do is automatically create an incremental number for each piece of evidence based on the IncidentID.

    For example:

    If IncidentID 07-0067 has 6 pieces of evidence, I would have to show Item 001, Item 002, ... Item 006, etc.

    If the next IncidentID has 12 pieces of evidence, I need to show Item 001, Item 002, ... Item 012.

    The fieldname for the Evidence Items in tblEvidenceItems is ItemDetailNo.

    Can anyone provide me with some assistance??
    Hi!

    I am just wondering how you have the relationship for the tables set up, and what fields you are using.

  6. #6
    Join Date
    Jul 2007
    Location
    Kent, England
    Posts
    3

    Smile

    Quote Originally Posted by Joseppi
    I know that multiple threads discuss similar situations but I am currently at a loss to find the correct solution.

    I am trying to update a table and related subform (tblEvidenceItems) that tracks Computer Security incidents which includes the tracking of evidence items.

    What I need to do is automatically create an incremental number for each piece of evidence based on the IncidentID.

    For example:

    If IncidentID 07-0067 has 6 pieces of evidence, I would have to show Item 001, Item 002, ... Item 006, etc.

    If the next IncidentID has 12 pieces of evidence, I need to show Item 001, Item 002, ... Item 012.

    The fieldname for the Evidence Items in tblEvidenceItems is ItemDetailNo.

    Can anyone provide me with some assistance??
    Hi Joseppi

    First off, who have you linked the IncidentID, and the ItemDetailNo together.

  7. #7
    Join Date
    Jun 2004
    Location
    Edmonton Alberta
    Posts
    19
    The IncidentID is currently not linked to the ItemDetailNo. The IncidentID in tblEvidenceItems is linked to the IncidentID in tblIncidents

  8. #8
    Join Date
    Jun 2004
    Location
    Edmonton Alberta
    Posts
    19
    Quote Originally Posted by GolferGuy
    Here is a sample of how I have done this in the past:
    Code:
      NextSONumber = CSng(Int(Nz(DMax("SortOrder", "tblPlanElevOption", "ProjectID=" & ProjectID))) + 1)
    In this code I retrieved the current Maximum "SortOrder" number for the ProjectID I'm currently working with. I use the Nz() function to change a Null (a null is returned if there are as of yet no records for this ProjectID) into a 0. Then that 0, or the number returned, I make sure is an integer with the Int() function, and then I change that Integer into a Single (CSng() function). After all that, I then add a 1 so I have then next number.

    HTH,
    And how would I code that for my database? Is the NextSONumber simply an alias for something? I figure I would replace "SortOrder" with ItemDetailNo, tblPlanElevOption would be replaced by tblEvidenceItems BUT I'm not sure what I would put in place of the ProjectID entry??

  9. #9
    Join Date
    May 2006
    Posts
    407
    ProjectID would be replaced by IncidentID.

    NextSONumber in my code is just that, it is the Next Sort Order Number. You are after the Next Item Detail Number as I understand your request. Therefore, wherever you need to put your Next Item Detail Number, this is the variable that will have that number for any new Item Detail records.

  10. #10
    Join Date
    Jun 2004
    Location
    Edmonton Alberta
    Posts
    19
    Code is generating a run-error (3075). Syntax error (missing operator) in query expression 'IncidentID='.

  11. #11
    Join Date
    May 2006
    Posts
    407
    Please show the entire SQL string if this is a query, or all the VBA code that builds the SQL string if this is done in VBA code.

  12. #12
    Join Date
    Jun 2004
    Location
    Edmonton Alberta
    Posts
    19
    Below is the code I used on the BeforeInsert of the subform:

    Option Compare Database

    Private Sub Form_BeforeInsert(Cancel As Integer)
    NextItemDetailNo = CSng(Int(Nz(DMax("ItemDetailNo", "tblEvidenceItems", "IncidentID=" & IncidentID))) + 1)
    End Sub

  13. #13
    Join Date
    May 2006
    Posts
    407
    The error you are receiving is because IncidentID does not have a value in it yet. So, you need to find out why IncidentID has no value. Where is IncidentID defined? If it is not defined on the subform you are trying to use it in, why not? I would think IncidentID would be the field you would be using for the Child/Master link field for the subform. But, no matter what, you need to search for where IncidentID is, and get that data into your subform if that really is the field you need to use to calculate NextItemDetailNo.

  14. #14
    Join Date
    Jun 2004
    Location
    Edmonton Alberta
    Posts
    19
    I've corrected that oversight but the nextItemDetailNo value is still not populating the field. Am I supposed to do something else with the ItemDetailNo field??

  15. #15
    Join Date
    May 2006
    Posts
    407
    but the nextItemDetailNo value is still not populating the field
    Somewhere, you need to set whatever field needs this newly set number to this new number. For example, if the field that is being used as the "autonumber" field is ItemDetailNo, then somewhere you need to set ItemDetailNo = NextItemDetailNo. I know of no other way to get this new number into the field where it needs to be.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •