Programmatically select record in TDBLookupComboBox - database

I am using a couple ADOQuery's to insert records in a database, one being tied to a DBLookupComboBox. I want the user to be able to insert a record, leave the DBLookupComboBox blank, and programmatically select a value in that combo box for them. I am fairly certain this is simple, I just can't find the exact code needed to do it.
I was hoping that I could just flip through records in the ADOQuery that is filling the combo box and set it on the record I am looking for, but when the code goes to do the Post it throws an exception about not being able to enter null values into the table (as in, the combo box was still sending nothing, instead of the new value)
Does anyone know what I would need to change to get this to work?

The answer was even simpler than I had imagined.
ADOQueryToUpdate->FieldByName("Column_Name")->AsInteger = ADOQueryFillingList->FieldByName("Column_Name")->AsInteger;
Earlier I was trying to cast and use FieldByName()->Value (which is a variant) and this wasn't working. Simply use AsInteger and it will update the ComboBox as you expect.
Sadly, I wish there was a bigger support interest in C++ Builder.

Related

Access 2016 | Update/Requery a combo box

Although a straight forward task, for a reason unknown to me I cannot update/refresh the combo box.
For the sake of simplicity, lets assume I have a table with a primary key, lets call it PK_Number (it is an integer number) and then I have 7 fields (6 field are required to fill in) of various types.
Thus all of the fields belong to a single table.
The PK_Number field I made it a combo box. I did this because when any PK_Number is chosen from the drop down list all the other field should change accordingly
When I try to build an After Update event with the macro builder and choose the Requery command I get the two following pop ups
Now, it is highly unlikely that the database is read only. i made the db and did not make such a thing.
There are couple more questions i want to ask but first i need to overcome this obstacle.
ALthough initialy the row source of combo box belonged to a junction table, i also changed the row source to the master table but without any luck
Can anybody help me out?
Thank you!!
i was able to solve it.
generally speaking the method is to avoid using macros and use VBA instead :-)
besides the fun:
i made a form with all the needed fields. however i also made an additional combo box that will be used to search all records and when the PK changes to change all other related fields
Lets suppose that the combo box name is cbo_PRF_Number. in the after update event i typed the below procedure.
Private Sub cbo_PRF_Number_AfterUpdate()
If Not IsNull(Me.cbo_PRF_Number.Value) Then
Me.txt_PRF_Number.SetFocus
DoCmd.FindRecord Me.cbo_PRF_Number.Value
End If
End Sub
Kindly note that in order for the above to work it is needed to make a bound text box with the same ow source as the combo box.

MS Access loading values from a different table to control source in datasheet form

please bear with me; I'm not particularly confident with MS Access, although I am quite experienced with SQL Databases from website work.
For my sins, I have been tasked with developing a database which will unify the hundreds of spreadsheets which we currently use to record details of our clients, work, and other stuff... The database is established and built, but I am now working with the forms to allow data entry.
I have a datasheet form (we have a member of admin staff who is hell-bent on refusing to use forms, so a datasheet form seems to be the best way to appease her!) and it is bound to a specific table ('referrals') to allow a user to input data into that table. It looks like this:
However, I need to be able to load values from another table ('pupil_details') into the 'UPN' field so that if a pupil already exists in the 'pupil_details' table, the user can pick that pupil and have their details automatically filled into the form for convenience.
The form's Record Source is currently:
SELECT referrals.*
FROM referrals;
The most obvious way to do this seemed at first to be to use a join query for the form's record source, along the lines of:
SELECT referrals.*, pupil_details.UPN
FROM referrals LEFT JOIN pupil_details ON referrals.UPN = pupil_details.pupil_id
and then use this as the control source but when I do this, it breaks the functionality of the form (displays all the records, prevents it being used to enter data)
So, can anybody suggest to me how I might be able to get the 'UPN' field to display a list of records based on this query when a user types into it:
SELECT pupil_details.UPN, pupil_details.name, pupil_details.date_of_birth FROM pupil_details
But then enter the details into the 'referrals' table when the user submits the form? Sort of like this that I mocked up in Photoshop:
I'm stumped, and half a day on Google has left me none the wiser... :(
Thanks!
You should use a continuous form, not a datasheet, it will give you a lot more control, but still look like Excel. Once you have the form, you can add a combobox to get the details you want. It is nearly always best to avoid Excel attitudes in Access.

