Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Incrimenting Numbers

Status
Not open for further replies.

STaL1N

Technical User
May 3, 2004
11
GB
pseudocode for the code i want like is as follows, i am just having problems applying the correct syntax.

Code:
 IF NewlyaddedCompanyName = unique then
          InvoiceNumber = 1
       End If
       IF NewlyAddedCompanyName = has appeared x times then
          InvoiceNumber = x

this code is an attempt to create an incrimenting invoice number for each company ie when a company appears multiple times in a job query it is assigned the appropriate invoice number
 
I think that you really want to increment the last invoice number that was used for a company.

Code:
dim varLastInvoiceNum as variant

varLastInvoiceNumber = DMAX("CompanyNameField", "TableName", "CompanyNameField = '" & NewlyAddedCompanyName & "'")

if isnull(varLastInvoiceNum) then
    InvoiceNumber = 1
else
    InvoiceNumber = varLastInvoiceNum + 1
endif
 
and would i put this code in a module or..?
 
Place this code in a module. Note correction in DMAX parameters.


Code:
Function NextInvoiceNumber(NewlyAddedCompanyName as string) as integer

    dim varLastInvoiceNum as variant
    dim InvoiceNumber as integer

    varLastInvoiceNumber = DMAX("InvoiceNumberField", "TableName", "CompanyNameField = '" & NewlyAddedCompanyName & "'")

    if isnull(varLastInvoiceNum) then
        InvoiceNumber = 1
    else
        InvoiceNumber = varLastInvoiceNum + 1
    endif

    NextInvoiceNumber = InvoiceNumber
end function
 
Code:
= '" & NewlyAddedCompanyName & "'")

was wondering do i need to amending this to my database?
am thinking i dont need to touch this part of the code.
 
also would i have to call this function and if so where would i do it?
 
You can call the function from a query, from the control source of a form, from another module, etc.

From a query

SELECT CompanyName, NextInvoiceNumber(CompanyName) as NewInvoiceNum FROM myTable

 
i created a form with a new text box and built an expression but the value returned was "#Name?"

also i did not know how to incorporate it into an existing query.

Code:
SELECT [tblJobs].[JobID], [tblCompany].[CompanyName], [tblCars].[Registration], [tblTyres].[Tyre Type], [tblTyres].[ManafacturersCode], [tblJobs].[NoOfTyres], [tblTyres].[Price], [tblJobs].[Comment], ([NoOfTyres]*[Price])*1.175*[DiscountValue]+50 AS Total, [tblCars].[CompanyID], [tblJobs].[CarID], [tblJobs].[TyreID], [tblJobs].[Discount], [tblDiscounts].[DiscountName], [tblDiscounts].[DiscountValue], [tblJobs].[Job Date], [tblJobs].[Mileage], [tblTyres].[Amount In Stock], [tblCompany].[InvoiceNumber]
FROM tblTyres INNER JOIN (tblDiscounts INNER JOIN (tblCompany INNER JOIN (tblCars INNER JOIN tblJobs ON [tblCars].[CarID]=[tblJobs].[CarID]) ON [tblCompany].[CompanyID]=[tblCars].[CompanyID]) ON [tblDiscounts].[DiscountID]=[tblJobs].[Discount]) ON [tblTyres].[TyreID]=[tblJobs].[TyreID];

where my relationships are setup was follows
 
also in the original code
Code:
IF NewlyaddedCompanyName = unique then
          InvoiceNumber = 1
       End If
       IF NewlyAddedCompanyName = has appeared x times then
          InvoiceNumber = x

NewlyAddedComanyName is pseudocode i should have rephrased to

Code:
IfTheCompanyIJustAddedToMyQuery = NotAlreadyThere then
InvoiceNumber = 1
       End If
       IF NewlyAddedCompanyName = has appeared x times then
          InvoiceNumber = x
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top