MS Access 2013, Subform operations destroy the class module - vba

I have a form in my Access project called MainForm, there is a sub form called subForm, also many buttons on the MainForm, at the same time, I created a class module to handle the OnClick event for all the buttons and the module name is classButtons.
Code in the class module:
Public WithEvents cButtons as Access.CommandButton
Dim tmpValue as String
Private Sub cButtons_Click()
Select Case cButton.Name
Case "ButtonA"
MainForm.subForm.Requery
Case "ButtonB"
Let tmpValue = subForm.ComboBox1.Value
DoCmd.RunSQL "update sometable set somefield='" & tmpValue & "'"
Case "ButtonC"
DoCmd.RunCommand acCmdUnhideColumns
End Select
End Sub
In the Open event of the MainForm, I have the following code:
For i = 0 to Me.Controls.Count - 1
If Left(Me.Controls(i).Name,6) = "cmdbtn" Then
set btnClass = New classButtons
set btnClass.cButtons = Me.Controls(i)
btnClass.cButtons.OnClick = "[Event Procedure]"
mdPublic.buttonColl.Add btnClass 'buttonColl is a collection variable declared in another module called "mdPublic"
End If
Next
Then once the MainForm is opened, all the 3 buttons works well, but once ButtonA or ButtonB is clicked, all the 3 buttons will stop working.
I tried to remove the subForm operations from ButtonA and ButtonB, and found that the problem is disappeared, so I guess the subForm operations just "destroy" the class module.
But I do need those operations, anyone has any ideas? Thank you !!!!!

What I do is make a function behind the form to handle the button behaviors, call it HandleButtonClick(x As String), then in each button Click event property:
=HandleButtonClick("ButtonA") -- change the button designation as appropriate
Also, I always name a subform container different from the subform it holds, such as ctrDetails. Code behind main form must reference the subform container name.

Related

One color change code for all ToggleButtons in a Userforms' Multipage

I have around 100 ToggleButtons.
I would like:
If .value = true then
togglebuttons.BackColor = vbRed
Else
= vbGreen
I can write the code for every one, but is there a way to create a group or class so that color change code would be applied to all of them?
-Excel365
Here's an example that creates a new class in order to handle multiple toggle buttons using one event handler. Note that it assumes that the first page of your multipage control contains your toggle buttons. Change the page reference accordingly.
First insert a new class module (Insert >> Class Module), and name it clsToggleButton.
Then copy and paste the following code into the code module for your new class . . .
Option Explicit
Public WithEvents toggleButton As MSForms.toggleButton
Private Sub toggleButton_Click()
With toggleButton
If .Value = True Then
.BackColor = vbRed
Else
.BackColor = vbGreen
End If
End With
End Sub
Then copy and paste the following code into your userform code module . . .
Option Explicit
Dim toggleButtonCollection As Collection
Private Sub UserForm_Initialize()
Set toggleButtonCollection = New Collection
Dim ctrl As MSForms.Control
Dim cToggleButton As clsToggleButton
For Each ctrl In Me.MultiPage1.Pages(0).Controls
If TypeName(ctrl) = "ToggleButton" Then
'ctrl.BackColor = vbGreen 'uncomment to initially set the backcolor to green
Set cToggleButton = New clsToggleButton
Set cToggleButton.toggleButton = ctrl
toggleButtonCollection.Add cToggleButton
End If
Next ctrl
End Sub
I have not worked with VB for many years and it was .net, so, if this solution is incorrect, let me know.
Solution 1: Arrays or Lists
You can create an array or a list containing all your toggle buttons, loop them and perform the operation you need for each of them. This will make sure that the logic above would be implemented exactly once rather than duplicated, yet, you still need to build your collections with the buttons.
Solution 2: A class
You can create a subclass for your toggle buttons and make sure that every toggle button in question will be of that class. And then you can create a static List for the class. In the constructor of each toggle button you append that button to the shared list in the class. And then you can create a shared method that loops the list and performs the logic you need.
P.S. Sorry for not writing code, I no longer remember the syntax of the language.

Form Controls not updated from Class methods

