I have a field named ID, now I want this:
I can enter any ID or If I don't enter ID on it, it creates an ID automatically according to (MAX ID + 1: example: the MAX ID is 400 now ID gets 401)
I have a field named ID, now I want this:
I can enter any ID or If I don't enter ID on it, it creates an ID automatically according to (MAX ID + 1: example: the MAX ID is 400 now ID gets 401)
Since you want to occasionally control it, you can't use autonumber. You can google for "Custom Autonumber" or "Custom Counter" to get various solutions. Here's three useful pages to get you started.
http://support.microsoft.com/kb/210194
http://msgroups.net/microsoft.public...onumber/133479
http://www.office-archive.com/3-ms-a...a062aaa733.htm
Hopefully, this should be enough info for you to mark the thread solved.
I solve it with this code:
Code:Dim srchID, createID As String srchID = DLookup("masaleh_ID", "blook_masaleh_tbl") If (srchID) Then createID = DMax("masaleh_ID", "blook_masaleh_tbl") Me.IDtxt.Value = createID + 1 Else Me.IDtxt.Value = 1 End If
The code doesn't look right. You don't have a where condition on your Dlookup, so that command is checking for ANY record in that table, in no particular order.
Here's a page that explains what Dlookup does - http://www.techonthenet.com/access/f...in/dlookup.php
What that particular code will do is to bring back the first value for Masaleh_ID that exists in the table, which is stored/returned by Access in random order, and put that value into srchID. If there are NO values for Masaleh_ID, it will put a NULL in that spot.
Next, if srchID is NULL, "If (SrchID)" most likely will throw an error, so the else condition that sets Me.IDtxt.value to 1 will never be reached.
Before I post any suggestions, let me ask you how you expect the calling form to operate. Is the operator putting the ID he wants into IDtxt? When/how does a new ID get added to blook_masaleh_tbl? How does the calling form know that the form is supposed to default to a newly created ID?