Solved

VBA function to create a sequence number

Posted on 2004-04-07
3
503 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
3 Comments
 
LVL 77

Accepted Solution

by:
peter57r earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
thanks pete, you have save me a lot of time.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
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.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

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

16 Experts available now in Live!

Get 1:1 Help Now