I am a newcomer to Visual Studio 2013, so my question is probably -and hopefully- a simple one.
I am currently writing a small program (in VB) that will essentially add/update/delete users from a table.
The main form has three TextBoxes (ID#, name, last name), a button to check if the user already exists and a couple more buttons (Save and Cancel)
I have also created a Class (dataLookup) where all the functions for adding, updating or deleting users are stored.
The way the program works is as follows:
1.- The user enters an ID# in the main Form's ID field and clicks on the "check user" button.
2.- The system calls a function stored in the datalookup Class that verifies if the ID# already exists.
3.- If it does, the function retrieves the name and last name from the table, assigns them to two local variables (vName and vLastName) populates the corresponding fields on the Main Form and returns TRUE. User can then either update data or Cancel (See code sample below)
MainFormName.TextBox1.Text = vName
MainFormName.TextBox2.Text = vLastName
return True
4.- If the ID# doesn't exist, the function returns FALSE. User is then able to enter new data in the three textboxes.
My problem is that I can't populate the TextBox fields from the function stored in the dataLookup Class. After the instructions are processed, the TextBoxes (which are both Enabled and have their Read Only property set to false) remain empty.
If I add the exact same code that populates the fields to the Main Form code, and assign values to the vName and vLastName variables, it works perfectly:
vName = "John"
vLastName = "Doe"
MainFormName.TextBox1.Text = vName
MainFormName.TextBox2.Text = vLastName
FYI, no errors are reported when I compile/run the program.
I am aware that I can modify the function so it will also return the name and last name and then I will be able to update the TextBox fields from the Main Form, but I am just curious: Why can't I do that from the function stored in the Class?
Hope my description was reasonably clear :) Any help will be much appreciated. Many thanks in advance!
Randy
Forms are classes (it says so at the top of each of them):
Public Class MainForm
....
As a class, an instance should be created however, VB allows what is called a default instance using the class name: MainForm.Show()
Under the hood, the compiler creates an instance of MainForm named MainForm and uses it. This is handy for developers hobbyists dabbling in code, but there are numerous ways this can bite you:
Sub DoSomeThing()
Dim frm As New Form1
frm.TextBox1.Text = cp.ToString
End Sub
Here a local instance of Form1 is created and used which has no relation to the Global Form1 which VB created. The local object goes out of scope at the end of the Sub never to be used by the rest of the app.
' in sub main:
Application.Run(New Form1) ' main form/pump
... elsewhere
Form1.TextBox1.Text = "hello, world!"
In spite of using the same name, these are actually different instances. The text will not show up and if the next line was Form1.Show(), a second copy of Form1 would display complete with the 'hello, world' text. These will also create/show new instances:
Form2.Show()
' or
Dim frm As New Form2
frm.Show()
In general, the more complex the app, the less appropriate using default instances is. For serious apps, create explicit form instances:
Dim myFrm = New Form7() ' myFrm is an instance of Form7
...
myFrm.TextBox1.Text = vName
myFrm.TextBox2.Text = vLastName
myFrm.Show
Classes or other forms can be told about this form various ways such as via a property or the constructor:
Class Foo
Private myFrm As Form7
Public Sub New(frm As Form7)
myFrm = frm
End Sub
...
End Class
Dim myFoo = New Foo(Me)
For the main/startup form, it can help to create a global reference:
Module Program
Friend frmMain As MainForm
End Module
Then set the variable when the main form loads:
Public Class MainForm
Private Sub MainForm_Load(sender ...)
frmMain = Me
End Sub
...
frmMain will be a valid reference to the main form for the entire application.

Problems when calling a public sub

I'm facing a deadend When trying to call this sub :
Public Sub backblue(ByVal frm As Form, ByVal boxname As String)
For i = 1 To 3
CType(frm.Controls(boxname & i.ToString()), TextBox).BackColor = Color.LightBlue
Next
End Sub
with button click event :
Private Sub Button1_click and bla bla....
backblue(Me, "txb1_")
End Sub
Can anybody show me a suggestion to fix the code.
It throws "Object Referrence not set to an instance bla bla" error
For information the textbox names are :
txb1_1 , txb1_2 , txb1_3
(these are some of the many textboxes in the form that i want its bakcolor changed)
and these three textboxes are already created through designer, not from execution.
i did check the textboxes names and there's nothing wrong.
the form class is also public.
if they are the only textboxs on said form you can just loop through
For Each box as Textbox In frm.Controls
box.BackColor = Color.LightBlue
Next
This error will occur if you do not declare the Form class to be public.
Also, make sure the textbox names are really correct, although this will probably cause a different error.
If you create the textboxes during execution, make sure they are initialized with New and added to the form's Controls collection.
Try this....
Public Sub backblue(ByVal frm As Form, ByVal prefix As String)
For i = 1 To 3
Dim bxName as String = prefix & i.ToString()
Dim bx as TextBox = CType(frm.Controls(bxName), TextBox)
If bx Is Nothing Then
MsgBox("Unable to find text box " +bxName)
Dim mtch() As Control = frm.Controls.Find(bxName, true)
If mtch.Length> 0 then
bx = mtch(0)
Else
Continue For
End if
End If
Bx.BackColor = Color.LightBlue
Next
End Sub
Although, a better solution would be to either create the textboxes inside a control and pass that control to BackBlue or to create an collection that has the controls and pass that in. Which brings up what is most likely yor problem your control is contained in a sub component and thus is not in the main form control collection
Alternative, you could use either the tag of the control or create a component control that implements IExtenderProvider and add it to the form --all of the above would effectively allow you to define the controls and/how they should be handled at designtime.
It may really seem that the names generated by this loop may not be the names of the original textboxes. My suggestion is before setting this Color property verify that the names generated by this loop are indeed the actual names. Maybe output this in a messagebox:
MessageBox.Show(boxname & i.ToString()) for each loop before you set the property

VBA - The Form Class has no the show method

