Results 1 to 5 of 5
  1. #1
    nygammel is offline Novice
    Windows 8 Access 2013
    Join Date
    Sep 2014
    Posts
    1

    beregne og tildele id til ny post


    Jeg har en tabell og et skjema som viser en post i tabellen. Jeg ønsker å legge til en kortroll (knapp) i skjemaet som lager ny post og samtidig tildeler id som skal være lik id på siste foregående post +1. Jeg er ny accessbruker men mener jeg har blitt fortrolig med tabeller, spørringer, skjema og kontroller men er ukjent med makroer og VBA (finner ikke ut av dette og trenger hjelp for å kommer over en kneik- automatisk tildeling av verdier i ny post)

  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,642
    Translating your post to English is much more likely to get a helpful response.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Shouldn’t do both of these tasks with the button! For the 'new record' button:

    Code:
    DoCmd.GoToRecord , , acNewRec

    For the auto-incrementing record number, here's a typical hack. The first code here would be for an IDNumber that is defined in the table as Text datatype. "Number" fields that aren't used for math are frequently defined as Text.

    If IDNumber defined as Text:

    Code:
    Private Sub Form_BeforeUpdate(Cancel As Integer)
    
    If Me.NewRecord Then
       If RecordsetClone.RecordCount = 0 Then
       Me.IDNumber = "1"
      Else
        Me.IDNumber = DMax("val([IDNumber])", "YourTableName") + 1
      End If
    End If
    End Sub


    Here's the same code for an IDNumber defined as a Number:

    Code:
    Private Sub Form_BeforeUpdate(Cancel As Integer)
    If Me.NewRecord Then
      If RecordsetClone.RecordCount = 0 Then
        Me.IDNumber = 1
      Else
        Me.IDNumber = DMax("[IDNumber]", "YourTableName") + 1
      End If
    End If
    End Sub

    The above has been used safely for multi-user environments, in my experience, because it doesn't assign the ID number until the very last second before the record is saved. This is necessary to decrease the chance of two users getting the same ID number. The only drawback is that the user cannot see the ID number until the record is saved.

    In a single user environment, the code can be moved to the Form_Current event and the ID number will appear as soon as a new record is started.

    Linq ;0)>

  4. #4
    azhar2006's Avatar
    azhar2006 is offline Expert
    Windows 7 32bit Access 2010 32bit
    Join Date
    Mar 2012
    Posts
    528
    Usesomething like
    Code:
    
    
    Code:
    Private Sub Form_Current()
      If Me.NewRecord Then
        On Error Resume Next 'It should never occur, just to be sure...
        Me!MailingListID.DefaultValue = Nz(DMax("[MailingListID]", "tblMailingList"), 0) + 1
      End If
    End Sub


  5. #5
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    That first code sample looks to be a little thin

    The second bit of code, assigning the ID in the Form_Current Event, can only be safely done, as I said, if this app is to be used as a stand-alone app. If it were to be used in the Form_Current Event in a multi-user environment, you run the risk of one user starting a New Record, and before he saves that Record, another user starting a New Record, in which case two Records will have the same ID! In multi-user environments the code has to be in the Form_BeforeUpdate event...which fires at the last instant before the Record is saved. Using this strategy I've never had a duplicate ID issued.

    Linq ;0)>

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

Similar Threads

  1. REST api POST method
    By irade92 in forum Programming
    Replies: 7
    Last Post: 02-03-2015, 03:21 PM
  2. My post was deleted.
    By azeotrope in forum Queries
    Replies: 2
    Last Post: 06-20-2013, 03:34 PM
  3. Need to post a text in Form...
    By Stephanie53 in forum Forms
    Replies: 2
    Last Post: 04-02-2013, 09:08 AM
  4. How to like a post, or thank someone who helped you
    By Matrix in forum Forum Suggestions
    Replies: 0
    Last Post: 11-18-2012, 11:18 AM

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