Transferring data between two forms? - vb.net

Okay so I've been searching for a solution for a while but can't seem to find any answers tailored to what I need to do. Also this is my first post here so I'm sorry if I'm being to vague with what I need. Basically I'm creating a program that has a few text boxes on one form and I need to display the data in those text boxes into a list box on another form. There's a button at the bottom of the first form that allows me to switch to form 2 and also display the data in the list box. I can switch to form 2 but nothing shows up in the list box. I also have a few radio buttons and check boxes on form 1 that will affect which constant values will need to show up in form 2's list box as well. One other thing is that I can't just use a form load option for getting data in the list box, it has to happen with the button press because I will be switching between forms and from my understanding the form load option only works once when the form is loaded for the first time. Anyway here is part of my code for form 1 that shows the button click:
Dim strCustomer As String
Private Sub btnPlaceOrder_Click(sender As Object, e As EventArgs) Handles btnPlaceOrder.Click
strCustomer = txtCustomer.Text
frmInvoice.ShowDialog()
frmInvoice.lstCustomerOrder.Items.Add(Me.strCustomer)
End Sub
My first form is frmMain and my second form is frmInvoice. Can anyone please help me with what I need to do differently with this code and what exactly I need to have for my code for form 2 to make this work. Again I am somewhat new to this so I'm sorry if any of this seems vague or if I'm not quite posting this is the right way. Also I'm using VB.

The code you provided should work fine but you are modifying the ListBox after you show the invoice Form. Since you are using showDialog(), the code that follows this statement will only be executed once the dialog form is dealt with (for example if you close it). This means that if you click the button a 2nd time, the ListBox should contain one item. If you switch the two lines so it looks like this, it should do what you expect it to:
frmInvoice.lstCustomerOrder.Items.Add(Me.strCustomer)
frmInvoice.ShowDialog()

Related

Endless form not showing after filter applied previously

I am using unbound endless forms to display data from my database.
The data source is set to queries which provide data to show on the form.
All tinkering with the data itself on the form is blocked (entry, adding, deleting, etc.).
The data on the form can, however, be filtered through Access' standard ways (right clicking on the data and selecting the options or through the navigation buttons down the bottom of the form).
I am using another unbound form as a menu. Buttons on the form let the user open the data display forms. The buttons are connected to the forms through an on- click event that triggers a DoCmd.OpenForm ("frmOutput") line of code to display the form.
Recently I've had users report, that opening the endless data display forms from the menu, filtering data on the form and then closing the form without taking the filter out has resulted in the form not being able to be opened again from the menu (clicking the respective button results in no action whatsoever). The bug seems to even save to the application somehow and moving the (frontend) file to another machine still shows the same error of not showing the form.
It seems that the bug appears more often when people use multiple screens, and use the application on their second screen (as per their Windows settings).
Does anybody know what causes the bug or how it can be prevented?
Any pointer in the right direction is much appreciated since I am at a loss where to even start looking for the culprit!
Hard to tell but it sounds like their filter is being saved when form is closed
If you already have an event that opens the form, try to just clear the filter property after it opens. You can do this from all your command buttons by just changing the form name that gets passed to it
Private Sub OpenUnfilteredForm(strFormName as String)
Dim frm As Form
DoCmd.OpenForm (strFormName)
DoEvents
If CurrentProject.AllForms(strFormName).IsLoaded Then
Set frm = Forms(strFormName)
With frm
.Filter = ""
.FilterOn = False
End With
End If
Set frm = Nothing
End Sub

How do I get to a control in a subform