Filtering a subform table based on parameters in a form

I have a form with two unbounded comboboxes that work fine (one lists several age values and the other lists several region). I have a subform, which is a query that refers to the comboboxes, but I can't get it to update when I change values in the comboboxes.
My query is:
Select * from mastertable where (age=[form]![masterform]![age] and region=[form]![masterform]![region]
The query works in the sense that if I run it and input the parameters manually it works. It also works in the sense that if I create a button that runs the query on the form, it produces the correct table/query.
My question is how do I get the query to work as a subform? I'd like to be able to select values in the combo box and see the subform update, rather than having to click on a button that runs the query separately from the form.
I also tried creating a table that the parameters on the form can bound to, then relating those variables to the mastertable, but that also didn't work.
How do I get a table/query subform to update as information about the parameters changes in the form? I'm guessing it will require some VBA code on the "after update" event associated with the comboboxes. Any idea how to do this?
Thanks
I figured it out. I just put a little VBA code that requeries the subtable after update.
Code is [subform].requery

Make Access Form Update With Buttons

I constructed an Access database for a group of end-users. This database is composed of one table, tblInventory, and several queries for them to edit their data quickly/easily. One of my queries, for example, is:
UPDATE tblInventory SET Amount = Amount-[Enter Amount]
WHERE ((([tblInventory].Equiptment_Name)=[Enter Name]));
This worked great in my opinion, but I have to please the end-user after all. They requested that I make a form and use buttons to update the data in the table for them. I have the form laid out like this:
The Equipment_Name and Amount boxes pull their information from my table, which has categories named that. My Unbound textbox field is where I would like them to be able to enter the number of the given part they would like to take out of inventory. The button should be to run my query above, but instead of prompting for inputs I would like it to use what they entered into the textbox. I've tried many different things and searched many different sites but cannot find what I'm looking for.
P.S Equiptment_Name and Amount are the only 2 datafields in the table besides other fields I have in the table to serve as more lenient ways to search for data when they entered in names. These fields are called things such as Alt_Name1 and have no real relevance to the form.
Thanks in advance for any help given.
There is a couple ways you can do it but the simplest way is:
Build your query as a predefined query(ies)
Build a Macro that disabled warnings then executes your query or queries in the order you wish to execute them.
Go to the form Define the button.
Go to the event tab.
Build an event
Set the OnClick Event to the name of the Macro.
Save and Test.

SQL 2000, Access Query

I have a SQL database where one of my fields 'Status' has one of 10 entries in it (not from a dropdown list). I let some people have access to the data through an Access database, they can add and modify entries.
My question is, in Access, how can I make it so that the 'Status' field is a drop-down list from which the users can choose from (the 10 entries which are already in the SQL database)? It would be easier for them and also mean that mistakes cannot be made.
Many thanks
Scott
The usual way to do this is to use a combo box on a form with the row source taken from the look-up table and the bound column set to the field (column) of the table to be updated.
In Access you can add lookup information to a column. That will automatically display a dropdown list.
Step 1: Start the lookup wizard:
Step 2: After the wizard, the lookup settings should look like this:
Step 3: When your users open a table, they should see the dropdown box:
In addition to the solution described by Andomar you must not use another table as the source for your lookup. You can also provide the lookup-values in a list, which is hardcoded in the table-definition. This is fine for simple scenarios where the lookup is something that is not likely to be changed.
Several issues here:
table datasheets are not suitable user interface for users.
you can create a saved QueryDef and if you view the properties of a field, the second tab is just like the second tab in table design, and allows you to use a combo box as your display type in your query. I would generally recommend against this, as, like table datasheets, a QueryDef is not a proper UI element, and if you use the saved query in other queries, you can run into the same problems that cause lookups in table fields to be such as bad idea.
you're building a UI, so use the tools that Access provides you for building a UI. That means a form. Your form's recordsource would have the bare data, and you'd create a combo box on your form that is bound to the field in the table behind your form, and displays the values from the lookup tables. There's a wizard to step you through this. If you like the look of datasheets so much (column sizing, sorting, show/hiding are all features that are nice in datasheets), you can set your form to display as a datasheet.
My advice is that for building a user interface, use the tools Access provides for creating user interface. In my opinion, a dropdown list belongs in a form, and nowhere else. While I occasionally might add one to a query for quick-and-dirty editing, I would never do that in objects that users are going to use.

Resources