Macro on Access to hide fields does not work - ms-access

I am trying to figure out how to make a Macro work in Access but have never used it before. The program is telling me there is a type mismatch. I cannot figure out what is wrong with the way it is written, but there is certainly an issue because it does not do what I wish. What I am trying to do is make a field visible when a checkbox is checked and invisible when it is not.
My Macro page looks like this:
If [Forms]![Form name]![Checkbox]=True Then
Set Property
Control Name: Control
Property: Visible
Value:True

Place this code in the VBA editor (in "your form name" code):
Private Sub Checkbox1_Click()
If Me.Checkbox1 = True Then
Me.Control1.Visible = True
Else
Me.Control1.Visible = False
End If
End Sub
Remember to change "Checkbox1" and "Control1" to the appropriate controls names.

Related

MS Access ToggleButton Picture change

I was wondering if it is possible to change the picture of a toggle button in Access VBA depending on the state of the toggle button (pressed and not pressed)?
My previous attempt on doing it included a check if the button's value is "true", but it hasn't really produced a valid result.
If Me.Toggle4.Value = True Then
Me.Toggle4.Picture = "IMAGE"
Else
Me.Toggle4.Picture = "IMAGE"
End If
What the code above produces is the button always having the same image.
It appears that the code seemed to run properly, except that it was place in the incorrect code section. The whole if-else statement was placed in the Form_Load() sub rather than the Toggle4_OnClick(). Special thanks to June7 for the help.

MSAccess (2007) - popup and modal form - instancing via VBA

I got stuck in a very annoying matter.
I need to open a form within a class module, passing the class itself to the form so that the form can use all of the class properties and methods.
I am talking about a form, NOT ABOUT A USERFORM. (The problem would not exist in this second case).
The form must be both popup and modal.
So let's suppose this code in the calling-class method that opens the form:
sub OpenFormMethodOfTheCallingClass
set MyForm = new [Form_FormToBeOpened]
with MyForm
set .MyFatherClass = Me
.SetFocus ' ... this opens the form
MsgBox "Ok, user has closed the form ..."
end with
end sub
This way, the code flow DOESN'T "STOP" INSIDE THE FORM.
I mean that the message "Ok, user has closed the form ..." appears immediately, "in front" of the opened form.
And then, obviously, the method ends and the form (again, obviously) disappears, as it's an instance of the closing method.
In design view, both Popup and Modal form are set to TRUE.
Setting the two properties in the calling procedure this way:
with MyForm
.Modal = True
.PopUp = True
(...)
... doesn't help at all, as:
- MODAL doesn't affect the code flow behavior
- POPUP can't be set (!): it returns a run-time error.
The only way I've found to achieve my goal is opening this way:
DoCmd.OpenForm "FormToBeOpened", WindowMode:=acDialog
This way, the code flow "stucks" into the form, and only when user closes the form itself the flow returns to the calling procedure, and its following instruction.
But the problem is that I can't pass the calling class to the form.
OK, someone could object that: as my form is MODAL, no multiple contemporaneous instances of the form can be opened by the user, and therefore I could pass any property of the calling class to the form in some other ways ("bridge-public-variables", or a JSON in OpenArgs ...). But it's ... really horrible.
I fear, don't know why, this is is a very stupid question, with a simple answer. :)
Let's see.
Thks,
FL
That's by design. Access runs a single thread, so dialog means dialog.
You can pass static values with OpenArgs to the dialog form, but you can also let this - when opened - pull data and properties from the calling form.
That code doesn't have to be ugly.
It may be that WinForms of .Net and Visual Studio will better fit your needs.
Create form class Form_Popup with .Modal and .Popup = True.
In Form_Popup's module
Add property variable, propFils_Class of type Fils_Class (or Variant, if not allowed)
Add Public Sub Let Fils_Class (inputFils_Class as Variant) and Public Sub Get Fils_Class (inputFils_Class as Variant) procedures to set and retrieve the propFils_Class
I'm assuming you need the input variables to be variants, but you may be able to pass them as Fils_Class type, not sure.
In the code requiring Form_Popup:
Dim frm as Form, varFils_Class as Fils_Class
'<set up varFils_Class here>
Set frm = New Form_Popup
'or Docmd.OpenForm "Popup" hidden etc., then you need to directly reference the form, e.g. Set frm = Forms("Popup")
frm.Fils_Class = varFils_Class
frm.Visible = True
Do While frm.Visible
Sleep 1
DoEvents
'<at some point actions on Form_Popup set .Visible to False>
Loop
varFilsClass = frm.FilsClass
'<do other actions with frm as needed>
set frm = Nothing

Referring to textbox name in click event

