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.
Related
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)?
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
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.
I have, what I think, is a pretty basic Access file, with a report. The report is based off a query. The query is:
SELECT * FROM dbo_NewPatient WHERE id=[Patient to view];
This works good, and prompts me for an id, I enter Id, and the results are the patient I want to see. Now I want to build a report that is based on this query, but I want to create a label on the report, that populates data from the results, so I've got this code:
Private Sub Report_Open(Cancel As Integer)
Label2.Caption = "Patient Name is " & PatientName & " his time in hospital is ... "
End Sub
I want that "PatientName" variable to be the data returned from the query. I've tried PatientName.Value, and PatientName.Text, but each time, i get an error message:
'The expression you entered has a field, control, or property name that Microsoft Office Access can't find'.
I'm assuming it doesn't know what "PatientName" is, perhaps because when I double click to open the report, I'm not yet prompted for the [Patient to view] variable, so the code doesn't yet know what PatientName is. How can I correct this, or is there a better way to go about this?
Thanks!
2 answers.
First, in your code:
Label2.Caption = "Patient Name is " & PatientName & " his time in hospital is ... "
Move it to the Private Sub Report_Load() procedure. Access will then know the value of PatientName.
Or, you can assign it in the report designer. However, you can't do this with labels, but you can with textboxes. Just lock the textbox control and it act just like a label. Put it in the Control Source property, preface it with an =, and put brackets around the fieldname. So...
="Patient Name is " & [PatientName] & " his time in hospital is ... "
It sounds like rather than doing this in VBA code, you just want to bind a column from your query (PatientName) to a label in your report. Don't do that in VBA code, do that in the report designer.
Your code could be tweaked to use Me to access the patient name field
Private Sub Report_Open(Cancel As Integer)
Label2.Caption = "Patient Name is " & Me!PatientName & " his time in hospital is ... "
End Sub
Edit:
It was pointed out that you can't do this on the open event. There is another event that you can hit that will work if you have more than one record to work with. You can use that group header paint event like this. (I even tested it...)
Private Sub GroupHeader0_Paint()
Label2.Caption = "Patient Name is " & Me!PatientName & " his time in hospital is ... "
End Sub
I'm pretty new to MS Access. I'm trying to create a simple form that will basically search for a particular record using a textbox, rather than a drop down box. Essentially a user would be able to enter an ID number and retrieve some other related Info. However, I do not want the user to be able to add any new records to the database. I've been able to get the forms to look the way I want them, but I'm not sure where to place the code (do I create a macro, insert the code into the properties of the button?) Any help is greatly appreciated!
I assume that you have bound your form to a table or a query and that you want to be able to enter the ID manually in a textbox, then press ENTER and load that record's data or display an error message if there is no such record.
As dsteele said, make sure that the form's Data property Allow Addtions is set to No to disallow users from adding records.
Then, from the AfterUpdate event of the textbox, add the following code (assuming that your textbox is named txtGoTo):
Private Sub txtGoTo_AfterUpdate()
If (txtGoTo & vbNullString) = vbNullString Then Exit Sub
Dim rs As DAO.RecordSet
Set rs = Me.RecordsetClone
rs.FindFirst "[ID]=" & txtGoTo
If rs.NoMatch Then
MsgBox "Sorry, no such record '" & txtGoTo & "' was found.", _
vbOKOnly + vbInformation
Else
Me.RecordSet.Bookmark = rs.Bookmark
End If
rs.Close
txtGoTo = Null
End Sub
Note that you will have to change the line rs.FindFirst "[ID]=" & txtGoTo to something that is adequate for your data:
"[ID]=" should be replaced by the field you want to search (it could be "[POReference]=" or something else.
if you are searching by a numeric ID, for instance because the field is an autonumber column, then the code is fine.
Otherwise, if the field you are searching on is a string (say PN12-G) then you have to change the code to:
rs.FindFirst "[ID]=""" & txtGoTo & """"
Failing to use the proper quoting (or quoting where not necessary) will result in errors of the kind Data type mismatch....
As a new user, I would recommend that you have a look at the sample NorthWind project database that is either shiped with older versions of Access or available as a template for download from Access 2007.
There a lots of techniques to learn from as a new Access developer, including other ways to implement record navigation.
Set the form property Data/'Allow Additions' to No.
Either in the AfterUpdate event of the textbox, or in the Click event of a button, you can write code or assign a macro to look up and display the record you want.