I've read a lot of articles and forums trying to figure this one and have yet to come up with a solution I can get to work. I need to create a unique id. I can do this using Dmax to find the largest value and add 1. The problem is that I need the unique ID to be based on the value of two other fields. So, in table of hundreds of records I want to look at only certain values to obtain the next available number. For example, if I have a table of orders and want the order number to be unique based off the client name and the product type.
So if Dmax is stated like this:
DMax ( expression, domain, [criteria]
and I have multiple criteria, how does that work??
I have a form where I would first have them select the client and then the product and then want the next available order number to pop up.
27 10156
"Criteria1 AND Criteria2"
-
Private Sub Form_BeforeUpdate()
-
Dim sOrderID As String
-
Dim iOrderSeq As Integer
-
Dim sCustName As String
-
Dim sProdType As String
-
-
sCustName = Me!CustomerName.Value
-
sProdType = Me! [ProductType] .Value
-
-
If Not IsNull(sCustomerName) And Not IsNull(sProdType) Then
-
sOrderSeq = Dmax(Val([OrderID], qryOrders,[CustomerName] = & sCustName & And [ProductType] = & sProdType & )) + 1
-
-
sOrderID = sCustName & sProdType & Format(iOrderSeq, 0000)
-
Else
-
Cant generate the OrderID
.Customer Name or Product Type are missing
-
End If
-
End Sub
-
- [CustomerName] = & sCustName & And [ProductType] = & sProdType &
Where do I place the code? I notice you have it on the BeforeUpdate of the form, but there needs to be multiple values selected first and they have not had the chance to choose the customer or product yet??
Where do I place the code? I notice you have it on the BeforeUpdate of the form, but there needs to be multiple values selected first and they have not had the chance to choose the customer or product yet??
The Form_BeforeUpdate is correct. This event runs just before the changed data on the form has been saved to the database. This is where validation of the changes is usually done...and it can be cancelled if needed.
[CustomerName] = & sCustName & AND [ProductType] = & sProdType &
I can not get this to work with two criteria... even if I use specific values for sCustName and sProdType...
[CustomerName] = & sCustName & AND [ProductType] = & sProdType &
I can not get this to work with two criteria... even if I use specific values for sCustName and sProdType...
That is Rabbit's suggested correction to my code. Did you correctly blend it in to my code, or are you testing that code line by itself? Please post your updated code and describe the error you are getting.
Alright, I got it to work using a simple multiple criteria inside a query. Now when I try to transfer this concept to my form where I actually have 3 criteria, it fails. Here is the code.
Private Sub Form_BeforeUpdate(Cancel As Integer)
If Not IsNull(Forms![line test]!PipeSpec) And Not IsNull(Forms![line test]!FluidCode) And Not IsNull(Forms![line test]!SystemNumber) Then
[txtseqno] = DMax("[seq no]", "Line_List", "[Pipe spec] = " & Forms![line test]!PipeSpec & " And [fluid code] = " & Forms![line test]!FluidCode & " And [system number] = " & Forms![line test]!SystemNumber & "") + 1
Else
MsgBox "Can not generate Sequence Number, the Fluid Code, System Number or Pipe Spec are missing"
End If
End Sub
When I use the form, it gives me a 0 value for the next sequence number as soon as I select the first one of the three variables. Then when I go to leave the record I an error "Run-time error 2001, you canceled the previous operation"
When I got out and got back in, this time the error was different. Run-time error 3085, undefined function 'A' in expression.
?????
When I got out and got back in, this time the error was different. Run-time error 3085, undefined function 'A' in expression.
?????
OK. Before I look over the code, can you give me an example of what the data for each criteria looks like so that I can get a visual picture of what the final OrderID should look like? Thanks.
That is Rabbit's suggested correction to my code. Did you correctly blend it in to my code, or are you testing that code line by itself? Please post your updated code and describe the error you are getting.
Yeah, sorry, I should have made that clear.
yeah, just a 3 digit number is all. I'll concatenate at times for reports and things to create a longer number, but this field is just a 3 digit number.
Alright, I got it to work using a simple multiple criteria inside a query. Now when I try to transfer this concept to my form where I actually have 3 criteria, it fails.
yeah, just a 3 digit number is all. I'll concatenate at times for reports and things to create a longer number, but this field is just a 3 digit number.
[/quote] If the OrderID is going to be a 3 digit number, try the syntax below. Keep in mind that the tab order for the 3 criteria must precede the txtseqno in order for it to work. -
Private Sub Form_BeforeUpdate(Cancel As Integer)
-
-
If Not IsNull(Forms![line test]!PipeSpec) And Not IsNull(Forms![line test]!FluidCode) And Not IsNull(Forms![line test]!SystemNumber) Then
-
-
-
[txtseqno] = DMax("[seq no]", "Line_List", "[Pipe spec] = " & Forms![line test]!PipeSpec & " And [fluid code] = " & Forms![line test]!FluidCode & " And [system number] = " & Forms![line test]!SystemNumber) + 1
-
Else
-
-
MsgBox "Can not generate Sequence Number, the Fluid Code, System Number or Pipe Spec are missing"
-
-
-
End If
-
-
-
End Sub
-
Undefined function 'A' in expression???? No number generated.
Is this some reference file that I need to load? Just a thought...
Undefined function 'A' in expression???? No number generated.
Did you make sure the tab order of the 3 criteria fields preceded the txtseqno?
Can you tell me which part of the code was highlighted? Can you tell what is showing for the value of each variable?
This is the line that is highlighting.
[txtseqno] = DMax("[seq no]", "Line_List", "[Pipe spec] = " & Forms![line test]!PipeSpec & " And [fluid code] = " & Forms![line test]!FluidCode & " And [system number] = " & Forms![line test]!SystemNumber) + 1
It is giving the correct value from the form for each field.
This is the line that is highlighting.
[txtseqno] = DMax("[seq no]", "Line_List", "[Pipe spec] = " & Forms![line test]!PipeSpec & " And [fluid code] = " & Forms![line test]!FluidCode & " And [system number] = " & Forms![line test]!SystemNumber) + 1
It is giving the correct value from the form for each field.
Try this code and tell me what happens. -
[txtseqno] = nz(DMax("[seq no]", "Line_List", "[Pipe spec] = " & Forms![line test]!PipeSpec & " And [fluid code] = " & Forms![line test]!FluidCode & " And [system number] = " & Forms![line test]!SystemNumber) + 1, 0)
-
-
MsgBox "seq no " & [seq no]
-
MsgBox "Pipe spec " & [Pipe Spec]
-
MsgBox "Fluid Code " & [fluid code]
-
MsgBox "System Number " & [system number]
-
It keeps telling me I've canceled the previous operation and won't go on.
I've gotten past that and I'm getting the unknown function 'A' again. I've tried repacing the three variables with a predefined string, but it's not reading it correctly. It's giving me the next available number for the entire database and not for the criteria.
Dim Pspec As String
Dim Fcode As String
Dim Synum As String
Dim STR As String
Pspec = Me![Pipe Spec].Value
Fcode = Me![fluid code].Value
Synum = Me![system number].Value
If Not IsNull(Forms![line test]!PipeSpec) And Not IsNull(Forms![line test]!FluidCode) And Not IsNull(Forms![line test]!SystemNumber) Then
STR = ([Pipe Spec] = Pspec And ([fluid code] = Fcode And ([system number] = Synum)))
[txtseqno] = DMax("[seq no]", "Line_List", STR) + 1
MsgBox "seq no " & [seq no]
MsgBox "Pipe spec " & [Pipe Spec]
MsgBox "Fluid Code " & [fluid code]
MsgBox "System Number " & [system number]
Else
MsgBox "Can not generate Sequence Number, the Fluid Code, System Number or Pipe Spec are missing"
End If
OK, try this. Please limit your changes to syntax corrections. Dont rework my code (it gets confusing when two people are making code changes to the same code).
..just tell me what happened (in detail) when you ran the code. -
Private Sub Form_BeforeUpdate()
-
Dim Pspec As String
-
Dim Fcode As String
-
Dim Synum As Integer
-
Dim STR As String
-
-
-
If Not IsNull(Forms![line test]!PipeSpec) And Not IsNull(Forms![line test]!FluidCode) And Not IsNull(Forms![line test]!SystemNumber) Then
-
Pspec = Me![Pipe Spec].Value
-
Fcode = Me![fluid code].Value
-
Synum = Me![system number].Value
-
-
STR = Pspec & Fcode
-
-
am assuming that [seq no] is the control source of txtseqno
-
[txtseqno] = STR & DMax("[seq no]", "Line_List", [Pipe Spec] = & Pspec & & And [fluid code] = & Fcode & ) + 1
-
-
-
-
MsgBox "seq no " & [txtseqno]
-
MsgBox "Pipe spec " & [Pipe Spec]
-
MsgBox "Fluid Code " & [fluid code]
-
MsgBox "SystemNumber " & [Synum]
-
MsgBox "STR " & STR
-
-
-
Else
-
-
MsgBox "Can not generate Sequence Number, the Fluid Code, System Number or Pipe Spec are missing"
-
-
-
End If
-
End Sub
-
Finally got it to work right. I think one of my problems is that System Number is a number format, so no (')s go around the string.
Thanks for the help.
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim Pspec As String
Dim Fcode As String
Dim Synum As Integer
Dim STR As String
If Not IsNull(Forms![line test]!PipeSpec) And Not IsNull(Forms![line test]!FluidCode) And Not IsNull(Forms![line test]!SystemNumber) Then
Pspec = Me![pipe spec].Value
Fcode = Me![fluid code].Value
Synum = Me![System number].Value
[seq no] = DMax("[seq no]", "Line_List", "[pipe spec] = '" & Pspec & "'" & " And [fluid code] = '" & Fcode & "'" & " And [system number] = " & Synum & "") + 1
Else
MsgBox "Can not generate Sequence Number, the Fluid Code, System Number or Pipe Spec are missing"
End If
End Sub
You are welcome. Glad I could help.
Just a follow up for anyone who refers to this at a later date. I moved the code to the AfterInsert update because on BeforeUpdate it would re-calculate the number with any change to any field in the record and I only want a number calculated when a new record is added. I also added an If statement to handle if the search returned no current value, so that the first record would get a value = 1.
Private Sub Form_AfterInsert()
Dim Pspec As String
Dim Fcode As String
Dim Synum As Integer
Dim STR As String
Dim Seq As Integer
If Not IsNull(Forms![line test]!PipeSpec) And Not IsNull(Forms![line test]!FluidCode) And Not IsNull(Forms![line test]!SystemNumber) Then
Pspec = Me![pipe spec].Value
Fcode = Me![fluid code].Value
Synum = Me![System number].Value
[seq no] = DMax("[seq no]", "Line_List", "[pipe spec] = '" & Pspec & "'" & " And [fluid code] = '" & Fcode & "'" & " And [system number] = " & Synum & "") + 1
If IsNull([seq no]) Then
[seq no] = 1
Else
End If
MsgBox "Sequence No. =" & [seq no]
Else
MsgBox "Can not generate Sequence Number, the Fluid Code, System Number or Pipe Spec are missing"
End If
End Sub
Thank you for taking the time to document your final solution for the benefit of others.
yeah, just a 3 digit number is all. I'll concatenate at times for reports and things to create a longer number, but this field is just a 3 digit number.
You can't actually have this as a 3 digit number because
you are concatenating other fields which would produce more than 3 digits.
This is how i picture what youre saying
field1 -- let's say the largest number now 13
field2 -- 220
field3 -- 220
if you dmax the field 1 of whichever table it belongs and then attach codes like
& [field2] & [field3]) + 1
result:
field1 -- 13220221
if you want the result to be 3 digits, use "+" instead of "&"
result:
field 1 -- 454 ((13+220+220)+1)
Hi All and thanks for all participants,
i have search in several websites and articals i dont find a solution for this subject, in this case all the three fields is text data type, i have match case but I would generate text transaction unique ID consist of three parts "textYYYYNNNN", where
text = 4 letter from "Dept" Field, (text datatype)
YYYY = year from "Date" Field, (date datatype)
NNNN = sequence from "Seq" Field, (numeric datatype)
So I have three conditions to generate transaction ID that:
1st condition is
SELECT Transmittal.TransmittalID, Transmittal.TransmittalTitle, Transmittal.Dept FROM Transmittal WHERE (((Transmittal.Dept)= "Geology"));
And from previous result I want to set the date I form and it should save to table then I want to apply 2nd condition
2nd condition is
SELECT Transmittal.TransmittalID, Transmittal.TransmittalTitle, Transmittal.TransmittalDate FROM Transmittal
WHERE (((Transmittal.TransmittalDate)=max)); >>>>>>> its mean current day, the reason of that may I have more then on transmittal at one day.
And from previous result I want apply the 3rd condition
3rd condition is
SELECT Transmittal.TransmittalID, Transmittal.TransmittalTitle, Max(Transmittal.Sequence) AS MaxOfSequence FROM Transmittal GROUP BY Transmittal.TransmittalID, Transmittal.TransmittalTitle;
Then I want (4 letter from Dept)&(year from date)&(max seq + 1))
Example: GEOL20080252.
Regads
abdulaziz
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Nick |
last post by:
Is it possible to have multiple auto increments in one column? Say I
have two tables...
1. table 'messageboards' with fields ('id' , 'name')
2. table 'messagethreads' with fields ('id' ,...
|
by: csomberg |
last post by:
SQL 2000
I thought I would throw this out there for some feedback from others.
I'd like to know if you feel using MS auto-increment field is a good
solution these days or should one grow their...
|
by: Tom |
last post by:
I am trying to store information into a table that has an auto
increment field. There is currently no data in the table. Using the
code below I cannot insert data into the table. I get an error...
|
by: Alpha |
last post by:
I retrieve a table with only 2 columns. One is a auto-generated primary key
column and the 2nd is a string. When I add a new row to the dataset to be
updated back to the database. What should I...
|
by: Chris |
last post by:
Before I started to create table, etc to track unique form field record
number assigments I thought I'd check to see if there is now a better way to
do this in .NET.
I have a parent form (table)...
|
by: Pauloviθ Michal |
last post by:
hi all,
I have problem with SERIAL field type (or sequence functionality).
I have table with three columns - ID, IDS, NAME.
I want auto-increment IDS grouped by ID.
Example:
1, 1, Ferdo
1, 2,...
|
by: S.Dickson |
last post by:
I had an access database that i use as an ordering system. I have a
form for entering customer details. When i add a new customer on the
form the customer number is an auto number that appears when...
|
by: chumlyumly |
last post by:
Hello scripters -
OS: Mac OSX
Language: PHP w/ MySQL database
I've created an insert page where a user inputs his info, which then goes to four different tables in a MySQL database. The...
|
by: badvoc |
last post by:
Hi,
I have had some good fortune on this site so I am back and I must iterate I am a beginer.
I am having some problems getting to grips with the right technique to manage variables and...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |