Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

VBA function to create a sequence number

Posted on 2004-04-07
3
Medium Priority
?
523 Views
Last Modified: 2008-03-17
hi, i am a beginner, so i think this would be easy for you guys.

I have this function in my module to create a sequence number. I called this function in my update query. I hope that this function will retirn a sequence but instead in retuns the same number for every record. This is my function.

Public currNo As Integer


Function addNo() As Integer

  currNo = currNo + 1
 
  addNo = currNo
 
End Function

* The reason I use currNo as public variable is to make sure that it will keep in the memory what is the current value. Maybe my method is wrong.

One more thing, I put this function in the query design, I assume that it will run this function row by row. Am I wrong about this?

this is my sql statement

UPDATE ACCOUNT_ACTIVITY SET ACCOUNT_ACTIVITY.ACC_SEQ = addNo()
WHERE (((ACCOUNT_ACTIVITY.ACC_ID)="1") AND ((ACCOUNT_ACTIVITY.ACC_DESC)="CLEARING CHQ"));


Really need help on this.
0
Comment
Question by:koCheE
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 77

Accepted Solution

by:
peter57r earned 750 total points
ID: 10772809
Hello koCheE,

Change the function so that it accepts a parameter.  Then use any field from the data record as the argument.
This will fool Access into thinking the value must calculated for each record.

Function addNo(vParam) As Integer

  currNo = currNo + 1
 
  addNo = currNo
 
End Function

UPDATE ACCOUNT_ACTIVITY SET ACCOUNT_ACTIVITY.ACC_SEQ = addNo(Acc_seq)
WHERE (((ACCOUNT_ACTIVITY.ACC_ID)="1") AND ((ACCOUNT_ACTIVITY.ACC_DESC)="CLEARING CHQ"));

Pete
0
 
LVL 54

Expert Comment

by:nico5038
ID: 10772887
You can also achieve this without your own function as Access already has a DCOUNT function you can use like:

UPDATE ACCOUNT_ACTIVITY SET ACCOUNT_ACTIVITY.ACC_SEQ = DCOUNT("ACC_ID","ACCOUNT_ACTIVITY","ACC_ID='1' AND ACC_DESC='CLEARING CHQ' AND UniqueAccountID<=" & UniqueAccountID);

This assumes you have a unique (e.g. autonum) keyfield on the ACCOUNT_ACTIVITY table.

It can even be used to do this for all ACC_DESC values by changing it into:

UPDATE ACCOUNT_ACTIVITY SET ACCOUNT_ACTIVITY.ACC_SEQ = DCOUNT("ACC_ID","ACCOUNT_ACTIVITY","ACC_ID='1' AND ACC_DESC='" & [ACC_DESC] & "' AND UniqueAccountID<=" & UniqueAccountID);

Clear ?

Nic;o)
0
 

Author Comment

by:koCheE
ID: 10773119
thanks pete, you have save me a lot of time.
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
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.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

598 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