How we can grab value from list box and fills automatically related text box in new form - ms-access-2007

I have a list box and its row source is "tblitems" with bellow fields.
Now I like when right click on this list box and select one option for example "new task" it opens new task form and automatically Grab the item number from a list box and fills related text box "item number" in "new task" form that is bounded to table "tbltask"
Now when I press "Apply" button it insert new record in "tbltask".
tblItems
item number (pk)
item name (text)
tbltask
task number (auto number,pk)
item number
enter image description here

Getting a value from a list box in a form is:
Me.List_Box_Name.Value
Or if you have unbound values then get it based on the column:
Me.List_Box_Name.Column(2)
or whichever column you need.
You can then populate fields with DLookup or a recordset. Then if you want this to happen when you open a new form, you may want to look into this:
Private Sub Form_Load()
'Stuff you want to happen when that form loads
End Sub
Updated
The following will print out the value each time you click it. You can use this method to trigger a new form to open, or you can have a user click a submit button afterwards.
Private Sub Test_List_Click()
Debug.Print Me.Test_List.Value
DoCmd.OpenForm "Form_Name"
End Sub
Now I'm not exactly sure the best way to add your variable to the opened form. If it were me with my limited knowledge, I would add a global or public string and have the Form_Load() check to see if that string length is greater than 0. If yes then it will populate the field.
Hope that helps
Update 2
Actually this link will help you populate a field from previous form:
MS Access - open a form taking a field value from a previous form

Related

MS Access one form to select table values and use to populate multiple fields on other form?

