Access 2007 Add Record using same form - ms-access

I am creating a simple invoice system in access. I currently have a form that I can use to view a particular invoice for a particular customer_id. I would also like to use this same form to add a new invoice. When the form is opened, I'd like it to already know that I want to use customer id #x. I already have the invoice table joined to the customer table on the "customer_id" field.
Currently when I open the form, it opens a blank invoice form that isn't tied to a customer at all. Basically what I want to know is, how to pass the customer ID to the form so it knows I'm "adding" a new record tied to an already-created customer ID.
(Normally I'm just opening the form, and it is already populated with the invoice details, and the customer info which is joined to the invoice details, I'd just like to be able to add a new record which is already tied to the customer)

You can pass an argument to a form when you open it by using the optional parameter OpenArgs of the DoCmd.OpenForm method.
In the form, you can get the value via Me.OpenArgs.
EDIT:
No, you just pass the value to OpenArgs, nothing else:
DoCmd.OpenForm([Invoice Detail],acNormal,,,acFormAdd,,1)
Unfortunately you can only pass one argument via OpenArgs.
If you need more, you have to do some tricks.

You need to quote your OpenArgs in your DoCmd.OpenForm method call:
DoCmd.OpenForm([Invoice Detail], acNormal, , , acFormAdd, , "[customer_id] = 1")

Related

Pass parameters from one combo box to another using two forms in Microsoft Access

Just started working with Access 2016 for a project I am on.
How can I pass selected data from one form in access to another?
That Selection will be the determining factor on what data is presented in drop down menus. Basically, if I select a department, I want that departments data to be the "select from" data on the next.
I'm not really sure if this is a clear question, so I've added pictures.
The Goal is to not have to create a new form for every department.
Update:
So I am able to link the department combo box with the lineid box with the following Query
SELECT DISTINCT Line.lineName, Department.departmentName
FROM Line INNER JOIN Department ON Line.departmentID = Department.departmentID
WHERE (((Department.departmentName)=[Forms]![Production_Select_Data_Input_Destination].[OpenArgs]));
I need to get the parameter passed so this is not prompt and the combo box for lineId loads a select list of lineId's that are within the selected department.
I figured it out using
Private Sub btnEnterDataInput_Click()
DoCmd.OpenForm "frmDataInput", , , , , , OpenArgs:=cboDepartmentName
End Sub
In my first form,
and
[Forms]![frmSelectDataInput]![cboDepartmentName]
in my second form combobox row source query.
in my second form.

How do I reference a autonumber field in access vb and be able to use it for a calculation

I have a auto number in a table that serves the purpose of a order id. I made a form to input line items for this order id into that table . I want a function that would allow me to click my "add to order button" and it would continue to use the same order id for every record until I complete the order. I tried the following in the on_click sub for my add record button. me.txt_Orderid = me.txt_OrderId - 1 but naturally that doesn't work. I am fairly new to access so the access vba is tripping me up. Ideas?
You should not be using AutoNumber on a field if you are wanting to multiple records of it. Instead create a new field and use that for your orderid. When you create new orders youll want to grab the max of that field, then add one. see below...
On Error Resume Next
DoCmd.GoToRecord , "", acNewRec
If (MacroError <> 0) Then
Beep
MsgBox MacroError.Description, vbOKOnly, ""
End If
'This is where you assign new order numbers.
Me.OrderIDFormControl = (DMax("OrderIDField", "OrderTable") + 1)
This will acheive the same result, but is better. And you can have duplicates of it.
Once you fix your ID, then every time you add a OrderLine Item you just grab the id of the order, and assign it to the new records.
There would be two tables tblOrder which will be master table for Orders. You can use OrderID as autonumber in this table. And second table would be tblOrderItems , this would be the detail table having duplicate values of OrderID for more than one Order Items.
Create a form frmOrder that binds to tblOrder, create another form that binds to tblOderItem. Now open the Order form in design mode, press F11 to see other forms in database, drag and drop frmOrderItem being create. Now this form will be a subform inside main Order form.
The sub-form property, set Master field, Child field property . You will be asked to set related fields, it would be OrderID from both the tables/forms.
You can use this form without need to code to get OrderID when inserting data to tblOrderItem. Access picks up value for you and adds.
Still if you want to have OrderID in your code for any reason or coding a unbound form/subform. You can get the value of OrderID from frmOrder form. when referring this tbOrderID text box control anywhere in the code of frmOrder, you can just use tbOrderID.value , but if you want to refer the text box in subform, remember to refer it with form name or get from Parent property of sub form for e.g. Forms![frmOrder].tbOrderID.value in case you want to use with Parent e.g. Me.Parent.form.tbOrderID.value

