I am somewhat of a beginner at Access and require
assistance regarding variables. Here's what I would like
to do:
set a variable called "salesordernumber" and reference
this from any query, macro or forms. Right now, I'm using
controls in forms to do this, but it's problematic (forms
aren't always open).
Any help is welcome, thanks.
Gary
In the General Declarations section I define giInvoiceNo.
Then when I need an invoice number I call the procedure
GetInvoiceNo and when it returns, I know the new invoice
number is in giInvoiceNo which I can use in a form, query,
or report.
The table "Control" is a 1 record table that contains a number
of variables the are used by the program. Invoice number
is just one of the fields in the control table.
This is just one way of doing what you want. Another way
is to do a DMax on the table and have it return the highest
Sales Order Number in the table, then add 1 to it. Then store
the new number in your Public variable.
Public giInvoiceNo As Long
Sub GetInvoiceNo()
'--Get a new Invoice no.
'--Get the number to be used, update it and put it back into the control record.
'--The Invoice no. in the control record is the next one to use.
'------------------------------------------------------------------------------
On Error GoTo GetInvoiceNo_Err
Set MYDB = CurrentDb
Set MYCRRS = MYDB.OpenRecordset("Control")
MYCRRS.MoveFirst
giInvoiceNo = MYCRRS!CT_InvoiceNo
MYCRRS.Edit
MYCRRS!CT_InvoiceNo = MYCRRS!CT_InvoiceNo + 1
MYCRRS.Update
GetInvoiceNo_Exit:
Exit Sub
GetInvoiceNo_Err:
MsgTitle = "Invoice Number Error"
MsgLine1 = "The error occured when updating the Invoice Number." & vbCrLf
MsgLine2 = "Error Code " & Err.Number & vbCrLf
MsgLine3 = Err.Description & vbCrLf
MsgLine4 = MsgLine1 & MsgLine2 & MsgLine3
MsgDialog = MB_ICONSTOP + MB_DEFBUTTON1
MsgAnswer = MsgBox(MsgLine4, MsgDialog, MsgTitle)
Resume GetInvoiceNo_Exit
End Sub
Ron