Based on form combobox selection to open the another form and move the value - ms-access-2007

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.

Related

How to Open new form and insert ID from first form, Access [duplicate]

*****EDITED
So I have a form titled "NewInvoice". This form edits a table "Invoice" which contains the following; Invoice Number, Customer, Order Date. In this form I have a button which opens a subform titled "InvoiceItem". This edits a table with the same name that contains Invoice Number, Item Code, Dimensions, Etc.
Now currently, my button has an event procedure as follows.
Private Sub CreateInvoiceItem_Click()
DoCmd.OpenForm "InvoiceItem", OpenArgs:="InvoiceNumber"
End Sub
(The reason I am using a button and opening the form in a separate window is because I have cascading combo boxes in the sub form that become broken when I insert the sub form into the parent form)
Now where I am having trouble is setting the Form Load command. What I would like to occur is that the InvoiceNumber which is filled out in the Parent form auto fills in the sub form when the button is clicked.
Private Sub Form_Load()
**** NEEDED CODE****
End Sub
So try fixing the comboboxes as described in comment under question. Also, recommend code to requery the dependent combobox be in its GotFocus event. Keep in mind, cascading combobox with lookup alias will not work nice in continuous or datasheet form.
If you really want to pass value to independent form, the OpenArgs is a good approach.
Probably need to open the form to a new record row.
DoCmd.OpenForm "InvoiceItem", , , , acFormAdd, acDialog, Me!InvoiceNumber
Need code that makes sure the form is on a new record.
Private Sub Form_Load()
If Me.NewRecord Then Me!InvoiceNumber = Me.OpenArgs
End Sub
I find that the best way to do this is to add a Public sub to the form that you're opening and then pass whatever parameters you need to this function when you open the form. So to do what you're looking to do add a function like this to the form that you're opening;
Public Sub SetUpForm(InvoiceNumber as Long)
txtInvoiceNumber.Value = InvoiceNumber
End Sub
Where txtInvoiceNumber is the control on the form that you want to put the value into.
Then from your button;
DoCmd.OpenForm "InvoiceItem"
Forms!InvoiceItem.SetUpForm InvoiceNumber
This will pass your value for the invoice number to the control on the form that you're opening. This also gives you a lot more flexibility to control the process because you can pass more than one parameter to the sub and in the sub you can perform any number of tasks.

MSAccess VBA: Pass values from a subform to a seperate form

I have a form frmDetail that contains several locked fields I want populated only when another form has been filled out, in the name of having some kind of standardization in my data. This other form frmSignOut is used to enter in a date and location that will populate the fields on frmDetail. frmSignOut also contains a subform subULookup that looks up users from a different table using an identifier number. The resulting last name, first name and phone # should also be passed to frmDetail. I also hope to combine first and last name somehow into a Last,First format.
My approach so far has been to open frmSignOut modally with acDialog and I inserted Visible=False into the On_Click event on frmSignOut. Then I try to reference my subform fields and set them to the desired fields on frmDetail. I finish by refreshing and then closing the dialog form.
Private Sub CmdSignOut_Click()
DoCmd.OpenForm("frmSignOut"),,,,,acDialog
If CurrentProject.AllForms("frmSignOut").isLoaded=True Then
Set Forms!frmSignOut!SubULookup.PhoneNbrTxt=Me.ContactNbrTxt
DoCmd.Close (acForm), ("frmSignOut")
Me.Refresh
End If
End Sub
I have only been able to get this far, trying to pull the first field PhoneNbrTxt. I can get frmSignOut to open, I put in data and when I click my 'close' command button I get run-time error 438: Object doesn't support this property or method.
The line highlighted is where I try to reference my subform field. I also have a 'Cancel' button added to frmSignOut that just closes the form, I have no errors there.
Again I'm completely new to Access without much prior experience in anything related, I'd appreciate any knowledge you guys can throw at me.
EDIT: I've been able to successfully pull the value to Me.ContactNbrTxt by adjusting my code to the below.
Me.ContactNbrTxt = Forms!FrmSignOut!SubULookup.Form!PhoneNbrTxt
It looks like the missing part was the Form! right before the control name, along with formatting this correctly and dropping Set.
Im still trying to work out how to combine first and last name before also pulling those to frmDetail, if anyone can help there.

Access: Execute Code when a value in dropdown is selected

I have a subform, which consists of a table.
In this table, one columns values are a dropdown field (with 3 types to choose from: Var1, Var2, Var3).
If a user now selects a value from this dropdown I want to execute a specific VBA code in the background.
For my understanding, that means I have to set up an event like "run code module xy if dropdown value changes".
I am a beginner and have no idea how to implement that. Hopefully you are able to help.
Thanks so much in advance!
My Code:
Private Sub runMacro()
''MyCodeHere
End Sub
My form with the table as a subform. You can select 3 different values for "solution". If one is selected, I want to run VBA code:
You have to open the form, which contains the Combo Box (the Drop Down) control, in Design View.
Then select this Combo Box control and open the Property Sheet if not already shown (you can swap its visibility with the key F4).
In the Property Sheet select the tab named Event and click into the event After Update.
Using the ...-button you can select the Code Builder to open the VBE.
It already prepared the event handler for you, where you now can place your code.

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

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

How to refresh an access form

I am building an MS Access application in which all the forms are modal. However, after data change in a form, I want to refresh the parent form of this form with newer data. Is there any way to do it. To elaborate further :
Consider there are two forms, Form A and Form B. Both are modal form. From Form A, I initiate Form B, and now Form B has the user attention. But at the close of form B, I want to refresh the Form A. Is there a way to do it?
You can repaint and / or requery:
On the close event of form B:
Forms!FormA.Requery
Is this what you mean?
No, it is like I want to run Form_Load
of Form A,if it is possible
-- Varun Mahajan
The usual way to do this is to put the relevant code in a procedure that can be called by both forms. It is best put the code in a standard module, but you could have it on Form a:
Form B:
Sub RunFormALoad()
Forms!FormA.ToDoOnLoad
End Sub
Form A:
Public Sub Form_Load()
ToDoOnLoad
End Sub
Sub ToDoOnLoad()
txtText = "Hi"
End Sub
"Requery" is indeed what you what you want to run, but you could do that in Form A's "On Got Focus" event. If you have code in your Form_Load, perhaps you can move it to Form_Got_Focus.
I recommend that you use REQUERY the specific combo box whose data you have changed AND that you do it after the Cmd.Close statement. that way, if you were inputing data, that data is also requeried.
DoCmd.Close
Forms![Form_Name]![Combo_Box_Name].Requery
you might also want to point to the recently changed value
Dim id As Integer
id = Me.[Index_Field]
DoCmd.Close
Forms![Form_Name]![Combo_Box_Name].Requery
Forms![Form_Name]![Combo_Box_Name] = id
this example supposes that you opened a form to input data into a secondary table.
let us say you save School_Index and School_Name in a School table and refer to it in a Student table (which contains only the School_Index field). while you are editing a student, you need to associate him with a school that is not in your School table, etc etc
to refresh the form you need to type -
me.refresh
in the button event on click