I want create one form from another. But the Form class has no the Show method, which described at http://msdn.microsoft.com/en-us/library/office/gg251540.aspx
It's code in Form_Main:
Private Sub btnTemp_Click()
Dim frmOpt As Form_Option
Set frmOpt = New Form_Option
frmOpt.Show vbModal
End Sub
But I received the "Compile error: Method or data member not found".
Where I made mistake?
Thanks
(VBA version 6.5; Access 2007)
=====
Sorry for my previous comment: right now I see that comment isn't obvious.
I don't have subForm on my mainForm.
I have two simple form: Form_Main and Form_Option. And I want to be the next logic:
Form_Main has button "btnOption"
Click on "btnOption". The Form_Option is opening
I change options on Form_Option
And click the btnSave button on Form_Option, and the next idea is executing:
Form_Main.TimerInterval = CLng(Form_Option.edtTimerInterval.Value)
At the moment I made it simple. And that is enough for me.
I write so:
Private Sub btnOptions_Click()
' After changing options, refresh timer interval of main form
DoCmd.OpenForm "Options", , , , , acDialog
Me.TimerInterval = 1000 * CLng(MOptions.loadOption("fPeriodVerifyNoticeInterval"))
End Sub
Where fPeriodVerifyNoticeInterval is parameter that stored in the options table.
And the Options Form changes the "fPeriodVerifyNoticeInterval" parameters at saving.
My problem is solved, Thanks
The "mistake" is that Show isn't a valid Method for Access Forms. The link you provided is for UserForms which are forms made in VBA.
If you want to create a new form that way what you want is something like this:
frmOpt.Modal = true
frmOpt.Visible = true
Though what I would recommend is doing this instead:
DoCmd.OpenForm "Option", , , , , acDialog which will open the Option form as a dialog.
Caution: If you create your form using New even though you set it as modal it will not halt the progress of VBA code. This means that your variable will go out of scope as soon as the code finishes. If you want your form to remain open, you will need to set it as static within the sub or declare it outside the sub like this:
static frmOpt As Form_Option
or outside the sub private frmOpt = Form_Option or public frmOpt = Form_Option

How to access to the properties of an UserControl from code side?

make my own UserControl and I can aggregate new TabPages to a TabControl and then, inside of then TabPage, I add my own UserControl using the following code.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim TabX As New Windows.Forms.TabPage("Tab " & TabCount.ToString) '(ConfiguracionTabPage)
Dim MyControl As New ClientesEmpresa
MyControl.Name = "Control" & TabCount.ToString
If ClientesTabControl.TabPages.Count = 10 Then
ClientesTabControl.TabPages.RemoveAt(9)
End If
TabX.Controls.Add(MyControl)
TabX.Name = "Tab" & TabCount.ToString
TabX.Text = "Tab" & TabCount.ToString
MyControl.TitularLbl.Text = "Coca Cola"
Me.ClientesTabControl.TabPages.Insert(0, TabX)
Me.ClientesTabControl.SelectedIndex = 0
TabCount += 1
End Sub
My user control have several Labels, TextBox and TabPages(inside of a TabControl).
Now I want to change some properties dynamically from the source code, but I don't know how to access them.
The most similar theme that I found is this How to Acces of an User control in c#, but, as the title says, is in C#, how I can do it in VB.NET?
Sorry, I just notice that the Enter key post the comment. :(
Thanks for your feedback, I understand what are you saying but I missing something in the middle.
When I create the control in running time in the above code I can access easily to the properties of the created object, in this case my UserControl, but I don't understand how to reach the properties of a particular instance of that control from outside of Button_Click; ie. another button_click event(second button)
I was thinking to use something like
Dim ControlList As Windows.Forms.Control() = Me.ClientesTabControl.TabPages(0).Controls.Find("ModeloLbl", True)
or
ClientesTabControl.TabPages(0).Controls.OfType(Of AlarmasVehiculo)()
But I'm stuck here.
------------------------------------- 3th post ---------------
Thanks Steve, I was resolved using "Control.Find" and a For Each but your solution is easier.
There's any way to get the name of the selected tab or I must to create an Array when I create the New TabPage?, the idea is to update the text of the controls inside of the selected tab only when is selected by the user or every 5 seconds but just the in selected one.
Thanks.
To borrow M4N's answer from the C# question, and translate it to VB:
Cleanest way is to expose the desired properties as properties of your usercontrol, e.g:
Public Class MyUserControl
' expose the Text of the richtext control (read-only)
Public ReadOnly Property TextOfRichTextBox As String
Get
Return richTextBox.Text
End Get
End Property
' expose the Checked Property of a checkbox (read/write)
Public Property CheckBoxProperty As Boolean
Get
Return checkBox.Checked
End Get
Set (value As Boolean)
checkBox.Checked = value
End Set
End Property
'...
End Class
In this way you can control which properties you want to expose and whether they should be read/write or read-only. (of course you should use better names for the properties, depending on their meaning).
Another advantage of this approach is that it hides the internal implementation of your user control. Should you ever want to exchange your richtext control with a different one, you won't break the callers/users of your control.
To answer your second question, if you need to access your dynamically created controls, you can do so easily using their names, for instance:
Dim c As ClientesEmpresa= CType(Me.ClientesTabControl.TabPages("Tab1").Controls("Control1"), ClientesEmpresa)
c.CheckBoxProperty = True