How to pull text from the selected TextBox on a Report? VBA - vba

I have a Report (rptCourses) which contains 170 TextBoxes which contain Course names, pulled from an access Table (tblCourses). When the user clicks on one of the Course Name TextBoxes, a form (frmRateCourse) loads containing a TextBox (txtboxCourseTitle), essentially a header, which ideally would contain the Course Name of the selected Report TextBox.
My question is: How can I take the text from the selected Report Textbox and input that text into the loaded Form's TextBox?
I apologize if this question already exists! And thank you, in advance, for any assistance!

Easiest is to pass the string as OpenArgs.
Private Sub CourseName_Click()
DoCmd.OpenForm "frmRateCourse", OpenArgs:=Me.CourseName.Value
End Sub
and in frmRateCourse:
Private Sub Form_Open(Cancel As Integer)
Me.lbTitle.Caption = Nz(Me.OpenArgs, "<no Title>")
End Sub

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.

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.

Microsoft Word VBA tab key to make textbox visible

Longtime viewer, first time question asker.
I'm currently working with UserForms within MS Word and have a particular form that can have up to 20 different labels and accompanying textboxes with varying texts. I have all but the first hidden while not in use, however I would like the next label and text box to become visible following input in the previous textbox. So if you enter data (anything) in the first textbox, the next label and text box will become visible. Does this make sense? I've seen other responses here suggest using AfterUpdate() rather than Change() or Click() but can't figure out how to use any of them. I would share my code but at this point I don't have any code to share, other than my labels and textboxes are lblField1 txtField1, lblField2 txtField2...
Any suggestions?
I would suggest using Change event, when using AfterUpdate you need to leave you TextBox for a while to fire the event. If you have only one TextBox visible there is nothing to move to. If you have more TextBoxes you would need to move back to fire AfterEvent and I don't think this is what you expect.
So, double click wherever on your userform and add the following code in code area:
Private Sub txtField1_Change()
txtField2.Visible = True
lblField2.Visible = True
End Sub
Next, add next portion for next textbox:
Private Sub txtField2_Change()
txtField3.Visible = True
lblField3.Visible = True
End Sub
And so on, if only you have an order in controls name you just need to change numbers in the end of control names.

winforms vb 2008 textbox validation issue

I have vb 2008 Winforms application.
In my Branch form I have 11 textbox fields and I want to validate 8 of them, either mandatory, or required format such as UK postcode, UK tel nos etc.
My issue now is that when the validation starts, it is validating the last text field first ( or seems to be)
here is my code
For Each oCtrl As Control In Me.Controls
If TypeOf oCtrl Is TextBox Then
oCtrl.Focus()
If Validate() = False Then
Exit Sub
End If
End If
Next
what's wrong please?
what's wrong please?
The controls collection isn't sorted or grouped. Your loop will access them in whatever order the collection has them.
Without more code it's hard to say how to fix it. However a tip may be in order. Use the same handler to handle the validate event for each textbox. This way you can keep the user at that textbox until the input is valid.
is it possible to add the items to the collection in the order of their tab indexes on form Shown event, how would I do that please?
A List(Of TextBox) and a custom sorter would probably be the way to go
Dim AllTB As New List(Of TextBox)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
AllTB.AddRange(Me.Controls.OfType(Of TextBox))
AllTB.Sort(Function(x, y) x.TabIndex.CompareTo(y.TabIndex))
End Sub
To loop through the textboxes use:
For Each tb As TextBox in AllTB
Because the TextBoxes are in the list by reference you can get or set any of the properties in the textboxes and any changes, will be reflected on your form. You could also use sequential names for the textboxes, tag property, etc. and sort by that.

Dynamic Textboxes

I have a gridview - GridView1.
I am adding rows to this Gridview1 dyanamically through code.
One of the columns is a dropdownlist that reads from a sqldatasource.
The textboxes, dropdownlists & sql datasources are all arrays.
If I change the value of the dropdownlist, it maintains its state even after the page reloads on any button click event.
This is ok.
However, the values of the textboxes are not maintained.
Say I enter "Hello World" in the textbox & click "Add" button, I want to collect the text value in dropdownlist(which I can read) & the value in textbox(which returns blank.)
Please suggest a method so that on add button click I can retrive the value I had typed in the textbox.
Each textbox has a unique id & I tried using the ID to get its value
eg
protected Sub Add_Click(ByVal sender As Object, ByVal e As System.EventArgs) handles add.click
{
Dim valueinText = gettext(1).text
}
now if I type "Hello World" in textbox: gettext(1),
Reults:
valueinText = ""
Thanks in advance
You will have to reset the values for unbound textboxes that get their value from code when you post back. In your Page Load Event...
If Page.IsPostback Then
'Code that uses dropdownlistAtoZ.SelectedValue
'to fill in correct value for Textboxes.
End If
I was trying to create the same textboxes on every page_load event. But this just erased their values & their client_ID kept changing.
Instead, I tried making all textboxes Shared and created them only once on add click event, added them to a Row (which is again shared) and on page_load if page.postback = true I just added the row to the table again. If you don't do so, the row will not be shown on reload.
This solved my problem(now the values entered in Textbox were not cleared like before).
I accessed the value by classname.textboxname(i).text.
Now the solution seems obvious, but I spent a few days trying to solve this.
Thanks!