Incremental Invoice Number using VBA - Access - vba

Part of my access application does invoicing. I have an invoice number table that wholes one field "InvoiceNum". On my invoice report I have the following code:
Private Sub Report_Open(Cancel As Integer)
'lookup invoice number when invoice opens
intInvoiceNum = Nz(DLookup("InvoiceNum", "tblInvoiceNum"), 0)
End Sub`
Private Sub PageHeaderSection_Format(Cancel As Integer, FormatCount As Integer)
'Incrementally add one (1) to the invoice number for every page of in the report
Me.txtInvoiceNum = intInvoiceNum
intInvoiceNum = intInvoiceNum + 1
End Sub`
Private Sub Report_Close()
'Update tblInvoiceNum with the last invoice number in the report
db.Execute ("UPDATE InvoiceNum SET InvoiceNum = " & intInvoiceNum)
End_Sub`
Problem: My report open with groupby ClientID and ProjectNum. Multiple invoices print on the same report. If any invoice spills over to second page the Format Code runs again inserting an incremental invoice number to the second page. How can I prevent this from happening?

Consider using a Running Sum control with the InvoiceNum in the report's Page Header section. No VBA required.
First, add an invisible textbox for running count over all report's records with following:
Control Source: =1
Running Sum: Over All
Name: RunningCount
Visible: No
Second, add another invisible textbox for DLookUp of InvoiceNum:
Control Source: =DLookup("InvoiceNum", "tblInvoiceNum")
Running Sum: No
Name: intInvoiceNum
Visible: No
Finally, add a visible textbox for sum of prior two controls serving as the incremental Invoice Number:
Control Source: =[intInvoiceNum] + [RunningCount]
Running Sum: No
Name: IncrementalInvoiceNum
Visible: Yes
You may be wondering, why the first two invisible controls? Reason being is using Running Sum: Over All will include both the counts and invoice number for each record of the report (e.g., 1 + InvoiceNum - 1st record; 1 + 1 + InvoiceNum + InvoiceNum - 2nd record...)

Related

Locking Previous Records in Access Database based on ID

I am trying to build a code where previous records in a certain form are locked for editing. This would be to allow users to create and edit new forms, but not change data from previous forms. I want to be able to allow users to access and edit the previous 5 forms, but lock the data in all forms previous to that.
I've tried several things, but as a novice VBA user, nothing has worked. I tried:
Private Sub Form_Load()
If Me.ID < 22 Then
Me.AllowEdits = False
Else
Me.AllowEdits = True
End If
End Sub
I used a dummy "22" to see if the code would work, but it just ended up locking all of the records, not just numbers 1-21, like I was trying to do.
I would like the "22" to be the value of the ID field in the most recent record. I would also like it to be: If Me.ID < (ID of most current record)-5
Is this possible?
Form_Loadis wrong event, as only raised once on startup withMe.IDbeing the first record. You have to check current selected record withForm_Currentevent.
Don't rely onIDbeing consecutive (e.g. caused by deletions). Select Top 5 IDs (sort decending) and get minimum.
Create as saved query (e.g. name it FormNameTop5ID, where FormName is name of form, TableName is table/query bound to form )
SELECT TOP 5 ID FROM TableName ORDER BY ID DESC
In the form create aForm_Currentevent
Private Sub Form_Current()
If Me.ID.Value < DMin("ID", "FormNameTop5ID") Then
Me.AllowEdits = False
Else
Me.AllowEdits = True
End If
End Sub

Update Related Fields of Subform When Updating Main Form In Ms Access

I have a main form and a continuous subform which is connected to main form using invoice id, customer name and invoice date. When I add a new invoice, I select customer in main form and enter data in subform because I allow edits in my subform.
Now if I change customer in main form, customer should also change in subform for the data which I already added. But my subform turns to blank, when I select previous customer it shows the entered data.
I want my subform customer update to newly selected customer in main form. I used some vba code but its not working. The below code I got from a tutorial.
Private Sub Customer_Name_AfterUpdate()
With Me.[Sales_Invoice_Detail(its my subform].Form.RecordsetClone
Do Until .EOF
.Edit
!Customer_Name = Me.Customer_Name (This is the Field which i want to update in my subform)
!Invoice_Id = Me.Invoice_Id( it has one to many relationship between forms)
.Update
.MoveNext
Loop
'MsgBox "You Change Customer To " & Customer_Name & "!"
End With
Me.Refresh
End Sub
a continuous subform which is connected to main form using invoice id,
customer name and invoice date
Remove customer name and invoice date from the connection (MasterLinkFields/ChildLinkFields). These can be pulled either directly from the parent form or - using a query - from the source of the subform.

Access dynamic Filter a Textfield with a numberfield

my form looks like this:
My problem is, that he "LeistungsBezeichnungsRef" isnt filtering. In my main form, I selected "13222", in addition i want to be shown at the subform just "13222" positions, not 20455, too.
This is my diagram:
The Idea is, that I got a cost centre (13222) to these cost centres it exists some Bills ( excavator, crane etc ) These Bills are cost centre specific! So i build up a table called "LV", where i can type in a position, amount, value etc and in " Leistungserfassung ", I can select a specific position and associate it with a Date/Daily report Number etc , selected in table "Räumstelle".
The field "LeistungsBezeichnungsRef" ( called like "position billing" in engl. ) Has this query:
I already wrote the Combobox-Fieldname of the mainform into the query criteria.. but its not filtering
In the AfterUpdate of the search ComboBox you must requery the subform
Private Sub SearchComboBox_AfterUpdate()
TheSubform.Requery
'Filter the combo in the subform
TheSubform.Form!TheLVCombo.RowSource = "SELECT whatever FROM LV WHERE ID = " _
& SearchComboBox.Value
End Sub

How do I select a specific item in a ComboBox?

I have 2 items in a ComboBox which are "Eating In" and "Eating Out".
I want the program to add 20% VAT if the user selects "Eating In". If the user selects "Eating Out" I don't want any VAT adding.
I have the following code to work out my total which has worked successfully. I just need to find out how to assign the item in the ComboBox to add VAT.
dTotalCost = dTotalSandwichCost + dTotalDrinkCost + dTotalExtraCost
A possibility is to declare an Enum and calculate based on it. e.g.,
(Psuedocode, untested, just to give you an idea)
Enum AvailableEatingOptions
EatingIn
EatingOut
End Enum
then write like:
If comboBox.selectedIndex = AvailableEatingOptions.EatingIn Then
dVAT = GivenVATValue * 0.2 ' Take 20% of your reference value
Else
dVAT = 0
End If
then add this dVAT to your total.
[Reference]

IF the sum of a column is <= 0 then

I am making a till system for a project in access and I have hit a block.
Background
There are numerous forms covered in buttons. clicking a button adds data to a table (TblCurSale) including description and price of item. each form also has a "total" button which sends you to the payment screen, doing so copies the data from the TblCurSale to another table (TblCalc)
TblCalc has columns SaleID, Item(name of item), SalePrice. the report auto adds the sale price column
The total form has two sub reports on it, the TBLCurSale and TblCalc.
On the total screen there is a text filed in which users an input money and then press pay which inputs that figure into the TblCalc as a negative number and then refreshes the page so the new total comes up. at the bottom of the subreport.
Problem
I need an IF vba code so that I can put it so that when the total of the SalePrice column <= 0 I can run a few lines of code. what I have so far is below, so any help would be greatly appreciated.
Private Sub Pay_Click()
Dim SQLPay As String
Dim SQLToTable As String
Dim SQLMoney As Variant
SQLPay = "INSERT INTO TblCalc(SalePriceTotal) VALUES (-'" & TxtPayment & "')"
SQLToTable = "INSERT INTO TblTotalSale (CurrentSaleID, SalePrice, Item) SELECT CurrentSaleID, SalePrice, Item FROM TblCurrentSale"
SQLMoney = "IF (SUM(SalePriceTotal) FROM TblCalc) <= 0 SELECT '1' ELSE '0'"
DoCmd.SetWarnings False
DoCmd.RunSQL SQLPay
DoCmd.RunSQL SQLMoney
If SQLMoney = 1 Then
DoCmd.RunSQL SQLToTable
Me.TxtPayment = ""
Me.Refresh
DoCmd.OpenReport "rptCalc"
Else
Me.TxtPayment = ""
Me.Refresh
Me.Refresh
End If
DoCmd.SetWarnings True
End Sub
I think you would be better to structure it like a transaction table. So instead of inserting the payment into a separate table, add another row to the TblTotalSale with an Item description of "Payment" and the value as a negative number. You can then simply sum the SalePrice column to give you the balance outstanding. It also allows you to record multiple payments against the one sale.
wrt to the negative sale, I think you should put some validation code on your form to prevent users from entering negative item prices, (in the beforeInsert and beforeUpdate events on the form)