Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Assigning a number

Status
Not open for further replies.

scra

Technical User
Dec 7, 2001
55
0
0
US
I have a command button name "New Issue" on a form. When I click on that command button, I get a clean form to enter new data. Within that form, there is a field named Issue Number. I would like to assign that field an automatic fill-in number (i.e. last issue number is 35..would like 36 to be the next number and so on) when the "new issue" button is clicked. The form is populating a table. I can't change the issue number field to an autonumber data type because there is already a primary key and have been attached as a relationship to other queries and tables.

Do you have a code to share to populate and auto number?
 
you could simply look up the last entry and add 1 to it

Something like

Private Sub Form_Open(Cancel As Integer)
Dim rs As DAO.Recordset
Dim db As DAO.Database

On Error GoTo ErrHandle

Set db = CurrentDb()
Set rs = db.OpenRecordset("NameofYourTable")

rs.MoveFirst
rs.MoveLast

Me.FormFieldName.Value = rs!TableFieldName + 1

ErrHandle:
'Handle a table with no records
If Err.Number = 3021 Then
Me.FormFieldName.Value = 1
Else
'Do something else
End If

End Sub

HTH << MaZeWorX >> "I have not failed I have only found ten thousand ways that don't work
 
Thanks, Maze. Now, the other question is...where do I place the code you provided?
 
scra,

Place the code in the form load event.... that way when you open a new blank form it will populate with your new issue number.

hope that helps

Max
 
You might want to be carefull if this is a multiuser application. This solution might create duplicate issue numbers.

There are other methods documented on the web and possibly the FAQs here for generating sequential numbers.

Duane
Hook'D on Access
MS Access MVP
 
Max, I'm getting this compile error which hi-lights the IssueAssignedNum.

Private Sub Form_Open(Cancel As Integer)
Dim rs As DAO.Recordset
Dim db As DAO.Database

On Error GoTo ErrHandle

Set db = CurrentDb()
Set rs = db.OpenRecordset("Issues")

rs.MoveFirst
rs.MoveLast

Me.FormFieldName.Value = rs!IssueAssignedNum + 1

ErrHandle:
'Handle a table with no records
If Err.Number = 3021 Then
Me.FormFieldName.Value = 1
Else
'Do something else
End If

End Sub
 
You might want to review the small sample database on Rogers Access Library.
page said:
This sample illustrates a number of ways to simulate your own Autonumber primary key.
Form1 illustrates how to use the DMax function to simulate the action of the Autonumber field.

Form2 does much the same as the Products form, but allows for multi-user collisions.



Duane
Hook'D on Access
MS Access MVP
 
I'm still getting a compile error on this...

Me.FormFieldName.Value = rs!IssueAssignedNum + 1

Please help.
 
How are ya scra . . .

Typically [blue]DMax[/blue] is used for something like this:
Code:
[blue]   Me!TextboxName = DMax("[IssueAssignedNum]", "Issues") + 1[/blue]
Note: if there's any sorting in the table then the last record is not necessarily the most currently added. [blue]DMax[/blue] is independent of sorting.

[blue]Your Thoughts? . . .[/blue]

See Ya! . . . . . .

Be sure to see thread181-473997 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
Scra.....

You will always get that message if you use
Me.FormFieldName.Value = rs!IssueAssignedNum + 1

you need to replace FormFieldname with your actual name of the field so for example if your field on your form is named ISSUENUMBERFIELD then

Me.FormFieldName.Value = rs!IssueAssignedNum + 1

will become

Me.ISSUENUMBERFIELD.Value = rs!IssueAssignedNum + 1

hope this helps

M
 
Thanks so much who contributed to this post. You Rock!
 
It's me again! What is the correct code to have the form go to a blank form? Currently the form is loaded showing previous records into the form.

Private Sub Command44_Click()
Dim rs As DAO.Recordset
Dim db As DAO.Database

On Error GoTo ErrHandle

Set db = CurrentDb()
Set rs = db.OpenRecordset("Issues")

rs.MoveFirst
rs.MoveLast

Me.IssueAssignedNum.Value = rs!IssueAssignedNum + 1

ErrHandle:
'Handle a table with no records
If Err.Number = 3021 Then
Me.IssueAssignedNum.Value = 1
Else
'Do something else
End If

End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top