I am trying to find a way to automatically refer to a textbox name within its click event. The name will be used in a sub called by the event code. I know that it can be hard coded, but I have many textboxes and would prefer to have general purpose code. Any help would be appreciated.
EDIT:
Yeah, I should have typed 'label' instead of 'textbox' - sorry. Anyway, I'd like to be able to use a simple bit of code like:
Private Sub lbl_Charges_Click()
tabPage (me.activecontrol.name)
End Sub
Obviously, the activecontrol method does not work on labels. The code I'm calling is:
Private Sub tabPage(page As String)
page = Mid(page, 5)
Echo False
Me.tab_Menu.Pages("pag_" & page).SetFocus
Me.tab_Menu.Visible = True
Me.lbl_Hide.SetFocus
Echo True
End Sub
The reason I'm doing all of this is that I'm trying to find a way to get rid of all of the flicker associated with this (tab menu and label backgrounds are transparent) The echo method will take care of the flicker with the tab menu pages, but doesn't help with the command buttons.
TIA
You can use ActiveControl:
Call SomeClickFunction(Me.ActiveControl.Name)
or, to pass the control itself:
Call SomeClickFunction(Me.ActiveControl)

Cannot reference to a form control which is out of focus

This is a follow up to a previous question I asked about how to update a textbox while typing. In my previous question, I had three textboxes, two of which were enabled and one which was not. They look like this:
My goal is to use the First and Last Name of the two textboxes to fill a "Code Personal" in the other textbox. As I type the first and last names, I want the Code Personal textbox to update immediately, with the format of LLLLLL_F (eg: BERNAS_P). However, whenever I try to update the Code Personal textbox, I receive this error:
The code I use to create the Code Personal format and to update the textbox is:
Private Sub TxtFName_Change()
firstName = Me.TxtFName.Value
lastName = Me.txtLName.Value
firstPart = Left(lastName, 6)
secondPart = Left(firstName, 1)
nameCode = firstPart + "_" + secondPart
upperNameCode = UCase(nameCode)
txtCodePersonal.Text = upperNameCode 'My debug tells me I have an error here
End Sub
I've tried to set the focus to the txtCodePersonal textbox through: [txtCodePersonal].SetFocus, but I still receive an error (MS Access can't move the focus to the control txtCodePersonal.)
Any Ideas as to how I can update the "Code Personal" textbox while typing in the other two textboxes?
Thanks in advance for all your help.
Do your value assignment to the text box's .Value property instead of its .Text property. Either of these should work:
Me.txtCodePersonal.Value = upperNameCode
Me.txtCodePersonal = upperNameCode
.Value is the default property for a text box, so you don't need to include it explicitly. Include it if you feel it makes the code clearer.

Opposite of combo box .Dropdown method?

Is there a way to toggle the dropdown of a combo box through VBA? .Dropdown is a method and it only works in one direction. I'm looking for the following functionality:
MyCombo.Dropdown = True
MyCombo.Dropdown = False
Obviously the above does not work in MS Access. I'm guessing I'll need to use some sort of hack/workaround for this. I'd like to avoid a .Requery. That would probably work, but there could be a major performance penalty depending on what the source of the combo box is.
I was dealing this this issue today, and after a lot of trial and error, I discovered that the following works beautifully in Access 2010:
With m_cboLookup
.ListWidth = .ListWidth ' Close combo box
'If Len(strSearch) > 3 Then .Dropdown
End With
Essentially, you are just setting the list width to the existing list width, but the combo box is closed, presumably to prepare for redrawing at a different size.
How about setting the focus to another control on the form? That should close the combo box just as if the user had moved the focus somewhere else on the form.
I was just dealing with this as well. The best I could come up with is below. It sends the ALT key twice, which closes the combobox without triggering an Undo or moving focus to another control.
SendKeys "%"
SendKeys "%"
Have you thought about a
SendKeys "{TAB}"
doesn't require you to send focus on any particular control but moves focus off this one
I had tried everything to achieve my desired combo box behavior. I finally found a method that works for me. It's way too elaborate to be the ideal solution, but it works. I tried Adam's listwidth reset method, but it didn't work for me in Access 2013. I had tried the Sendkeys method, but that caused my clients' Num Lock to be turned off. This code gives me perfect combo box behavior.
'The following code goes in a non-class module.
Public booListOpen As Boolean
Public sub subDropDown()
If booListOpen = False Then
Screen.ActiveControl.Dropdown
booListOpen = True
End If
End Sub
'The following code goes in the form module.
Private Sub cboList_Enter()
booListOpen = False
End Sub
Private Sub cboList_Change()
subDropDown
End Sub
Private Sub cboList_Click()
booListOpen = True
End Sub
I know this is an old thread but I had the same problem and tried several solutions. The .ListWidth didn't work for me. It did close the dropdown, but it displayed the bound column values (the IDs) in the 'text box' part of the combobox after the dropdown was closed. Mine is also a multi-select combobox bound to a multi-value field; may be different that the OP.
I was able to solve it by doing .Requery
worked for me.
Combo on worksheet.
SendKeys "{ESC}"
SendKeys "%{Down}"