I want to go to a control in a subform in order to be able to enter data.
I can make a DoCmd.OpenForm but then I get an extra copy of the subform on top of the main form. I would rather go directly to the subform. I have tried a lot of options and the following I thought was the most promising
Me.Subform.SetFocus
Me.Subform.Form.mycotrol.SetFocus
DoCmd.GoToControl "mycontrol"
This piece of code does not stop at the Subform to let me enter data. I have also tried Me!Subform with the same result. I have thought of adding a Stop statement, but then I don't know how to resume execution when data has been entered to the subform.
I think there is something I have not understood. Can someone help me out?
Biørn Veirø
VBA is single threaded. It can't work with Access forms this way. You could do it with a modal popup that returns a value, like an InputBox. Access forms don't return a value this way.
If you really want to have a step by step using Access forms then you need to hook in to form events. For this specific case I think the event you want is TextBox.AfterUpdate
https://learn.microsoft.com/en-us/office/vba/api/access.textbox.afterupdate-event
You can have different events on each control object, allowing you to have code that runs after each input.
First set focus to the subform control, then the control in the form:
Me!YourSubformControlName.SetFocus
Me!YourSubformControlName.Form!YourControl.SetFocus

How do I take a combobox and a textbox in one form to transfer to another form combined?

I am creating a template generator and have added a sub form to add new templates vs what is already coded in. I am able to input the new template name in one Textbox and the data in a second textbox. When I hit the add button it sends the template name to the combobox on the first form like it is supposed to but it sends the data to the textbox in the first form not attached to the Template name. How do I link the two boxes when they send over?
I have done research in various websites and consulted with several books on trying to figure this out and the code I have is the closest I have gotten to figuring it out. I feel like I am only missing one link command but not sure what it is. I am reteaching myself how to use visual basic.
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form1.ComboBox1.Items.Add(TextBox1.Text)
Form1.TextBox1.AppendText(TextBox2.Text)
Close()
End Sub
What I am expecting to happen is when I click the button it sends the newly created template over to the combobox on form1 to add to dropdown list of Templates and be usable. What is happening is Textbox1 is going to the combobox perfectly but textbox2 is not attaching to it.

How to make refresh function in VBA

I am using Access Database to make a program.
Here is the problem:
After I enter the data in textbox, which is in a blue boxes, and click 'Add Data' button, data move to the ListBox, which is marked with orange box. But I should press 'F5' button(refresh) to see the data. I want to see the data immediately after I click 'Add Data' button. Is there any way to do that?
Any comments would be greatly thankful(It would be nice if you can share your code)
Add ListBox.Requery to the button's click event right after new data has been added to the ListBox.
Private Sub YourButtonName_Click()
'Add data
Me.YourListBoxControlName.Requery
End Sub
requerying the form is the simplest way probably, but with no idea of what the button does, how your data is structured, how the form is structured, i.e. is the listbox bound or unbound, so cant really say, if F5, is doing it, then just look for the VBA method for refreshing the form.

Microsoft Access - Navigation form is causing my query to not work

I got a form that works amazingly by itself, but the second I attach it to my navigation form. I start to get prompted for user input since this line
[Forms]![frm_addReceiveReportInformation]![cbo_PurchaseOrderID]
no longer works due to the current form becoming subform in the navigation form which was explained in
ACCESS 2010 Navigation Form Query Property
I can't seem to figure a way out of using the !form since I absolutely need to retrieve the ID from a combo box to update another combo box.
I tried multiple ways of using the !forms but I can't seem to wrap my head around how to retrieve my information that I am seeking.
I got the 2 way navigation menu(vertical + horizontal tabs). Anyone got advice or has encounter this problem in the pass, who can direct me down the right path.
To access a field inside a form, which is attached to a navigation tab, you should use the following structure:
[Forms]![YourNavigationTab]![NavigationSubform].[Form]![YourField]
Note: tested in MS Access 2013
In order for queries that contain form references to work, the form must be fully loaded. I think the problem you are experiencing is that the query in the Row Source of the Part Code combo is being evaluated before the form is loaded and therefore you are being asked to input the parameter value.
You can work around this by leaving the Row Source property of the Part Code combo empty until it gets focus for the first time, something like:
Private Sub cboPartCode_GotFocus()
If Len(cboPartCode.RowSource) = 0 Then
cboPartCode.[RowSource] = "Your Query"
cboPartCode.Requery
End If
End Sub
I sometimes just put a button on the navigation form which opens the desired form as a standalone form. It is probably not the neatest solution but it does work.