Access VBA - Button on Report to show additional information

I have a report called 'Expenditures' that shows various information about each record (date, amount, type, etc.). Each record also has a 'Notes' field.
I want to add a button on each line of the report (I've done that part) that will show the Notes for the selected record. I'm not sure if this would just be running a query, then displaying it, or something else.
All I have right now is a query that shows the Notes field, but when you click the button, it shows the Notes field for all the records since I don't have any criteria yet.
Is this possible? If so, I would appreciate any help! Thanks in advance.
Typically reports in access are not 'interactive' (unless you view them in "Report" view). For example, the button will not be visible on "print preview".
I suggest you add a field to your table that controls the visibility of the field (ShowNotes = Yes/No). That checkbox would be added to the form where you maintain the data. When running the report you would then add code to the "On Format" event for the details section.
Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
Me.Notes.Visible = Me.[ShowNotes].Value
End Sub
Or, if you are going to the trouble of adding the field to the table, you could adjust your query below the report to show values in the "Notes" field only if the check-box is checked. (a "no code" solution).
I had the same issue with a report 'Invoice List' that displays the summary info about all invoices, and I wanted to have a button on each line to open the report 'Invoice' that displays the full details of just that invoice. In the event properties of the button I added an OnClick event (using the macro builder). This event is OpenReport 'Invoice' and includes a filter with the following condition;
[Invoice_Data]![Invoice_No]=[Reports].[Invoice List].[Invoice_List_Invoice_No].[Value]
The report 'Invoice' has got a query added to its Record Source (Data Tab) property called 'Invoice_Data' which pulls the invoice data from the various tables, the above filter condition then just tells it which individual Invoice No to use when running this query.

Access 2010 VBA Form Pulling In Table Data

I have an "Invoices" table, which I run an "Invoices From off of to create invoices for my clients. All my client data; address, hourly rate, etc. is on my "Client Lists" table. The Tables are linked together by a common account/client ID.
I don't want to update general items like hourly rate, address, every time I create an invoice. I am looking to set up an after update event that will auto populate these general items from my "Client Lists" table when I enter the client ID on the "Invoices" form.
---Update---
Below is the code I am working with, and I keep getting an error "Expected: list separator or).
In normal speak; The service type field on my "Invoices" form, should look at my Service type field, on my Client Lists table, for the same Client ID as Account ID.
Private Sub Client_ID_Change()
Service_Type = DLookup("Service_Type", "Client Lists", "Account ID=" & Client ID)
End Sub
Have you tried anything?
The short answer is:
create a query to represent the data you want. In vba use a recordset to run the query's logic for the ID you've got, then display the returned data in the recordset on your form.
However there's no detail here, as you have little yourself
Syntax error on the dlookup function.
To specify a search criteria (last parameter of dlookup) based on a field that contains a blank space, you will have to enclose it in brackets. Also consider that control names can have blank spaces, but to reference them in vba you must replace write them using underscores.
"[Account ID]=" & Client_ID
I would strongly suggest you name table fields and form controls without blank spaces.

Filtering data by clicking a button

I'm pretty new to Access so bear with me.
First of all I have a Table with 6 columns, namely: first name, second name, address, city, state and zip.
I have designed a report where only the first names appear with a button behind them.
If the button is pressed I want a form to open on the row so that it gives the details of the person clicked.
I have already created the form it opens up, yet I can't get to open the right record.
What must I do to get the right record?
The OpenForm method of DoCmd has the following arguments:
expression.OpenForm(FormName, View, FilterName, WhereCondition,
DataMode, WindowMode, OpenArgs)
-- http://msdn.microsoft.com/en-us/library/aa141520(v=office.10).aspx
The one you need is the WhereCondition
You do not mention a unique ID on your table, but you will need one, so add an autonumber field to your table if it does not already have one. You will also need to ensure that the form (not report) includes this field, although it does not have to be visible. Your code will then look something like:
DoCmd.OpenForm "PersonDetails",,,"ID=" & Me.ID
Thanks for your reply
I already solved the problem!
I droped the report and created a form instead(I thought that only reports were able to list the rows)
Added a macro when clicked on the ID number on the dataview form the detailed view opens with the same ID number