0

Can anyone help me here? I am trying to do a user defined autonumber when I click on a button. The code worked fine when the recordset is empty, it give me a 1 on my first record, however, after I click on the button again, it give me 1 again. Here's my code.

Private Sub BtnNew_Click()
    Dim rsClone As Recordset
    Dim pVal As Integer

    Set rsClone = Me.RecordsetClone

    If Not (rsClone.BOF) Then
        DoCmd.GoToRecord , , acNewRec
        rsClone.MoveLast
        pVal = rsClone.AbsolutePosition + 2
        Me.CatgId.Value = pVal
        Me.CatgId.SetFocus
    Else
        rsClone.AddNew
        Me.CatgId.Value = 1
        Me.CatgId.SetFocus
    End If
End Sub

Thanks for the help.

Jeff Ogata
  • 56,645
  • 19
  • 114
  • 127
  • Don't use recordsets in .Net. They exist for backwards compatibility to old code. Move on to datareaders/datasets. Also, the `Set` keyword doesn't mean the same thing as it did in asp classic anymore and you should probably avoid that as well. – Joel Coehoorn Nov 30 '10 at 05:09

1 Answers1

0

I'm guessing that this is for a web page. If that's the case, every time your page handles any event, you're working with an entirely new instance of the page's type. That is, you're "Me" reference in that code points to a different object every time the function is called.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • Sorry I forgot to mention this code is applying to Microsoft Access 2007. In addition, if I reopen my form and click on the button again, it gave me the next number. It doesn't work only when the recordset is emptied. – Clifford Dec 01 '10 at 01:03