Solved

Access increase by 1

Posted on 2009-04-05
11
558 Views
Last Modified: 2013-11-28
I have not found an exact answer in the exchange for my question, so I thought I would ask.  I have an access table which uses BATCH_NUM as the primary key.  We are now doing multiple tests on each batch in our lab.  I will add a value called LINE_NUM to this table.  I want LINE_NUM to start at 1 and then increment 1 for each additonal test on the batch.  So, in my form, lets say batch 1111 has it's first test and by default value property of LINE_NUM, LINE_NUM on the form shows 1.  Now, the next day I am going to run another test on batch 1111 and I want LINE_NUM to be a value of 2 for this test.  Can I use an expression or VB on the form to do this?  Something like select BATCH_NUM where BATCH_NUM = txtBATCH_NUM and then find the MAX of LINE_NUM and increment by 1?  Autonumber would work, but it would just keep increasing the number for all records, not by batch number.
0
Comment
Question by:etechit
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 2

Expert Comment

by:pskeens
ID: 24074178

On event

Dim Lnum as Integer
Dim bnum as integer
Dim formbNum as integer

lnum = "SELECT max(LINE_NUM) FROM yourTable Where BATCH_NUM = formbNum"
bnum = Me.txtBATCH_NUM

txtLINE_NUM = bnum +1
0
 
LVL 30

Accepted Solution

by:
hnasr earned 500 total points
ID: 24074393
NextNumber = DMax("Field1", "Table1", "ID='value'") + 1
0
 
LVL 57
ID: 24076319
Just be aware that if multiple users can be working within a batch at the same time that Max/DMax may possibly hand out the same number if two users got to save at the same time.
If you have this situation, you'll need a key generation routine which has some type of lock built in so that only one user can be grabbing a key at one time.
JimD.
0
 
LVL 11

Expert Comment

by:mildurait
ID: 24076913
You could  try something like this.

public sub txtBatchNo_AfterUpdate()

       If isnull(me.txtBatch_Num)=true then exit sub
       If isnull(me.txtLine_Num)=true then exit sub
       dim lastline as long
       dim sql as string
       sql = "SELECT TOP 1 LINE_NUM"
       sql = sql & " FROM TABLE"
       sql = sql & " WHERE BATCH_NUM=" & me.txtBatch_Num
       sql = sql & " AND LINE_NUM IS NOT NULL"
       sql = sql & " ORDER BY LINE_NUM DESC"
       dim rs as dao.recordset      
       set rs = CurrentDb.openrecordset(sql)
       if not rs.eof = false then lastline = rs.fields("LINE_NUM").value else lastline = 0
       rs.close
       set rs = nothing
       me.txtLine_Num = lastline+1
   
End sub

Note you will have to change TABLE to the actual name of your table.
0
 
LVL 11

Expert Comment

by:mildurait
ID: 24076923
...correction to above.
public sub txtBatch_Num_AfterUpdate()
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 1

Author Comment

by:etechit
ID: 24088848
mildurait;

Your example errors out on
Set rs = CurrentDb.OpenRecordset(sql)
0
 
LVL 1

Author Comment

by:etechit
ID: 24088871
hnasr;

Your example doesn't work as it has no idea of what the table name is.  I did insert my correct table name, but in troubleshooting my table name; tblLOG, says empty.
0
 
LVL 1

Author Comment

by:etechit
ID: 24088910
pskeens;

I get a type mismatch error on the first line
Lnum = "SELECT max(LOG_LINE_NUM) FROM tblLOG Where BATCH_NUM = formbNum"
0
 
LVL 30

Expert Comment

by:hnasr
ID: 24092630
Try to post a sample db!
0
 
LVL 11

Expert Comment

by:mildurait
ID: 24092758
@etechit      
Errors in all solutions here would point to the BATCH_NUM being of a non-numeric data type.
0
 
LVL 1

Author Comment

by:etechit
ID: 24093453
hnasr;
Your answer was correct, I was putting it in a Sub and not a Function.  It works!
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now