How to make Access select the entire text in a box instead of all records when using ctrl+a? - ms-access

In Access, is there a way to set what is selected when a user hits ctrl+a? I have a text box for users to input data, but if they hit ctrl+a, it selects all of the records instead of the entire text of a field. Is there a way to get around this? I am worried about someone accidentally deleting a bunch of records instead of the text of a field.
I'm a beginner user, so please forgive me if I am asking a silly question. I tried to search the forum, but haven't found a good answer.

Working with a textbox and the Keydown event, you can say:
Private Sub Content_KeyDown(KeyCode As Integer, Shift As Integer)
' Debug.Print KeyCode, Shift
If KeyCode = vbKeyA And Shift = acCtrlMask Then
MsgBox "Please use F2"
KeyCode = 0
Shift = 0
End If
End Sub
The textbox is called Content in this case.
You may like to get more complicated and look into KeyPreview.

Related

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

Referencing the value of a hyperlinked text box

So I'm having some difficulties with this code. I know it's obnoxiously wordy, but every attempt I made to turn some of these form references into variables to save space ended with me having even less functionality than before.
Basically what I've done so far is create a navigation form with several tabs, one to create a ticket, one to resolve/edit a ticket, and one to search the tickets. The search tab is basically a continuous form that updates based on the search criteria I enter. My goal is that when I click on the ticketID for each record, it will take me to the selected record on the Resolve/Edit Ticket page (on that page I have a combo box [called cboGoToRecord] where you can select the record you want).
I have a hyperlink in place that takes the user to the Resolve/Edit page and code that works ONLY when the line I've denoted with four asterisks (for clarity) is replaced with
rst.FindFirst "ticketID =" & [some number].
When I do that, the results are as expected. If I leave it as it is below, every record looks up the first record (A Debug.print check shows that the value of this field is apparently always 1...) So I guess what I need to figure out is how do I access the ticketID hyperlink's value so that I can put it on that line and make my code function effectively? I apologize if this is overly detailed but figured too much was better than not enough.
Private Sub ticketID_Click()
'Takes user from Search Tickets to Resolve/Edit Issues tab
DoCmd.BrowseTo acBrowseToForm, "frmResolveIssues", "frmBrowseTickets.NavigationSubform"
On Error Resume Next
Dim rst As Object
Set rst = Forms!frmBrowseTickets!NavigationSubform.Form.RecordsetClone
[Forms]![frmBrowseTickets]![NavigationSubform].Form![cboGoToRecord].Value = [Forms]![frmBrowseTickets]![NavigationSubform].Form![ticketID].Value
****rst.FindFirst "ticketID =" & [Forms]![frmBrowseTickets]![NavigationSubform].Form![cboGoToRecord].Value
Forms!frmBrowseTickets!NavigationSubform.Form.Bookmark = rst.Bookmark
Debug.Print [Forms]![frmBrowseTickets]![NavigationSubform].Form![ticketID].Value
End Sub
Edit:
After altering my form to add a separate hyperlink and referencing the static ticketID, I have concluded that everything I thought was true was not. Finding the value of a hyperlink was NOT the problem. The problem is that my ticketID value truly does insist on being one, and I have no clue how to fix that.
When this works:
Debug.Print [Forms]![frmBrowseTickets]![NavigationSubform].Form![ticketID].Value
then also check out:
Debug.Print [Forms]![frmBrowseTickets]![NavigationSubform].Form![cboGoToRecord].Value
As June7, I never use the Navigation form. It complicates everything too much.

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

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

Refresh calculation after combobox selection?

I have a drop down combo box, and I also have a text box that has a Macro that does a DLOOKUP then throws the result into the blank text box. This part is working decently enough.
What's frustrating is that it doesn't update the result text box. I need to go back and forth on a record to see the change.
Of course, what I want is for the macro to run as soon as the Combobox is selected then update the result text box with the new number. I'm sure this is easy to do but I just can't figure it out.
Thanks!
Edit:
I've put the following code in my After Update event...
Sub updateDM()
KeyID = Me!DMID.Value 'vDMID.Value
If IsNull(KeyID) = False Then
AssignedPlan = DLookup("AssignedPlan", "DMPlanSum", "DMID = " & KeyID)
vAssignedPlan.Value = AssignedPlan
Me.Refresh
End If
End Sub
Private Sub vStartMonth_AfterUpdate()
updateDM
End Sub
Weird thing about it is that it kindof works, but only if I do the drop down TWICE. For example, let's say I'm on plan A = result 100 (starting selection). I choose Plan B = 200, but result box still has 100. I select Plan B again, result box now changes to 200.
On the other hand, if I change back to Plan A after choosing Plan B, it still changes to 200!
It seems like it's one step behind??
remove that code from form_update and stick it to the combobox_before/after_update event.
just assign the value directly to the textbox.
Private Sub ComboBox_AfterUpdate()
me.txt_resultBox.value = nz(DLookup("AssignedPlan", "DMPlanSum", "DMID = " & ComboBox.value),"")
End Sub

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