I have a table called GL_Account and IM_Productline
The table IM_Productline has various fields that need to be populated with a value from the field GL_Account.AccountKey (i.e. IM_ProductLine.InventoryAcctkey and IM_ProductLine.CostOfGoodsSoldAcctKey)
To populate the IM_ProductLine table I made a form "Product Line Maintenance" with all the fields. To populate the field IM_ProductLine.InventoryAcctkey I put a (magnifying glass) button behind the field with the following code:
Private Sub CMD_Select_GL_Account_Click()
Me.Refresh
If IsNull(Select_ProductType) Then
'do nothing
Else
Forms![Product Line Maintenance].InventoryAcctkey = Me.SelectGLAccountKey.Column(0)
Forms![Product Line Maintenance].Refresh
End If
DoCmd.Close
End Sub
So the button opens a form Called "Select GL Account" with a combo box that enable to SELECT GL_Account.AccountKey, GL_Account.Account, GL_Account.AccountDesc
FROM GL_Account; and when the OK button is clicked it writes the value from GL_Account.AccountKey to IM_ProductLine.InventoryAcctkey, closes the form "Select GL Account" and then refreshes the form "Product Line Maintenance" so the account number and description become visible for the user.
This all work fine but here's my question:
Now rather than creating a new form for every account field I need to populate (i.e. "Select Inventory GL Account" select "Cost Of Goods Sold GL Account" etc) I'd prefer to use the form "Select GL Account" to select and populate the 11 different account fields. So behind each xxxAcctkeyfield on form "Product Line Maintenance" is a (magnifying glass) button that when clicked pulls up the form "Select GL Account" and when "OK" is clicked it writes the selected AccountKey to the correct field on form "Product Line Maintenance"?
I'd greatly appreciate anyone's efforts to understand what I am trying to explain and point me in the right direction.
Ok, there is the issue that all 11 fields should not require to be "copied" since you have a relational database (you would ONLY store the row PK ID of that selection in the current report. (a so called FK (foreign key) value). That way, say you want to change the choice? Well then you could pop up that form - search + select the one record with all that information, and then upon return ONLY store the one value.
So, I would give some thoughts to the above - you want to leverage the relational database features. And as a result, you don't need to "copy" all that data. This is not much different then say creating a invoice. I can create the new invoice, but all of the address information, and the customer that this ONE invoice belongs to? Well, that is one column with a FK value that points to the customer. Once I select that one customer, then display of the customer name + address can be say a sub form or some such - but no need exists to "copy" that information. It would also means with near zero code, you could move a invoice between customers!!! - (just change the one fk column with to the new/different customer ID (PK) value.
Now, back to the question at a hand?
You can certainly pop up a form, let the user select, enter, pick and do whatever. And THEN you can have the calling code grab + pick out the values from that form.
The way you do this? It involves a not too wide known trick.
The code that calls the form can simply open that form as a dialog form. This will HALT the calling code, the user does whatever, and when done the calling code will THEN continue. Not only does the calling code continue, but it can get/grab/pull/take any values from that pop up form WIHOUT having to use global vars.
The approach is thus thus:
dim strF as string
strF = "frmPopAskInfo"
docmd.OpenForm strF,,,,,,acDialog
' above code waits for user input
if application.AllForms(strF).IsLoaded = true then
' user did not cancel, get values from form
me!AccountNo = forms(strf)!AccountNumber
etc. etc. etc.
docmd.Close acForm,strF
end if
Now the only other issue? Well, the "ok" button on the popup for DOES NOT close the form, what it does is set visible = False. This will kick the form out of dialog mode.
me.Visible = False
So, if the user hits the cancel buttton (close form) or hits the X upprer right (close form), then the form will NOT be loaded when your calling code continues. But, if they hit OK button, then you don't close the form, but ONLY set visbile = false.
This allows the calling code to continue, you are free to get/grab/take values from that form, and then once done, you close the form.
So a form close in that popup = user canceled the form.
So, a popup form, and even a modal form? They do NOT halt the VBA calling code, but a acDialog form does!
You can thus place 2 or 5 little buttons that pops up this form, the user can pick/choose/select/enter values. When they hit ok, your calling code continues, and you are free to pull values from that form. So while all 3-4 buttons might pop up that form, each individual button launch of the form can have code that follows and updates the given control the pop button was placed beside.

Getting a Value if a Field and Record number is known?

Good Evening,
I am working on a Combo Search Form that is designed to search for information by criteria. The form has a combo box containing field values and a text box beside it. The selection of a field value in the combo box will fill in the text box beside it with the relevant information for that record, all the relevant information is contained in the PetTable.
I have managed to get the combo box to display the fields from the PetTable by setting the rowSource to PetTable and the sourceType to Field List... however that's where I hit my dead end.
In the Text Box beside the combo-box I tried grabbing the value of the combo box and putting it into the textbox by making the Text box control source "=ComboBox", however this just created a textbox which has a literal text string to that of the combo box.
My next thought was to make the text box Control source "=PetTable.PetComboBox" my thought was that the PetTable references the table with my information and the "PetComboBox" becomes the field a need to get. This did not work either and gave a #Name error"
What should be happening is: In the Combo-box if I selected [Pet Name], I would hope that the textbox beside it becomes "Fido" but instead it also becomes [Pet Name].
Any and all help would be appreciated!
Thanks
Desired Effect
What you need to do is to change the Row Source Type of the Combo Box to "Table/Query". Then in the "Row Source" click on the "..." to open up the Query Builder. Select the table that you want. Add the columns that you want. I would suggest the table's primary key PetID, and then any other fields - in your case at least PetName. You may also want to sort by PetName to make it easier for the user to scroll through. Close the Query Builder and save the changes. Change the combo box's ColumnCount to 2, and set the Column Widths to be "0cm;6cm" (setting the first column to have a width of 0 means that it is not displayed to the user).
Now move to you TextBox, and set the Control Source to be:
=[Combo0].Column(1)
Note that columns in a combox box are 0-indexed, so the first column is column 0, the second (in your case containing PetName) is column 1.
As you actually want to show the field names, rather than the data in the combo box, then you will need to set the RowSourceType to be "Field List", and then select the table name as the RowSource.
You will then need a small piece of VBA to lookup the value of that field in the table for the current record:
Sub sListFieldData()
If Not IsNull(Me!Combo0) Then
Me!Text2 = DLookup(Me!Combo0, "tblPet", "PetID=" & Me!PetID)
Else
Me!Text2 = ""
End If
End Sub
And you will then need to call this procedure in the combo box's AfterUpdate event (to catch when it has been changed by the user) and also in the form's Current event (to catch when the user moves between records):
Private Sub Combo0_AfterUpdate()
Call sListFieldData
End Sub
Private Sub Form_Current()
Call sListFieldData
End Sub
Regards,

Setting ComboBox RowSource property to query in "GotFocus()" method leaves blank values in ComboBox Access VBA

I want to populate my ComboBox in an Access form using VBA based on data from another table. Previously to do this I did the following:
In the Property Sheet -> Data tab I filled out Row Source and Row Source Type fields with this information:
Now whenever I clicked on the dropdown for my combobox, it would populate the dropdown list with all of the names from t_people table.
This limited me however to when data changed in the t_people's name column. In order to get an updated list, I must close the form and re-open it so that the query runs again. I have limited the access to this Access file so that the user is only presented with x number of forms, and cannot close/open them or others.
My solution is to remove the query on the form load, and instead run the query every time the combobox gains focus, has a click event or something of the same sorts. I did this with the following event in VBA:
'Run when the "name" combobox gains focus
Private Sub nameCb_GotFocus()
[nameCb].RowSource = "SELECT name FROM t_people"
End Sub
I have set breakpoints and this code does run. However, the the combobox is not populated after it does. How can I get the combobox to populate with the values from a query for each time the combobox gains focus?
Set the RowSource in design and add a .Requery when entering the control.
Private Sub nameCb_Enter()
nameCb.Requery
End Sub

Fill in a text box when selecting a combo box option on Access

Im not very advanced in Access and I am struggling with defining the VBA code/expression build/ or Control Source for an instruction that can make the following isntruction:
If i make a selection on a Combo box (with my key field "TypeService" on my table TblMain)
Then; my text box is able to return the TypeConfig rows related to the TypeService selected before (my table TblConfig contains TypeServiceFK and TypeConfig fields)
So, my objective is to get a dynamic textbox that depends of a multi option combobox.-
Thanks for the help!!
Put a listbox on your form, let's say it's called List2, and cancel the wizard if it pops up.
In the Row Source property of the listbox, put this SQL statement
SELECT tblConfig.TypeConfig FROM tblConfig WHERE (((tblConfig.TypeService)=[Forms]![frmMain]![Combo0]));
This assumes your form is called frmMain and your combobox is called Combo0, so change those as necessary.
Right click on Combo0 and choose Build Event, then Code Builder. Replace the Sub...End Sub stub with
Private Sub Combo0_Change()
Me.List2.Requery
End Sub
Now every time Combo0 changes, the SQL statement behind List2 will run and return the TypeConfigs associated with that TypeService.

Based on form combobox selection to open the another form and move the value

I am new to access.
I have DFR form, From DFR form we are copy the record to different history card based on the asset code.
So what I did now I create one button and generate the code to open the form based on combobox selction ( the combobox contain the assed code no).
AFfter complete the DFR form press generate button it is opening the form what asset code contain in the combobox but i dont know how to copy the value from DFR form to the opened to asset code form. Because all the time I am not opening the same form so in the VBA I am not able to mention form name. Please help me
The code for the button:
Private Sub Command632_Click()
DoCmd.OpenForm Combo99.Value
End_Sub
according to the above command the form opened but iam not able move the value
For example I am using two form one is called as a DFR onother one is Mech_history card
in this case i know the designation form name. but above case i dont know the form name so please help me
What you want to do is pass an argument to the form that you are opening. This is possible as follows:
Private Sub Command632_Click()
DoCmd.OpenForm Combo99.Value, , , , , , "example"
End_Sub
Then in the form that is opening, you can use it for example as such:
Private Sub Form_Load()
Me.Label0.Caption = OpenArgs
End Sub
Where it will set the caption of Label0 to "example".
The OpenArgs argument is a single value, but you can pass multiple values by concatenating. This is already very well explained here: http://www.fmsinc.com/MicrosoftAccess/Forms/openargs/index.htm so it doesn't seem of added value of me to reword that for my answer here. Please take a look on that website.