Results 1 to 2 of 2
  1. #1
    slaterino is offline Novice
    Windows XP Access 2003
    Join Date
    Apr 2009
    Posts
    25

    Creating a unique ID for a new set of values

    I'm currently using Visual Basic code to insert values into a temporary table and am looking to change one thing. When I add the values sometimes I will just add one value at a time or sometimes it could be more, there is no limit. Each time I add these values I would like to have a unique ID attached to each value that is added at that specific time, kind of like a batch ID. I was thinking the way to do this would be to use a '+1' formula or something like that. Is this the best way? Would I need to do some kind of lookup for the highest ID and then do the '+1'? I am far less sure about using Visual Basic for these things than SQL! Here is my current code:



    Code:
        varNotes = Me.txtNotes
        lngLocID = Me.cboLocID
        lngActID = Me.cboActID
        lngActName = Me.txtActivityName
        lngPrjBox = Me.cboProjectBox
        Set db = CurrentDb
        If Me.grpRepeats = 2 Then 'need to loop through dates
            For datThis = Me.txtStartDate To Me.txtEndDate
                intDIM = GetDIM(datThis)
                intDOW = Weekday(datThis)
                If Me("chkDay" & intDIM & intDOW) = True Or _
                        Me("chkDay0" & intDOW) = True Then
                    strSQL = "INSERT INTO tbl_temp_schedule_dates ( tscDate, tscActID, tscLocID, tscActivityName, tscProjectBox, tscStartTime, tscEndTime, tscNotes ) " & _
                        "Values(#" & datThis & "#," & lngActID & ", " & _
                        lngLocID & ",""" & lngActName & """," & lngPrjBox & ", #" & Me.txtStartTime & "#, #" & _
                        Me.txtEndTime & "#," & _
                        IIf(IsNull(varNotes), "Null", """" & varNotes & """") & ")"
                    db.Execute strSQL, dbFailOnError
                End If
            Next
         Else  'dates are there, just add the title, notes, times, location, Activity
            strSQL = "Update tbl_temp_schedule_dates Set tscActID = " & lngActID & _
                ", tscActivityName = """ & lngActName & """, tscProjectBox = " & lngPrjBox & _
                ", tscLocID = " & lngLocID & ", tscStartTime =#" & Me.txtStartTime & _
                "#, tscEndTime = #" & Me.txtEndTime & "#"
                
            If Len(varNotes & "") > 0 Then
                strSQL = strSQL & ", tscNotes = " & IIf(IsNull(varNotes), Null, """" & varNotes & """")
            End If
            db.Execute strSQL, dbFailOnError
        End If
        Me.sfrm_temp_schedule_edit.Requery
        MsgBox "Temporary schedule built. " & _
            "You can now edit the schedule and " & _
            "append to the permanent schedule.", vbOKOnly + vbInformation, "Temp schedule complete"
    End Sub

  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,521
    You can find the highest ID with a DMax() function, and add 1 to that. You may want to wrap the DMax() in an Nz() function to handle the possibility of there being no records, if appropriate to your application.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Unique Values and Boolean Fields
    By Triad in forum Forms
    Replies: 1
    Last Post: 07-15-2010, 06:28 PM
  2. Count of Unique Values
    By vsmk in forum Queries
    Replies: 2
    Last Post: 03-14-2010, 12:07 AM
  3. how to set a unique ID
    By archie in forum Access
    Replies: 1
    Last Post: 09-08-2009, 04:28 AM
  4. Replies: 0
    Last Post: 10-23-2008, 12:08 AM
  5. Creating top values with two records
    By avesamuel in forum Queries
    Replies: 0
    Last Post: 10-15-2007, 10:45 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