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}"
Related
On a continuous form I am attempting to toggle the caption of a toggle button to match the button’s state. In this case when the record/state is True, I would like the button to read “Current” and if the record is False have the button read “Obsolete”.
The script below works in switching between the two desired values but is switches all of the visible buttons and not for the individual records. I am not sure how to tie the individual records to the individual togglebutton's caption.
Private Sub Toggle5_Click()
If Me.Toggle5.Value = True Then
Me.Toggle5.Caption = "Current"
Else:
Me.Toggle5.Caption = "Obsolete"
End If
End Sub
I am using MS- Access 2013, I expect this question has been answer before, I have not found a working solution.
As Gustav wrote, you cannot do this directly. All static properties of controls in a continuous form always apply to all instances of that control.
Possible workaround:
Use a textbox (disabled & locked, perhaps with special effect = Raised) to show the text, with a control source like this:
= IIf([Status]=True, "Current", "Obsolete")
Put a transparent button on top of it, for easy clickability (it won't show a Click animation though).
Use Conditional formatting to set the background color of the textbox.
You can't.
An unbound control in a continuous form carries the same values and properties on all records.
What I'm looking for is deselecting / dehighlighting a field's contents by using vba, just as if the user would click with the mouse in that field. Maybe the solution is too easy to have found its way to forums ? Simple goal, but it seems difficult to be achieved. SendKeys always got errors. The .OnClick property does not simulate a click, just tells what to do on click.
My form (main- with subform) has many fields between which the focus is moved depending on the fields values. For this I'm using xyz**.SetFocus**
Works fine so far, but in many fields the user should be able to immediately edit the contents by keyboard, without first clicking into that field with the mouse. The keyboard arrows should move the cursor, not highlight the next or previous field. Combobox fields should not be highlighted at all.
There is a database option (File/Options/Client Settings/) which should enable this by selecting "Go to start of field" or "Go to end of field". However, this does not work on combobox fields (optically bad). Moreover, this option should not be set for the whole database but depend on which form has focus, and even better on the field getting focus and its contents.
You can use the .SelStart and .SelLength properties.
With Me.myCombobox
.SetFocus
.SelStart = 0
.SelLength = 0 ' Nothing selected
End With
With Me.myCombobox
.SetFocus
.SelLength = Len(.Text) ' Content selected
End With
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
My DB is split into Front and Back. I have three tables that I am working with in this problem; tblMain, tblDetail, tblNote. I know that the number of records is relatively small but I am designing this for long term use. I have a bound form (frmMain) for tblMain that has a subform (subDetail) for tblDetail. SubDetail has a subform (subNote) for tblNote. When I click a button on the main form, frmMain, the subDetail form becomes visible and allows me to add a new record. I copied the button and the VBA to subDetail so I could do the same thing for adding a record to subNote if needed. However, after the record is created, the subNote form disappears and focus is returned to subDetail. If I move to a new record then back, the subNote form becomes visible and shows an empty record except for the PK. I've checked all the properties of both subDetail and subNote. They are the exact same except for the OnCurrent event for subDetail. I have the following for the subDetail OnCurrent Event.
'Hide subform if no records
Private Sub Form_Current()
Me!ctrlmlsID.SetFocus
With Me!subNote.Form
.Visible = (.RecordsetClone.RecordCount > 0)
End With
End Sub
This hides or displays the subform, subNote, depending on if there is any records or not. Any help in figuring out this behavior would be appreciated.
Here is the code for adding a new record to subNote upon button click.
If Me.Dirty Then Me.Dirty = False
Dim AddTransID As String
AddTransID = Me!ctrlID.Value
Me!subNote.Visible = True
Me!subNote.SetFocus
DoCmd.GoToRecord , , acNewRec
Me!subNote.Form!ctrlID = AddTransID
Me!subNote.Form!ctrlType.SetFocus
I've used breakpoints and the adding record works just fine. It's just the subform going invisible again and the focus being changed back to the parent that is the issue because it won't allow those notes to be added.
I get the impression you are fighting Access here.
You normally do not need a button to create a new record using a properly bound subform with AllowAdditions = True. That will happen automatically.
The problem you are running into may stem from the fact that the new records don't really exist yet until saved (as you observe there is no PK value). So the logic to hide subforms isn't going to work. If you insist on staying with this approach you might try
.Visible = (.RecordsetClone.RecordCount > 0 or .NewRecord)
But I think your life will be easier if you remove the buttons and code you have mentioned and let Access do what it is good at doing. Make sure your subform data binding properties are set right.
This seems like it would be a simple affair, and I am sure I have done this before, but it has been a while since I have done any UI programming in Access. What I need to do is put a button on a form to toggle between datasheet and form view for a subform.
I have found a defaultview property, but nothing that looks like it would toggle the view of the form after it is already open.
Essentially I need the property I can fill in the following code..
sfEmployeeBatchEntry.Form.??? = acFormDS
I found it on my own. I was missing it because it used the silly and clunky RunCommand syntax instead of a simple property or method on the control or form classes.
It ain't pretty, but for posterity, here is the answer.
'You have to set focus to the subform control or the change view call will'
'fail (UGH!)'
MyForm.mySubFormControl.SetFocus
'Change to datasheet view...'
DoCmd.RunCommand acCmdSubformDatasheet
'Change to Form View...'
DoCmd.RunCommand acCmdSubformFormView
I tried a number of different solutions I found on different sites. Some seemed unnecessarily complicated. I cleaned out some of the clutter and found this fits my needs the best.
Dim intView As Integer
intView = Me.Form.CurrentView
If intView = 1 Then
DoCmd.RunCommand (acCmdSubformDatasheetView)
Else
DoCmd.RunCommand (acCmdSubformFormView)
End If
Exit Sub
To Toggle the View of a subForm between Continuous and Datasheet, use this code:
Private Sub cmdToggleView_Click()
If Me.frmViewDetailedTransactionsSub.Form.CurrentView = 1 Then
Me.frmViewDetailedTransactionsSub.SetFocus
DoCmd.RunCommand acCmdSubformDatasheetView
Exit Sub
End If
If Me.frmViewDetailedTransactionsSub.Form.CurrentView = 2 Then
Me.frmViewDetailedTransactionsSub.SetFocus
DoCmd.RunCommand acCmdSubformFormView
Exit Sub
End If
End Sub