Getting data to automatically come up once having selected a product - forms

I am trying to make a form on access which allows me to select a product from a drop down list and then the details that go with that product such as colour and description automatically come up. Beloe is the coding which I have done on each of the texts boxes which im using.
Any ideas?
Private Sub filterDescriptionTextBox_Click()
End Sub
Private Sub filterProductTextBox_Click()
End Sub
Private Sub sourceGroup_AfterUpdate()
ApplyDataSource
End Sub
Private Sub filterCategoryComboBox_AfterUpdate()
PerformFilter
End Sub
Private Sub filterDescriptionTextBox_AfterUpdate()
PerformFilter
End Sub
Private Sub filterProductTextBox_AfterUpdate()
PerformFilter
End Sub
Private Sub productTextBox_Click()
Shortcuts.viewProduct Me.[Colour]
End Sub
Thanks in advance, Dominique

What is your control field behind the Text (IE ListBox.selected.Value)?
As well is the form itself bound to a Table or View?
If you have a Form bound to a Table then assign the Table field names to the appropriate controls but make them dependent to the ListBox selection.
If its based on a View, then you will need to remove the Form's Bound source and then in VBA perform the data population to the TextBox but also handle the Updating of information from the selected ListBox and text from TextBox.

Related

How do i change the caption of a label from one form due to answer from a option button in a different form, VBA

I have an assignment where I am creating a benchmarking tool. The tool requires different forms to pop up depending on which button clicked.
What i want to do is to have a specific label change from one form based on the option selected from a different form. Below is the code im trying to use. I know that without Form2. ahead of the Yourlabel. I could change a label in current form (form 1) but need it to change caption of label in form 2. I hope this is clear.
Private Sub Option1_Click()
Form2.YourLabel.Caption = "Customer Code"
End Sub
Any help ?
Make Form1 have a public property of type Form2:
Private otherForm As Form2
Public Property Get TheOtherForm() As Form2
Set TheOtherForm = otherForm
End Property
Public Property Set TheOtherForm(ByVal value As Form2)
Set otherForm = value
End Property
Private Sub Option1_Click()
otherForm.YourLabel.Caption = "Customer Code"
End Sub
Now whoever is responsible for bringing up the Form1 instance, is also responsible for setting the TheOtherForm reference:
Dim frm1 As Form1
Set frm1 = New Form1
Dim frm2 As Form2
Set frm2 = New Form2
Set frm1.TheOtherForm = frm2
frm1.Show
Notice the code is Newing up form instances instead of working with the default instance of the form type: that way you fully control the objects' lifetimes.
Now, you're coupling Form1 with whever UI layout you're having on Form2. A better idea would be to introduce a little bit of abstraction between the two, by exposing a public procedure on Form2:
Public Sub SetMyLabelCaption(ByVal value As String)
YourLabel.Caption = value
End Sub
And then the click handler in Form1 can just call otherForm.SetMyLabelCaption "CustomerCode" without caring what the actual label is named, or whether there's even a label at all.
You will have to refer Form Name --> Label Name , Then call the Caption property of Label to change the caption.
Then Load the form, And do a repaint to show the new Caption of label
Private Sub CommandButton1_Click()
If OptionButton1.Enabled Then
msgbox "The Caption of your label is going to be changed"
UserForm2.lbl_frm2.Caption = "The New caption Is here"
UserForm2.Show
UserForm2.Repaint
End If
End Sub

Open a VBA Multi Tab Form in Excel to a specific tab

I'm confident this is going to be a very easy answer for anyone with VBA knowledge (which I don't have). I have a form built for Excel in VBA. It's a multitab form and I want to have the form consistently open on the first tab when the "Open Form X" button is clicked in Excel. Let's call the form "Form1" and the tab "IDTab"
I have tried the following code but neither has worked:
Private Sub GetFormButton_Click()
Form1.Show With MultiPage1 Value = 0
End Sub
AND
Private Sub BackToExclu_Click()
With MultiPage1
.Value = (.Value - 1) Mod (.Pages.Count)
End With
End Sub
You were very close, actually :)
If you place the code into the Initiliaze Event within the userform itself, it will always display the first page of the multi-page control any time the form is loaded.
Private Sub UserForm_Initialize()
Me.MultiPage1.Value = 0
End Sub
To place this code into the UserForm Module, right click on the UserForm object in the VBE and click View Code.
Then your button click code would just be:
Private Sub GetFormButton_Click()
Form1.Show
End Sub
Open y
open your excel workbook
find your form
right click the name of the form
select "show program code"
In the programcode for the form enter the code below:
Private Sub UserForm_Activate()
MultiPage1.Pages("IDTab").Enabled = True
End Sub
Mind you: MultiPage1 might be called differently in your form. MultiPage1 is the default name of the container holding the tabbed pages in my excel workbook. In the properety window you can find the actual name of that container.

Update event handling in Access form

