Referring to textbox name in click event - ms-access

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)

Related

Macro on Access to hide fields does not work

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.

How to pass value from subform to mainform ms access

I have a form with a subform (Datasheet view form). When i click a row in subform, i want pass that row value to text box in main form . In subform even wrote this
Private Sub Form_Click()
MsgBox (Noloan)
End Sub
But i don't know how to pass that subform form_click no_loan to mainform.textbox. Thanks for your help.
Additional data:In my db , i have a table which consist field No,NoLoan,Name. NoLoan Field value as i wanna clicked .From that table i create a subform/datasheet view ,subform name is T_Loan1. Then I create Main Form, name is FindLoan2. In that main/parent form i create text box, called Text7, and put T_Loan1 in subform object at footer.
I put this
=[Forms]![FindLoan2]![subformTLOAN].[Form]![Noloan]
in my Text7 Control and It's work.
Thank's for all attention.
Goto to the subform and click on the field which has the row value
Select Field property - click on events tab --> On Click --> Code Builder
Put below code in the Click event where Form_frmMain is your form name and Me.ID is the field.
Private Sub ID_Click()
Forms!FindLoan2.Text7 = Me.ID
End Sub
The best way is to write code that ignores the forms!names. If you copy the form, or change the sub form, or even drop the sub form into a different form, then all your code breaks.
To reference the textbox in the parent form from code, use:
me.Parent!Text7 = me!id
I am not sure which version of MS Access you are using, but it is also possible to use Tempvars to store values for use elsewhere if you have a complicated application which makes referencing another form/field/control difficult.
To create:
Tempvars.Add "TempVarName", TempvarValue
To use:
Tempvars!TempVarName
To remove:
Tempvars.Remove "TempVarName
I try to limit my use of them, as it becomes hard to manage. I am always careful about when I set the values and I ensure that they are removed after I am finished with them to avoid confusion or issues with values persisting when they shouldn't.

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.

MS Access de-select listbox after running macro/query

So I have a form (frmBookingForm) in Access, and a listbox (lstMyBookings) that displays the results of a query.
Below this, I have a button (cmdDeleteBooking) which runs a delete query using the lstMyBookings selection as input. The button then runs a macro that firstly checks whether a record in the listbox is selected, and if one is, runs the delete query and requeries the data, so the deleted record is no longer shown in the listbox. If not, an error message is displayed.
However, if I then click the button again, it again runs the delete query, even though there is apparently nothing selected in the list box.
Essentially, how can I 'clear' the listbox selection?
I'd prefer a solution that can be done in a macro format, as I have little to no understanding of VBA. However, if providing a VBA solution, I'd greatly appreciate a short explanation of it.
Thanks :)
Looks like this website has a nice little function to do it. Essentially, you need to test if it's a multiselect, and then do one of two things. I suppose if you know ahead of time that it is/isn't a multiselect, you wouldn't even need the "if" statement:
If List0.MultiSelect = 0 Then
List0 = Null
Else
For Each varItem In List0.ItemsSelected
List0.Selected(varItem) = False
Next
End If
If the control's MultiSelect property is set to None, this code just sets List0 to Null. If the control's MultiSelect property is set to anything else, the code loops through all items that are currently selected, and sets the Selected property of that item to False. My example assumes your control is called List0.
EDIT
To use this code, use an event instead of a macro. Here's how you do this:
Right click on the button, select properties
In the Property Sheet window, click on the "Event" tab
Click inside of the "On Click" blank area, and click the dropdown arrow, then select "[Event Procedure]"
Click the ellipsis ("...") to go into the code editor.
In the code editor, your should already have your event for the button (let's assume the button is called Command1:
Private Sub Command1_Click()
End Sub
Add your code in between (assuming the listbox is called List0):
Private Sub Command1_Click()
If List0.MultiSelect = 0 Then
List0 = Null
Else
For Each varItem In List0.ItemsSelected
List0.Selected(varItem) = False
Next
End If
End Sub

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}"