Hello,
Access 2007
In my input table I have a field called RecordIDNo. I have another field called communityID. I am trying to generate a number in the RecordIDNo field in relation to the communityID. For example there are communityID 0001 and communityID 0002 I want the database to keep track of how many records that are associated with each communityID number. In the RecordIDNo field I want the record number populated in that field when I am adding a record. Instead I get the number zero in the RecordIDNo field.
Any help would be greatly appreciated. Thank you.
This is the code I used.
Private Sub Form_BeforeInsert(Cancel As Integer)
'Generate the record id number for a the community
On Error Resume Next
Dim rs As DAO.Recordset
Dim strSQL As String
Dim lngID As Long
strSQL = "SELECT RecordIDNo FROM DataForm " & _
"WHERE CommunityID = '" & Forms!DataForm!Community & "'"
Set rstOriginal = db.OpenRecordset(strSQL, dbOpenSnapshot)
If (rs.RecordCount > 0) Then
rs.MoveLast
lngID = rs.RecordCount + 1
Else
lngID = 1
End If
rs.Close
Me.RecordIDNo = lngID
End Sub
Public Function NextID() As Long
On Error Resume Next
'Generate the record id number for a the community
Dim rs As DAO.Recordset
Dim strSQL As String
Dim lngID As Long
lngID = 1
strSQL = "SELECT RecordIDNo FROM DataForm WHERE CommunityID = '" & Forms!DataForm!Community & "'"
Set rs = db.OpenRecordset(strSQL, dbOpenSnapshot)
If (rs.RecordCount > 0) Then
rs.MoveLast
lngID = rs.RecordCount + 1
End If
rs.Close
NextID = lngID
End Function
Private Sub Form_BeforeUpdate(Cancel As Integer)
On Error Resume Next
If (IsNull(Me.RecordIDNo)) Then
Me.RecordIDNo = NextID()
End If
End Sub
Private Sub Form_Load()
On Error Resume Next
Set db = CurrentDb
boCopy = False
End Sub
Access 2007
In my input table I have a field called RecordIDNo. I have another field called communityID. I am trying to generate a number in the RecordIDNo field in relation to the communityID. For example there are communityID 0001 and communityID 0002 I want the database to keep track of how many records that are associated with each communityID number. In the RecordIDNo field I want the record number populated in that field when I am adding a record. Instead I get the number zero in the RecordIDNo field.
Any help would be greatly appreciated. Thank you.
This is the code I used.
Private Sub Form_BeforeInsert(Cancel As Integer)
'Generate the record id number for a the community
On Error Resume Next
Dim rs As DAO.Recordset
Dim strSQL As String
Dim lngID As Long
strSQL = "SELECT RecordIDNo FROM DataForm " & _
"WHERE CommunityID = '" & Forms!DataForm!Community & "'"
Set rstOriginal = db.OpenRecordset(strSQL, dbOpenSnapshot)
If (rs.RecordCount > 0) Then
rs.MoveLast
lngID = rs.RecordCount + 1
Else
lngID = 1
End If
rs.Close
Me.RecordIDNo = lngID
End Sub
Public Function NextID() As Long
On Error Resume Next
'Generate the record id number for a the community
Dim rs As DAO.Recordset
Dim strSQL As String
Dim lngID As Long
lngID = 1
strSQL = "SELECT RecordIDNo FROM DataForm WHERE CommunityID = '" & Forms!DataForm!Community & "'"
Set rs = db.OpenRecordset(strSQL, dbOpenSnapshot)
If (rs.RecordCount > 0) Then
rs.MoveLast
lngID = rs.RecordCount + 1
End If
rs.Close
NextID = lngID
End Function
Private Sub Form_BeforeUpdate(Cancel As Integer)
On Error Resume Next
If (IsNull(Me.RecordIDNo)) Then
Me.RecordIDNo = NextID()
End If
End Sub
Private Sub Form_Load()
On Error Resume Next
Set db = CurrentDb
boCopy = False
End Sub