I’m newbie in VB and Access. So I have table with Yes/No column „Channged”, and some text column, when I change text in text field (using form) I want to set Changet to true.
In my form, I handle Update event:
Private Sub Question_Updated(Code As Integer)
End Sub
How can I update Changed column through this method? Thanks.
You can use the following code to set your Yes/No UNBOUND field as long as it is included in your recordset for the form. Note that I am using the 'AfterUpdate event for the field you will be changing. The update will take place AFTER you move to another record or close the form.
Private Sub Question_AfterUpdate()
Me!YesNo = vbYes
End Sub

VBA Hide User form but retain data entered into it

I'm back again with what I hope is a fairly easy question.
I'm attempting to create a user form in VBA. The user will enter certain bits of information into the form, and then close the form. I'd like the user form to retain the data entered after it is closed by the user. I'm treating it as a class module, since techinically they are, or at least that is how I understand it. Here is the code I'm using:
In the main sub that displays the user form:
Sub NonACATMemo()
Dim UserInput As MemoReasons
Set UserInput = New MemoReasons
UserInput.Show
... And then in the user form itself to close it...
Private Sub UserForm_Terminate()
MemoReasons.Hide
End Sub
I also call this sub from a command button on the form. The issue I'm running into is that when I use this method, I get an error "Run-time error '402': Must close or hide topmost modal form first." If I use unload me, when I try to get data out of the form it is cleared and I get a "server not available" error or something to that effect.
So, any ideas on hiding a user form but retaining the data inside?
Final couple of notes: This is the only user form in the project, and here is an example of how I'm trying to get data out of it using the Public Property Get method:
Debug.Print UserInput.EmailFlag
Debug.Print UserInput.ContraFirm
Debug.Print UserInput.MemoReason
Well, I'm all ears if anyone has any suggestions.
It's an old topic... hopefully someone will need a help on this.
You can do the following :
1-Place a Close/Cancel button (you can set Cancel property to True)
2-Attach following code to Click event
Private Sub btnClose_Click()
'Do some stuff if necessary
Me.Hide
End Sub
3-Attach this code to QueryClose event
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = 0 Then 'vbFormControlMenu = 0 ([X] button on top right), see MSDN
Cancel = True 'Don't fire Terminate event...
btnClose_Click '...instead, call my close event handler
End If
End Sub
You can check the result by placing a debugger breakpoint in UserForm_Initialize event and it should be fired only for the first time showing the UserForm and thus, granting UserForm state preservation.
I've not seen this approach before. Normally, I would just instantiate the form by:
MemoReasons.Show
Indeed the _Terminate() event is wiping out the data held in the form. So the solution is to not call the _Terminate() event from the button-click. Instead, simply hide the form, e.g.:
Sub ShowMemoReasons()
'In a normal code module, this calls the form
' could be run from the macros menu or attached to
' a shape/button/etc on the worksheet.
MemoReasons.Show
End Sub
Put these in the MemoReasons code module:
Private Sub CommandButton1_Click() '<-- Rename to handle your button's click event
MemoReasons.Hide '## Hides the form but does not release it from memory
End Sub
Private Sub UserForm_Terminate()
'Any events pertaining to the termination of the form object
' otherwise, all form control data will be wiped out when
' this object releases from memory
End Sub
After you do these, if you use the button to HIDE the form, you can call the ShowMemoReasons() and it should re-display the form, while preserving data that was previously entered in the form.
If you use the red "X" button or some other event triggers the Terminate event, you will lose the form data. There are ways to do validation and prevent this with the QueryClose event if necessary.
Update
I don't think you need to Dim an instance of the user form (an exception would be if you will be potentially displaying multiple forms at the same time). Otherwise, declaring UserInput as a public variable is redundant and confusing.
Inicidentally, this is why you're getting the error: Must close or hide topmost modal form first. If you must implement it this way, instead of doing MemoReasons.hide you should use Me.Hide.
As long as you are only displaying one instance of the form, you can always refer to MemoReasons.property because MemoReasons is a public object, just like ThisWorkbook or ActiveWorksheet, etc.
Instead, you should be able to refer to this object (MemoReasons is an object) in any subroutine, for example create another one that is not called from the previous subs. Run the sub to show the form, enter in some data, and then hide the form. With the form hidden, then run this subroutine, and you should see the resulting data from the form.
Sub Test2()
Debug.Print MemoReasons.EmailFlag
End Sub

Access - How to create a dropdown with all tables populated in subform?

I have an access DB with around 20 tables. I'd like to create a form that has a dropdown menu of all my tables in it. When a table is selected, Im trying to get the subform to reflect the information from those tables.
Basically, instead of having to click and open each table, look through them within a form. Is this possible?
Is this possible?
You could call this sub to populate your dropdown menu:
Private Sub Add_Tables_To_DropdownMenu()
Dim T As TableDef
For Each T In CurrentDb.TableDefs
If (Left(T.Name, 4) <> "USys") And (T.Attributes = 0) Then
Dropdownmenu0.AddItem T.Name
End If
Next
End Sub
Then you can set an event on change of the dropdown menu, and update the SourceObject of your subform based on the value selected:
Private Sub Dropdownmenu0_AfterUpdate()
Subform1.SourceObject = "Table." & Dropdownmenu0.Value
End Sub