MS-Access 2010: setting focus without selecting / highlighting field contents - ms-access

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

Related

how to create a combo box in access which can be converted to text box to enable writing if the user needs to add a new option to the existings?

I was wondering how I can define a combo box in a form where the user can select an option from the existing list, and if he can't find the desired option, a "add a new option" can be accessed which converts the combo box to a text box for writing the new option?
You can put a button next to the combo, for an 'add option' which is triggered when the combo 'not in list' event is hit - and the button then would call a popup form - or in your suggestion, changes the visibility of an overlayed text box that allows for entry. I suggest a popup form, however, because then you can validate the input for length, content, etc - and then once the form or text box is filled in - exiting the text box, or submitting the form would save to your table for the drop down, refresh the combo box source query, and assign the value. I do this in VBA alot with popups.
Sorry my bad english :-(
You can directly use the "On Not in List" event. Look at the examples at
https://learn.microsoft.com/en-us/office/vba/api/Access.ComboBox.NotInList
This is the scenario: A combobox called cb and a textbox called tb. Visible property of tb is false. Combobox and Textbox have the same data source. When you type a value that is not in the combobox list, that value goes to the textbox; when you finished editing textbox, it is hidden again.
Private Sub cb_NotInList(NewData As String, Response As Integer)
' Suppress error message and undo changes.
Response = acDataErrContinue
Me!cb.Undo
' Prompt user to verify they wish to add new value.
If MsgBox("Value is not in list. Add it?", vbOKCancel) = vbOK Then
Me!tb.Visible = True
Me!tb.Value = NewData
Me!tb.SetFocus
End If
End Sub
Private Sub tb_AfterUpdate()
' Hide again texbox
Me!tb.Visible = False
' Update combobox items
Me!cb.Requery
End Sub

Update result of formula in text box

I'm trying to update an unbound text box in a form I'm building.
The text box has a default value which is: =DLookUp("[Metadata]![Username]";"[Metadata]").
When the value in the field username is changed;
how do I update this change in the text box without closing the whole form and then opening it again?
--
I've tried to to do this by adding a button in the form and then on mouse up event try and either by vba or the macro editor find a solution.
It would supposedly be possible to create a macro that will close and then open the form again but it's not really what I'm looking for.
VBA: I found lots of suggestions for requery but I couldn't get this to work:
Set UserName = Forms![Update test]!Text10
UserName.Requery
A general solution that could work with multiple such text boxes is preferable
(but not required, anything that tries to point me in the right direction is welcome).
Go to the design view and select the textbox which represents your table value username, then go to events and select the On Change() event:
Private Sub username_Change()
YourTextbox.Value =DLookUp("[Metadata]![Username]";"[Metadata]")
End Sub

Toggling the Caption on a continuous Form

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.

if checkbox is marked, then create msgbox with form value in access

So I have an access form based on a table. The form has a list of colors and a yes/no checkbox next to it. If the user marks the checkboxes then clicks a button, i want a msgbox to appear to show all the colors next to the marked checkboxes. Here is the code I currently have, it does not run if I click the button a second time. It also sometimes only shows the first color and is buggy in general.
Form looks like this
Red x
Blue
Green x
Yellow x
Code looks like this
private sub command5_click()
dim rs as dao.recordset
set rs=me.recordsetclone
rs.movefirst
do while not rs.eof
if rs!checkboxes = true then
msgbox rs!color
end if
rs.movenext
loop
set rs=nothing
end sub
You need to add Me.Dirty = False at the top of command5_Click. Requerying the recordset is overkill - you lose your scroll position and your current record.
When you have a continuous form, data is written out to the database when focus is moved from the current record to a different record. This does not happen when you move focus to a control in the header or footer. This is by design, as it is a useful feature. Here's why:
Suppose you had a 3rd field in your database, a text field called "essay" where you could write a 10-line essay all about the color. The field is too big to show on the continuous part of the form, so you add a bound textbox to the form footer. As you move up and down through the color records, the essay for the current color will show at the bottom of the form. And it will be editable. When you click on the Essay textbox, the current record is still being edited. The current record can have bound controls in the header, detail and footer, and edits in any of those places will all be written to the DB simultaneously.
When you move focus to an unbound control (such as command5), it's no different. The current record is still the current record, even though none of its bound controls currently have the focus.
Whenever you want the current record to remain the current record, but to force its edits to be written to the DB, you use Me.Dirty = False.
As to why command5 only works the first time you click it? I have no idea!

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