Referencing a subform as variable in a module - ms-access

I've been using a module to take the name of a form and a control as variables to change a property of a control on a form. For example:
Sub mySub(formName As String, controlName As String)
Forms(formName).Controls(controlName).ForeColor = Colour.RedDark
End Sub
I'd like to do this for a property on a control on a subform as well, but can't quite figure out the syntax to also refer to the subform as a variable too.
I thought it might be something like:
Forms(mainFormName).Form(subFormName).Controls(controlName).ForeColor = Colour.RedDark
...but this isn't working (Object doesn't support this property or method).

My Access 2010 does not recognize a control property named ForeColour; but ForeColor is valid. (I don't know whether there is a locale issue involved here but my locale is US English.)
Other than that, I think you're trying to access ForeColor through the .Form(subFormName) property. Instead of that, reference the subform control, and from there the target control in its contained subform.
In this working example, Form12 contains a subform control named Child0. The subform control contains a form named fsub2 which in turn contains a text box named txtMemo_field. But note the name of the subform (the form name not the control which contains it) does not appear in this statement:
Forms("Form12").Controls("Child0").Controls("txtMemo_field").ForeColor = vbBlue

Related

Highlight A Control On A Specific Subform Record

I have a form (called frmMain) with a subform control (called subFrmSub) that is linked to the form frmSub.
frmSub's Default View property is set to Continuous Forms. frmSub contains several controls that the user can tab to.
What I would like to do is when the user tabs to a control in a specific instance of frmSub, that specific instance of the control is highlighted by setting the Border Style property to Solid. No instance of that control from other instances of frmSub should be so highlighted.
I have tried searching for a solution to this problem in the Access context, but even the best ones result in all instances of the control being highlighted.
The best I have come up with is the following code, which also highlights the entire collection of that control rather than the specific one...
Private Sub cmd02_GotFocus()
Me.cmd02.BorderStyle = 1
End Sub
Is it possible to highlight the specific control, and if so, then how? If not, then why not?
Thank you for your time.

MS Access VBA display Listbox Count to Textbox

This is not working.
Me.Textbox = Me.ListBox.ListCount
It say;
What code should I put into it?
When I load the Form, it should display the count of the ListBox's items on the textbox.
The TextBox and ListBox are not the names of the variables, but oh the classes. When you instantiate an object of Textbox (or ListBox), VB gives it the name TexBox1 (or ListBox1). You can change this name in the properties window. I suppose what you have now are TextBox1 and ListBox1.
Me.Textbox1 = Me.ListBox1.ListCount
There is nothing wrong with the syntax of your code. So the first thing that should be looked at is the names of your listbox and textbox. By default ms-access calls them list and text followed by a number. To find the assigned name go to their property and then the other tab and name of the control is at the top of the list.
Also make sure that you are running the code from the form's onload event. You may try the forms oncurrent event to see if it makes ant difence

How do I declare a reference to an ActiveX "ListView" Control in Access VBA?

I'm using Access 2003.
In "References" (Tools > References > Browse...), I've added in "Microsoft Windows Common Controls 6.0 (SP6)" (c:\windows\system32\mscomctl.ocx) and I've created/inserted an instance of the control "Microsoft ListView Control 6.0 (SP6)" on a Form and given the control the Name "MyListView".
I wanted to decorate MyListView with some custom methods so I've created a class ("DecoratedListView") which contains a member field ("lvw").
I want 'lvw' to point/reference MyListView, but I don't know what reference type to use in its declaration. Importantly, I also want to capture lvw's ColumnClick event.
I've tried:
Public WithEvents lvw As Object
Public WithEvents lvw As Control
Public WithEvents lvw As MSComctlLib.ListView.2
and none works when I
set lvw = MyForm.MyListView
Can anyone explain how I should create the reference (lvw) to the existing object (MyListView)?
Access can't really handle that sort of thing.
The OnClick should be available on your MyForm though, but you can't see it in the Event tab in properties. ActiveX Controls are too complicated for that. Instead, go into the code and choose MyListView in the combobox on the top left. You'll then find the extended ActiveX events in the combobox on the top right. One of them is ColumnClick.
Not sure what your full intentions are, but if you want to sort of mimic an isolated DecoratedListView class then create a special form that only includes a ListView control and use it as a subform for other forms. You can refer to the ListView from the parent Form as you would any other subform control object. Parent specific code can be run from the listview subform by checking the me.parent.name object which is basically the parent form. You can even call subform's listview events by changing the default Private Sub to Public Sub.
I can't comment yet, so I'll type it as an answer.
Did you look in View, Object Browser to see what methods and properties are available for that object? You should be able to write what you want using that information, assuming what you want is possible.

MS Access: Subform doesn't work, but the main form does

I have a form with a button that updates data in a table, form which works perfectly. However, when I add it as a subform on a tab paged form, it no longer does. Access prompts out asking for the [Forms]![MyForm]![textbox] variable, although it exists and is filled out. I'm guessing there's a different way to reference a subform.
Refer to the name of the form, the name of the subform control, the form property and the name of the control (reference MVPs, MS ). You have MS Access 2010, so you can use the query design window and intellisense to build the relevant string, it will work put something like:
[forms]![Gestiune]![SubformControlNameHere].Form![idInchirieri]
The expression [Forms]![MyForm]![textbox] probably appears within the query used as RowSource of a ComboBox or ListBox on the subform. This subform is now no longer Forms!MyForm but
Forms!MainForm!MySubformControl.Form
I don't know the correct names, adapt them accordingly.
Change the expression to something like
Forms!MainForm!MySubformControl.Form!textbox
Forms is the collection of the open forms. (invariable)
MainForm is the name of the form with the tab control. (adapt)
MySubformControl is the name of the control containing the subform. (adapt)
.Form designates the subform itself. (invariable)
Finally textbox is your TextBox. (should be ok, otherwise adapt)

Hide a column programmatically in MS-Access

I want to hide or show a column based on variable data from a users selection. How do you set a column to hidden in MS-Access 2003?
For Example,
After user change event...
For Each ctl In Me.FormNameHere.Form.Controls
If (TypeName(ctl) = "Textbox") Then
If InStr(GetTextList(), ctl.Name) > 0 Then
ctl.hidden = True
Else
ctl.hidden = False
End If
End If
Next ctl
What is the best approach to this type of challenge?
Is there a more obvious solution?
Controls do not have a "hidden" property (no objects in Access have a hidden property). They do have a .Visible property.
For future reference, I suggest you familiarize yourself with the Object Browser in the VBE -- open the VBE and hit F2. You can then restrict your search to the individual libraries used in your project. It does take a while to get to the point where you understand the object model, though.
Also, you can rely on Intellisense to learn the properties/methods of an object, so in the code of the form you're working with, you can type "Me.MyTextBox." and the Intellisense dropdown will show you all the properties and methods of that particular control. It doesn't work for a generic control variable (as in your code) because different control types have different properties.
And, of course, the properties sheet gives the names of the properties, even though in code they don't always use the same orthography (usually they are the same with spaces removed).
Also, there are differences in how you might want to do this depending on whether it's a regular form or a datasheet form. In a datasheet, your controls also have .ColumnHidden and .ColumnWidth properties (setting those in any view other than datasheet view has no effect, and neither of those properties are available in the standard property sheet, but changes to them are retained when you save the form).
I answered a similar question to this not long ago to do with hiding columns on a datasheet. However you seem to want to hide textboxes arranged in a column on a form, is that correct?
Iterating over all the controls in the form could be slow if you have many controls. If you really need to use textboxes like that, then you could try group the 'columns' together then hide the groups. Another approach would be to use a listbox or datasheet to represent the data, where you can alter the layout of the columns directly.
I found the ColumnHidden property does the trick.
For Each ctl In Me.FormNameHere.Form.Controls
If (TypeName(ctl) = "Textbox") Then
If InStr(GetTextList(), ctl.Name) > 0 Then
ctl.Columnhidden = True
Else
ctl.Columnhidden = False
End If
End If
Next ctl
I got a hint from this related question.
A one-liner approach is using:
forms(fname).Controls(ctrlname).columnhidden = false
where
fname is name of your form
ctrlname is name of your control