Solved

Creating a new ID in VBA

Posted on 2008-06-17
6
459 Views
Last Modified: 2013-11-28
Hello,

I'm trying to create a new ID for any additional records we add into our database. I started out by creating an entry form that (all unbound text boxes) with 2 buttons one which I will append the fields to the table and one to fetch the current max id +1 for this record. the ID button is not working and I've tried different variations. We have 2 tables one which permanent and is a SQL table (linked to MS SQL) and the other a temprary table, both of which have IDs. I'm trying to pull that max number ID and 1 to it and assign it to a variable where I can send the value to a text box on the form. below is my code, what am I doing wrong?
Private Sub cmdGetNewID_Click()
'Get new ID for respondent
 
    Dim intPID As Integer
    Dim db As Database
    Dim rst As Recordset
    
    Set db = CurrentDb()
    
    'check which contact type it will be
    'If radio button 1 is selected then Phys table (Permanent)
    'If radio button 2 is selected then Temp table (Temprary)
    Select Case Me.frmContactType.Value
        Case 1
            Set rst = CurrentDb.OpenRecordset("SELECT Max(tblPhysContactInfo.ID)+1 AS [NewPID] FROM tblPhysContactInfo;")
            If Not rst.EOF Then
                rst.MoveFirst
                intPID = rst![NewPID]
            End If
        Case 2
            Set rst = CurrentDb.OpenRecordset("SELECT IIf(Max([tblTempContactInfo].[ID])+1=1,'2000001',Max([tblTempContactInfo].[ID])+1) AS [NewPID] FROM tblTempContactInfo;")
            If Not rst.EOF Then
                rst.MoveFirst
                intPID = rst![NewPID]
            End If
    End Select
    
    Me.txtPID.Value = intPID
End Sub

Open in new window

0
Comment
Question by:AIdoHSG
  • 3
  • 2
6 Comments
 
LVL 27

Expert Comment

by:MikeToole
ID: 21803312
Why not set the ID column your tables to automatically increment?
Use the IDENTITY keyword in SQL Server.
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 21803316
try this


Private Sub cmdGetNewID_Click()
'Get new ID for respondent
 
    Dim intPID As Integer
    Dim db As Database
    Dim rst As Recordset
   
    Set db = CurrentDb()
   
    'check which contact type it will be
    'If radio button 1 is selected then Phys table (Permanent)
    'If radio button 2 is selected then Temp table (Temprary)
    Select Case Me.frmContactType.Value
        Case 1
'            Set rst = CurrentDb.OpenRecordset("SELECT Max(tblPhysContactInfo.ID)+1 AS [NewPID] FROM tblPhysContactInfo;")
'            If Not rst.EOF Then
'                rst.MoveFirst
'                intPID = rst![NewPID]
'            End If

            intPID = Nz(DMax("ID", "tblPhysContactInfo"), 0) + 1

        Case 2
'            Set rst = CurrentDb.OpenRecordset("SELECT IIf(Max([tblTempContactInfo].[ID])+1=1,'2000001',Max([tblTempContactInfo].[ID])+1) AS [NewPID] FROM tblTempContactInfo;")
'            If Not rst.EOF Then
'                rst.MoveFirst
'                intPID = rst![NewPID]
'            End If
   
            intPID = Nz(DMax("ID", "tblTempContactInfo"), 0) + 1
   
   
    End Select
   
    Me.txtPID.Value = intPID
End Sub


0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 21803331
that is, if your db application is a single user, for multiuser you will used @@identity
0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 

Author Comment

by:AIdoHSG
ID: 21803349
Thanks capricorn1, but when I clicked the button it gave me a Run-time error '6': Overflow
and it takes me to line 21 or 31 based on my selection.
Any ideas?
0
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 500 total points
ID: 21803373
change this

       Dim intPID As Integer

to

     Dim intPID As Long
0
 

Author Closing Comment

by:AIdoHSG
ID: 31467967
Thanks capricorn1, this is excatly what I was looking.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Question about Relationship and Userform in Access Database 17 54
Calculation in Access 5 26
ServiceCenter IR Query Expressions 1 23
Trying to force an answer in a combobox 7 18
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question