Access 2010 here.
Ok, Dlookups appear behave different based on where they are used. I have this Dlookup
inside a ClockNo_AferUpdate() subroutine that works well on new form entries to change a label's caption to what is found in the "Employees" DB under "EmployeeName" field based on the entered "ClockNo" in a ClockNo combo-box:
Me.LabelName1.Caption = DLookup("[EmployeeName]", "Employees", "[ClockNo] =" & Forms![InspectionEntryForm]!ClockNo)
The Employees database has four fields: AutoNumber-type "ID," Number-type "ClockNo," Text-type "Shift," and Text-type "EmployeeName."
Re-EDIT:
The RowSource for the ClockNo combo box as it sources from the Employees database:
SELECT DISTINCTROW [ClockNo], [EmployeeName] FROM [Employees] ORDER BY [ClockNo];
END Re-EDIT
What I am looking for is the same functionality in Form_Current() so browsing through older entries preserves the Label's caption based on the entered ClockNo. Unfortunately, simply re-using the above Dlookup gives a "Run-time error '3075': Syntax error (missing operator) in query expression '[ClockNo] ='."
Attaching the Dlookup as a control source to a text box does work O.K, but labels seem to be the best use here. I have mucked about with the Criteria section of the Dloopkup for some time without any real success.
End goal is to have a simple label next to a combo box that displays the employee's name based on their current and past ClockNo entries. The name is stored in a separate Employees database alongside their clock number and their shift.
This should be quite simple as both the ClockNo entry and the Label operate on the same form with the same database. Thanks for your input!
You have a combo box named ClockNo with this as its Row Source:
SELECT DISTINCTROW [ClockNo], [EmployeeName]
FROM [Employees]
ORDER BY [ClockNo];
I'm not sure why you want DISTINCTROW there. I would have suspected DISTINCT to be more appropriate. But I don't think it matters.
The important point is that the combo already includes [EmployeeName], so you shouldn't need to use DLookup to fetch [EmployeeName] again. Simply read the value from the second column of the combo's selected row.
Imagine your form includes a text box named txtEmployeeName. In the form's current event, you could do this:
Me.txtEmployeeName = Me.ClockNo.Column(1)
Notice the column index numbers start with 0, so the second column is .Column(1).
And you could do the same thing in the combo's After Update event.
Finally, you wanted to change a label's .Caption, but I showed you how to change a text box's .Value. If you can't make this technique work with the label, just use a text box instead. You can set the text box's Enabled property to No and adjust its other properties so that it is visually indistinguishable from a label.
Another approach could be simpler still. If you want to keep [ClockNo] as the combo's bound value, but are willing to display [EmployeeName] as the combo's selected value, you can set the width of the first column ([ClockNo]) to zero. You would still see both [ClockNo] and [EmployeeName] in the dropdown. If this is acceptable, you wouldn't need to bother with a label or text box.
Related
I made a query that only shows me a single field and a single record from another query, how can I put that value in a textbox in a report?
The record I want to set is a date that I insert with a MsgBox.
I read that there are ways to just put in the Control Source "=[table]![field]" but I get the Name? error, another way I read but did not understand it is defining a recordset. I can't put the value to the textbox despite trying the ways I've read in other posts.
One simple way is with DLookup() domain aggregate function expression in textbox. If field name has space or punctuation/special characters or is a reserved word, [ ] delimiters will be required. Usually table/query name does not but can't hurt.
=DLookup("[fieldname]","[tableORquery name]")
Problem:
I'm trying to make it so when a numerical field is filled in with a certain ID it fills in the other fields accordingly to what the first field was filled with. So if a person were to type in their ID it would populate it with their name accordingly.
Example:
But upon ID/numerical change I want it to do something like this:
Prior knowledge that may or may not help:
-I know this is possible with combo boxes but having a combo box with 2k-8k entries seems absurd
-I don't really think subform is optimal for this situation
-I think you can do this with "=DLookup" but I don't exactly understand how to pull it off
Please and thanks for any help. All is appreciated greatly!
Some built-in Access mechanisms:
Search field in the Navigation bar of the form.
Find feature (Ctrl-F).
Alternatively, with VBA you can use your own UI design and have more control:
Obtain the desired ID from the UI in whatever way you like - a textbox, a button and a popup, etc. Don't use a textbox that is bound to the actual ID field (ID fields should be read-only and you shouldn't use an edit field for a search field - keep them separate).
In VBA, obtain the ID that user searched for and then jump to that record using code like this:
Me.RecordsetClone.FindFirst "StudentID = " & studentID
If Not Me.RecordsetClone.NoMatch Then
Me.Bookmark = Me.RecordsetClone.Bookmark
End If
You don't need VBA for this. Simply add the Student ID textbox value as a parameter the form's query and let it filter the results according the ID provided.
The query should be something like this:
PARAMETERS [Forms]![YourMainForm]![YourIdTextBoxName] Long;
SELECT *
FROM YourTableName
WHERE ((([Student ID])=[Forms]![YourMainForm]![YourIdTextBoxName]));
When you add a new ID, requery the form to see the results.
Me.Requery
In an openoffice-base (ooBase) query, and in setting the Criterion of a column. How do I call a value from a Combo Box [combo_1] in a Form [Form1] to filter the query which produces my report?
In MSAccess it is [Forms]![Form1].[combo_1] but I can't find the syntax for ooBase any help appreciated
I can think of two ways to do this, and both of them are significantly more complicated compared to the MSAccess method.
The first way is to make the combo box save to one row of a filter table. To ensure it always saves to the same one row the "Content type" for this form or subform will need to be "SQL command" with the "Content" something like SELECT * FROM "Filter" WHERE "FilterID" = 1 (1, or whatever the primary key of the row you're using is).
Now set your query to have a join to that row of the filter table.
The second way is to use a macro. On your combobox the macro will be triggered by the event "Item status changed", and the macro would read the combo box selection and put it where you need the data to go.
Embarrassingly simple question but I can't work it out or find the answer via google.
Got something like this with two colums
But when selected it only displays one column, making the information much harder to read/ understand.
Tried changing properties in property sheet (such as column number) but to no apparent effect.
It depends to a certain extent on what you are doing, often something like this suits:
SELECT Id, Surname & ", " & Forename from Table
In other words, the bound column is a unique ID and the selection column includes both the surname and forename in a single column.
EDIT based on additional information:
SELECT [Contact].[CID], [Contact].[Csname] & ", " & [Contact].[Cfname]
FROM [Contact] ORDER BY [CID], [Csname], [Cfname];
Since my first column is used to group items in the second it didn't look nice to just separate them using
SELECT ID, [Type] & ": " & [Title] AS BothValues
as suggested. With a lot of items the dropdown looks confusing like in this example.
I found out that Access seems to display the first (technically) visible column in the combobox after a selection has been made. Therefore I ended up using
SELECT ID, [Type] & ": " & [Title] AS BothValues, Type, Title
and setting the width of the second column to the smallest possible, de facto invisible value (0";0.007";1",1"). Don't forget to raise the column count (4). Now the dropdown appears grouped, but I still get both information after the selection.
You can also use a visual workaround for this Access limitation, using a text box at the right of the combobox, and setting the textbox value equal to the third column of the combobox.
If the combobox is based on
SELECT [Contact].[CID], [Contact].[Csname], [Contact].[Cfname]
FROM [Contact] ORDER BY [CID], [Csname], [Cfname];
Set column width equal to 0cm;5cm;6cm (or whatever you may need) and combobox width equal to the second column (5cm).
Then create a new text box at the right of the combobox, without any space in between, set its width equal to the third column (6cm) and set textbox controlsource equal to:
=[combobox_name].[Column](2)
Textbox will not be modifiable by the user since it is associated to a control source.
I have a DBLookupComboBox that is wired to a database query. That part is working fine. When I run the program the DBLookupComboBox is populated with the results of the query. I'd like to to see the DBLookupComboBox populated with the first item "Please Select" when the program first runs or when a new item action is initiated. (See below image)
Also, if I'm loading a previously saved database record that had selected Index 2 "Quick Elimination" how would I get the DBLookupComboBox to display that selected entry?
Yes, "Please Select" is index 0 and it is retreived as part of the query.
You could try this (I know you've probably solved it by now, as you asked over 2 years ago), but in case anyone else was interested...
dbluLookup.KeyValue := dbluLookup.ListSource.DataSet.FieldByName(dbluLookup.KeyField).Value;
That simply sets KeyValue to the first record in the ListSource dataset, which should be the row 'Please Select'.
My guess would be that the value of the underlying table field is NULL rather than zero, which tells the DBComboBox that no value has yet been selected and it displays accordingly.
If the value in the table were zero, I thinkg the text in the edit field of the combo would be selected to indicate that, but I may recall this incorrectly.
Anyway, just check for Field1.IsNull (or IsEmpty) and then set it to zero. It does mean that you can no longer distinguish between an "unknown value" (NULL) and "no selected value" (zero), unless you prevent the zero value from making it back into the table...
One way of doing this would be to add 'Please select' to the underlying table from which you are selecting, where the key to this tuple would be 0. Then when you display the combobox and the value of the connected field is 0, 'Please select' would be displayed. Of course, you have to make sure that this value is never selected!
The DBLookupComboBox displays by default the ListField(s) whose KeyField in the ListSource matches the DataField in the DataSource. So, to ensure to display some value for the DataField being NULL, you have to provide a KeyField of NULL in the ListSource. But one could imagine not wanting a NULL value in the underlaying table associated to the ListSource.
One way to circumvent this is to add the NULL value to the dataset behind the ListSource with a UNION SELECT. That should work just fine, since that dataset does not have to be editable.
Now, to assure this special dataset is only available when you are adding a new record to the dataset associated to the DataSource, manage the query for ListSource.DataSet in DataSource.OnStateChange. When DataSource.State = dsInsert, then update ListSource.DataSet.
Modification for Steve Childs answer in TwwDBLookupCombo, InfoPower component
cboFilterTravel1.LookupValue := cboFilterTravel1.LookupTable.FieldByName(cboFilterTravel1.LookupField).Value;
Thanks Steve It's works for me