Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Access increase by 1

Posted on 2009-04-05
11
Medium Priority
?
571 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 31

Accepted Solution

by:
hnasr earned 2000 total points
ID: 24074393
NextNumber = DMax("Field1", "Table1", "ID='value'") + 1
0
 
LVL 58
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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
 
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 31

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

916 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