MS Access - Dlookup criteria from selected item (listbox) - ms-access

I've seen similar questions but the provided answers couldn't solve my problem.
In access I created a form.
From a listbox you can select a name. The names are listed in the table tNames in the column names_combined (last name, given name) . In two other columns last name and given name are separated.
On the right side of the listbox you can find information about the name which will be shown in text boxes.
The goal is to show the last name from table tNames.lastname by looking for tNames.names_combined.
So I tried this:
=Dlookup("lastname";"tNames";"names_compined =" & Me.listbox)
However I just get error messages in my text box.
Thanks in advance!

DLookup requires commas not colons.
This should work assuming all the names of tables/fileds and controls are correct:
=Dlookup("lastname","tNames","names_compined='" & Me.listbox & "'")
Also make sure the actual bound field of your listbox is the combined name (by the way you code says comPined).
Finally as it was pointed out in the other answer me.something will only work in the form itself or its VBA module. Everywhere else you need a global identifier.

Try with:
=Dlookup("lastname";"tNames";"names_combined = '" & Forms!YourForm!listbox & "'")

Related

MS Access DSUM issue

I have a text box in a form that sums up an "encumbered" column with the where condition matches the ID number in that table and the ID shown in the form. I get a #Name error but all of the naming is correct.
=DSum("EncAmount",[tblEncumbrances],"[BID]='" & [frmProcurementEdit]![BudgetItemID] & "''")
tblEncumbrances is the table name, EncAmount is the column whose sum I need to add up. FrmProcurementEdit is the form that the textbox is on and BudgetItemID (Also on the form) needs to match BID(In tblEncumbrances)
Am I missing something here? Any help would be greatly appreciated!!
The literal table name must be enclosed in quote marks. Also, there is an extra closing apostrophe delimiter.
According to your comment, linking fields are not same data type. This will definitely be an issue and really should correct data structure. However, it can be dealt with. Assuming BID is an integer number:
=DSum("EncAmount", "[tblEncumbrances]", "[BID]=" & CInt([BudgetItemID]))
or
=DSum("EncAmount", "[tblEncumbrances]", "[BID]=" & Val([BudgetItemID]))
=DSum("[EncAmount]","tblEncumbrances","[BID]='" & [BudgetItemID] & "'")

Expression in Textbox not passing value to the table

Obviously I am not an Access expert or I would not be asking this question. I created a small database with several tables. On a form, there are several combo boxes for the user to choose different combinations of medium, paper, sizes, etc. I have already created an expression that returns the correct value I need, but I cannot figure out how to get this value into the correct field on the table to store with the record the form is creating. Below are screen shots of the form and a couple of the tables. I have also included the expression I am using. I need the value that the expression returns to go into tbl1Artwork and populate the ArtWorkSKU field.
Expression:
=Left([PieceName],4) & [cbxArtist].Column & [cbxMedium].Column & [cbxPaperType].Column & [cbxPrintType].Column & [cbxSize].Column
The ArtWorkSKU text box is unbound as I had to type the expression in there. I am not sure if this is the correct way to accomplish the goal. In the tables below, except for the PK, all fields are Short Text.
All guidance is greatly appreciated.
Saving calculated data is usually not necessary and can be risky. Saved value can become 'out of sync' with raw data. Value can be calculated when needed.
Saving calculated data requires code (macro or VBA).
Can be as simple as Me!fieldname = Me.controlname.
Real trick is figuring out what event(s) to put code into. In your case, most likely form BeforeUpdate.
Advise not to use spaces nor punctuation/special characters (underscore only exception) in naming convention. Better would be ArtworkSKU or ArtworkSKUnum or Artwork_SKU_Num.
Create the following function in VBA:
Function UpdateSKU()
Me.ArtWorkSKU = Left(Me.[PieceName],4) & Me.[cbxArtist].Column(3) & _
Me.[cbxMedium].Column(2) & Me.[cbxPaperType].Column(2) & _
Me.[cbxPrintType].Column(2) & Me.[cbxSize].Column(2)
End Function
Then, on the form, update the After Update event property (not vba) of each "feeder" control to:
After Update: =UpdateSKU()

How to use the DLookUp function and if it is the correct function for this job?

