Automatically fill fields on an Access form - ms-access

What is the simplest way for me to get MS Access 2007 to autofill a form based on the entry added in the first field?
I currently have 4 tables.... Customers, Parts, Order Header, and Order Lines. I have created a form for the order header table with a subform for the orderlines.
ideally what i want is that when i add the customer number into the order header form, it autofills the rest of the form with the customer name and details etc etc....
and same principle... when i add the product number to the order lines form, it autofills the Order ID and part description and sales price, taking the info from the parts table and order header table.....
Now i know for the majority of you guys this is bread and butter, but please explain in the simplest form possible... i am by no means 100% computer literate.

Here is an example from a form I use. It uses Dlookup from a table in the same .mdb file. You enter a part number and then everything else populates after you hit tab:
Private Sub Item_Number_AfterUpdate()
PopulateFields
End Sub
Private Sub PopulateFields()
''''PopulateFields takes the Item Number to fill in all the remaining fields with regards to that Item
Me.Full_Desc = RTrim(DLookup("ITEMDESC", "GP_Parts_List_Import", "ITEMNMBR = '" & Me.Item_Number & "'"))
Me.Item_Type = RTrim(DLookup("ITEMTYPE", "GP_Parts_List_Import", "ITEMNMBR = '" & Me.Item_Number & "'"))
Me.General_Desc = RTrim(DLookup("ITMGEDSC", "GP_Parts_List_Import", "ITEMNMBR = '" & Me.Item_Number & "'"))
Me.Current_Cost = RTrim(DLookup("CURRCOST", "GP_Parts_List_Import", "ITEMNMBR = '" & Me.Item_Number & "'"))
Me.Item_Class_Code = RTrim(DLookup("ITMCLSCD", "GP_Parts_List_Import", "ITEMNMBR = '" & Me.Item_Number & "'"))
End Sub
UPDATE
To get to the VBA part of your application, you press F11 or right click on the control you want triggering the code. After right click, select 'Build Event' and then choose 'Code Builder' to open the VBA Editor window.
The drop down on the left will give you every control you can choose from in that form, and the drop down on the right will give you every event that control has available. So when my text box Item_Number is filled and a user moves on, AfterUpdate is triggered and runs the function PopulateFields.
You'll need to replace the text boxes and table names obviously, and this is only one way to do it. But hope this helps.

Related

Button to Add a record & subsequently view/edit that record specifically

Could you please help me regarding this coding issue.
I have 3 forms as follows:
1) frmAuditProceduresMain – A main form which includes data that will be used later.
2) frmAuditProceduresSummarySubFrom – which is the subform of frmAuditProceduresMain mentioned above. This form includes the button which triggers the code.
3) frmAuditProceduresDetails – A separate standalone form that opens on pressing the button in frmAuditProceduresSummarySubFrom.
Both frmAuditProceduresSummarySubFrom and frmAuditProceduresDetails includes a field called ProcedureName.
Initially I open frmAuditProceduresMain & through it I right the data in the ProcedureName field of its subform (whose name is frmAuditProceduresSummarySubFrom) then I press the button that is located next to this record.
When the button is pressed frmAuditProceduresDetails should open and as it is the first time to add that record, the ProcedureName that I have just added should be added automatically in the ProcedureName field of frmAuditProceduresDetails along with another field from frmAuditProceduresSummarySubFrom called ProcedureID in addition to getting data from fields of the main form (frmAuditProceduresMain).
But if the records already exist, I need when I press the button, I can view/edit that record specifically on the frmAuditProceduresDetails.
I wrote the following code, but it returns all the field of frmAuditProceduresDetails as empty and I do not know why:
Public Sub cmdAuditProceduresDetails_Click()
DoCmd.Save
Form.Refresh
DoCmd.OpenForm "frmAuditProceduresDetails", acNormal, , , acFormAdd, acWindowNormal
If Forms!frmAuditProceduresDetails!ProcedureName <> Me.ProcedureName Then
Forms!frmAuditProceduresDetails!ProcedureName = Me.ProcedureName
Forms!frmAuditProceduresDetails!ProcedureID = Me.ProcedureID
Forms!frmAuditProceduresDetails!AuCode = Me.Parent!AuCode
Forms!frmAuditProceduresDetails!AuName = Me.Parent!AuName
Forms!frmAuditProceduresDetails!AuditCycle = Me.Parent!AuditCycle
DoCmd.RunCommand acCmdRefresh
Else
DoCmd.OpenForm "frmAuditProceduresDetails", , , "ProcedureName = '" & Nz(Me.ProcedureName, "") & "'"
End If
End Sub
I shall be grateful if anyone can help me regarding it.
Regards.
Raya.

Why are duplicate records being generated in the table?

I am using MS Access 2010. I have a form that captures a couple data items and then writes the record to a table using a function that I defined. The function is called in the click event of the Write Record command-button in the form.
Form:
Code:
> Private Sub writerecord_Click()
Rem MsgBox ("in write record click event")
Dim sqlString As String
Rem 1 - write to the kit_items table
sqlString = "INSERT INTO kit_items (item_vendorid, item_name, item_description, item_cost) VALUES (" & Me!vendorlist & ", '" & Me!item_name & "', '" & Me!item_description & "', '" & Me!item_cost & "');"
DoCmd.RunSQL sqlString
MsgBox ("done writing to table kit_items")
End Sub
records saved in table looks like this
**What is generating the duplicate record ?
If your form is bound to the table that you are writing to, the button will add a second record.
If you would like to work with data this way, you would need to make sure that the form is unbound.
Beware however that if you don't take steps to 'clear' out the data once the user clicks the button to write the record, every time they click the button you will get a new record.
Is there some reason why you need to do the inserting manually (as opposed to letting the bound form handle the inserts/edits)?

Microsoft Access Database - Search textbox form

I am trying to create a form which contains a textbox and a button to click on to search for a record in a specific table and show all pertaining information for the searched text. With this form I will be using it as a sub-form so I think VBA is probably the best way.
Here is my Table:
Here is an example of what I would like to see happen:
I would like to enter a partno and click on the button and the fields will populate.
Please advise how to approach this.
On the click of the button you can set the SubForm's Recordsource to be based on the Query. It should be something like.
Private Sub Command0_Click()
Me!subFrm_searchResult.Form.RecordSource = "SELECT theFields " & _
FROM theTable " & _
WHERE partNoFieldName = '" &
Me.partNumberTextBoxName & "'"
End Sub
This should be a start for you.

Access 2002/VBA - how to populate subform from query to allow user to select a row

Consider:
I have a form I need to display payment events for individual clients on. I do not want to display all columns in the payment table, I want user-friendly column names, and I want Access to display a popup form that displays information should the user click on a row of the form (a payment event).
Firstly, I am unsure whether Access 2002 and VBA will allow me to do this. Secondly, I am unsure how to make the individual payment events on the form clickable.
I assume I can do something like:
strSQL = "SELECT payment.payment_id, payment_amount AS Amount, payment_date AS Date" & _
"FROM contact_payment, payment " & _
"WHERE contact_payment.contact_id =" & forms([ContactForm].contact_id & _
"AND payment.payment_id = contact_payment.payment_id"
in order to get the data for the clickable form. Is this the correct way, or is there a better way to achieve this?
You could use the form's DblClick event (or Click event) to open a new form:
Private Sub Form_DblClick(Cancel As Integer)
DoCmd.OpenForm "frmMoreInfo", , , "ID = " & ID
End Sub

MS access add item to combo box if non in list

I have an access form, i want to know how to add item in combo box if there is not in there.
my combo box is in value mode.
Unfortunately you cannot change the rowsource permanently without changing the form to design mode and adding the new value. It is possible to do this with code, but it is not a good idea when people are working. The easiest thing is to create a small table and add the values to that. New values will then be saved when you close the form.
Allen Browne has a description of how to do this : http://allenbrowne.com/ser-27.html
This is one of the ideas he shows:
Private Sub CategoryID_NotInList(NewData As String, Response As Integer)
Dim strTmp As String
'Get confirmation that this is not just a spelling error.
strTmp = "Add '" & NewData & "' as a new product category?"
If MsgBox(strTmp, vbYesNo + vbDefaultButton2 + vbQuestion, "Not in list") = vbYes Then
'Append the NewData as a record in the Categories table.
strTmp = "INSERT INTO Categories ( CategoryName ) " & _
"SELECT """ & NewData & """ AS CategoryName;"
DBEngine(0)(0).Execute strTmp, dbFailOnError
'Notify Access about the new record, so it requeries the combo.
Response = acDataErrAdded
End If
End Sub
You'll need to set the property limit to list to true.
Then add some code in the On Not In List event that potentially adds the value to the combo box. Here is a tutorial. Or you can view the other answer.
Please note that it is usually better to utilize a table that stores the values for your combo box. With a value list, unless you disable the shortcut menus a user can right click on the combo box select Edit List Items... and modify the list even if it is set to limit to list... which effectively defeats any limitation you are trying to place on the field.