I have two tables in my database, and a form that is opened when the user logs in.
A text box in this form will display that users name e.g. "Ollie", I want another text box to display this users contracted hours from within the users table when the form is opened.
I have tried using the DLookUp form in the before update section however nothing happens? Below is an example of what I have tried.
=DLookUp("[ContractedHours]","tblUser","[Operator] =[tblUser]![UserLogin]")
So saying that the value in my form text box "Operator" is Ollie, the value in my table column "UserLogin" is Ollie, i'd like another text box on the form to display the contracted hours on the record for Ollie.
tblUser contains these columns
ID UserLogin Contracted Hours Password
1 Ollie 8:00 *****
2 Ryan 5:00 *****
My form contains a text box that will equal either Ollie or Ryan, and I would like another text box that displays the relevant Contracted Hours.
DLookup will only work if you provide it a valid condition that allows it to identify a specific record in the table. What you have provided is not a value; it's a reference to a column in a table. There's no way for DLookup to know which record you are referring to.
Have a look at this page for some working examples. Notice they all have a condition that points to a specific record:
DLookup("UnitPrice * Quantity", "Order Details", "OrderID = 10248")
or
DLookup("CustomerID", "Orders", "OrderID = Forms![Orders]!OrderID")
It should read like:
=DLookUp("[ContractedHours]","tblUser","[UserLogin] = '" & Me!TextboxWithUserName & "'")
Based on your excerpt from table users, it's probably because you didn't include the space in the Contracted Hours column name in your DLookUp. Also, I would put your form call outside of the string.
Try:
=DLookUp("[Contracted Hours]","tblUser","[Operator] = '" & Forms![tblUser]![UserLogin] & "'")

How to Get the Column/Field Name Using Access VBA

I've copied the recordsource of a subform to a recordset by RecordClone. this subform source is composed by a query (some tables) and all the fields of this query were renamed like, for instance, id_document to "ID Document", date to "Document Date". what I need is to get those new names instead of the original names. instead of id_document, I want to get "ID Document". I tried to get them in the property or fiels collection, unsuccessfully. if any of you know how to do that, please, help me. I'd appreciate your help.
thanks in advance.
P.S.: this is the way I renamed the field in the query design view:
"ID Document": id_document
Take a look at this excellent answer from Gord Thompson (whose name is familiar to many here):
How to export a table in access to CSV with dot in field names through VBA?
It's not exactly what you're doing (this person wanted to remove a "." from field names) but you're doing something pretty similar. You can probably do a DLookup from a crosswalk table to figure out what the column should be named.
Just build a new table called tblCrosswalk and then fill it like this:
Old New
id_document ID Document
date Document Date
etc... etc...
Then, in the build the CSV header line section, instead of those "Replace" functions, put this:
s = DLookup("New", "tblCrosswalk", "Old = '" & fld.Name & "'")

Filtering A Lookup Field Based On Another Field

I have a lookup field in my table based on another table. I'm having trouble filtering those values based on another field that is entered prior to the field.
Is it possible to filter a lookup field based on another field?
EDIT
Let me try and clarify my original question, sorry about that. Ok, so I have a table1 that has the following fields: ID, Name, Logo.
If a user enters a specific name in the Name field, when they click on the Logo field, it'll only display those values associated that are similar to the name entered. Does that make any sense? If it does make sense, would there be an easier suggesion on accomplishing this task?
If you're talking about inside a table, the answer is "No". You can create cascading combo boxes on a form, but you can't base a lookup value in a field of a table off of a different field in that table (or the field in any other table).
Here is an example of how to handle filtering a combo box based on the value selected in another combo box:
I have the following form:
The combo boxes are named cboIntPN and cboManPN.
The Row Source for cboIntPN is set to: SELECT uniq_key, part_no, revision FROM inventor. The Row Source for cboManPN isn't set to anything.
When the user selects a value for Internal PN the following AfterUpdate Event is triggered:
Private Sub cboInternalPN_AfterUpdate()
[cboManPN].RowSourceType = "Table/Query"
[cboManPN].RowSource = "SELECT uniqmfgrhd, mfgr_pt_no FROM invtmfhd " & _
"WHERE uniq_key = '" & cboIntPN.value & "'"
End Sub
It sounds like he is having the same issue as me. I also wanted to filter a field in a table for data entry on another field's input and my conclusion is "it is time I stopped entering data manually in tables and begin to create Data entry forms. I was putting this task off until later, but if I don't do it now, I might make worse trouble for myself later.
Btw, what an old thread.