How to Set Focus in Access Continuous Form (VBA) - vba

Right now it's being ignored. Here's the code at it's most basic, it causes an inescapable scenario currently, but I plan to condition these out. Just need to get the basic setFocus working.
Private Sub fieldbox_LostFocus()
Me.fieldbox.SetFocus
End Sub
In a regular form, it works as expected. When fieldbox loses focus, the focus is set back onto fieldbox.
In a continuous form, it does not work, and the set focus does not happen. This is possibly due to multiple instances of fieldbox existing for each record.
What I'd like is if fieldbox1, 2 and 3 exist on the continuous form, and you click out of fieldbox2, how do I set focus back to fieldbox2?

You need to use the form's BeforeUpdate() event instead of focus:
Private Sub Form_BeforeUpdate(Cancel as Integer)
If RecordOK = False Then
Dim response, strMsg as String
strMsg = "There is data missing from the record. " _
& vbCrLf & "Press Yes to continue, editing. " _
& vbCrLf & "Press No to discard all changes."
Cancel = True
response = MsgBox(strMsg, vbYesNo)
If response <> vbYes
Me.Undo
End If
End If
Code above is not tested, may need some tweaks, paste and see. My recent update provides cleaner handling of the user's response to the dialog box. (You do have Option Explicit set, don't you?!)
A good explanation of this method at the control level is found here. But you may want to use the form event, as above. Sometimes I just temporarily insert Msgbox cues in the events so I can see what is firing when.
Note the dialog box should provide clues on how to avoid wiping the work done so far by a user.

Related

Access VBA Stopped showing ActiveControl Is the focused control

I have a bit of an oddball problem. I use a routine to check focus; and ... it stopped working. I have no idea how or why.
The routine basically checks to see if the active control is the one you're checking, and if so, returns true (so we can handle the cases where it's not).
However...it recently started returning false all the time (we didn't change anything, we only noticed when some field auditing started returning weird values). Even when the control is focused, and if there's no other controls on the form, or only one form open, and the form clearly has focus.
Does anyone have any ideas how or why this might be? It's confounding me. As you can see, I've got a test field, where we're running an init in it...and the values clearly match, name, values, every field compared, and it still doesn't return true.
What am I doing wrong?
Edit: forgot to add the code.
The whole thing as-is:
' I call it from here:
' Inside form, any control, say `PurchaseCostBox`
Private Sub PurchaseCostBox_AfterUpdate()
' Check if the focus is had
If VerifyFocus(Me.PurchaseCostBox) Then
' Save more field info.
Debug.Print Me.PurchaseCostBox.SelStart
Debug.Print Me.PurchaseCostBox.SelLen
Debug.Print Me.PurchaseCostBox.Value
Else
' Do limited stuff
Debug.Print Me.PurchaseCostBox.Value
End if
End Sub
Public Function VerifyFocus(ByRef ctlWithFocus As Control) As Boolean
Dim FrmParent As Form
Dim ctlCurrentFocus As Control
On Error Resume Next
' Determine parent form for control
' Verify focus of parent form
Set FrmParent = Screen.ActiveForm
' Verify focus of control on form
Set ctlCurrentFocus = FrmParent.ActiveControl
If Not ctlCurrentFocus Is ctlWithFocus Then
ctlWithFocus.SetFocus
DoEvents
End If
' Even adding the below line does not return true:
ctlWithFocus.SetFocus
' Return true if the control currently has the focus
VerifyFocus = FrmParent.ActiveControl Is ctlWithFocus
' Discard any errors
Err.Clear
End Function
I've also had it try this:
Public Function VerifyFocus(ByRef ctlWithFocus As Control) As Boolean
On Error Resume Next
' Return true if the control currently has the focus
VerifyFocus = Screen.ActiveControl Is ctlWithFocus
' Discard any errors
Err.Clear
End Function
Neither work any more...and I'm floundering.
Well, this turned out to be something utterly unexpected, and totally unrelated to the focus.
Turns out, one of the ways I call this is by getting a control's parent, by using Control.Properties.Parent.Form. While this DOES return the correct form, it also makes the above VerifyFocus routine never return true, ever (even when it's not being used). I don't know why. I really, at this point, don't care. But I'm going to leave it here for others to find.
Refactoring my GetTopForm routines allowed me to get the focus.

How can I force a navigation control to refresh?

In my Clients form I have 2 textboxes (tbHHName, and tbHHID) and a navigation control (nav) displaying subforms/subreports.
When the user clicks tbHHName, a modal form opens to select which client to view.
Before switching clients, the form shows:
tbHHName: Client123
nav: Account1, Account2, Account3
After switching to Client456, here's what it displays:
tbHHName: Client456
nav: Account1, Account2, Account3
Here's what it should be displaying:
tbHHName: Client456
nav: Account4, Account5, Account6
After the user clicks on one of the nav tabs it displays correctly, but I can't figure out how to force refresh the nav control without that click. Any advice would be very much appreciated.
All of the nav's subforms/subreports have the NavigationWhereClause set to: "[HH_ID] = tbHHID"
Below is the Form_Load, and the tbHHName_Click subs
Private Sub Form_Load()
On Error Resume Next
'Set form recordsource
Me.RecordSource = "SELECT * FROM _HOUSEHOLDS WHERE HH_ID = " & GetSetting(AppName, Reg_JBGeneral, Reg_HH_ID, 0)
'Apologize to users
MsgBox "Please click on the Holdings tab again to refresh the data." & vbNewLine & _
" Sorry for the extra click," & vbNewLine & _
" -MaybeOn8", , AppName
End Sub
Private Sub tbHHName_Click()
On Error Resume Next
'open the form that sets new HH_ID value using the SaveSetting method
DoCmd.OpenForm "Households_Select", WindowMode:=acDialog
Form_Load
End Sub
Thanks in advance, SO
I tested with UNBOUND combobox sitting on main form to select filter parameter and NavigationWhereClause has criteria expression HH_ID=Forms![NavForm]!Combo9. Code in combobox AfterUpdate event:
Forms![NavForm]!NavigationSubform.Requery
Then I tested with code to change value of combobox and requery.
Forms!NavForm.Combo9 = 17
Forms!NavForm.NavigationSubform.Requery
This all works and does not involve changing RecordSource. Now just decide where to place code that changes control's value and requery subform.
Also, can use DoCmd.BrowseTo method to set focus on particular Navigation tab.
Found a fix. I tacked this onto the end of the Form_Load sub:
Forms("Households").NavigationSubform.Report.FilterOn = False
Forms("Households").NavigationSubform.Report.FilterOn = True
Even though there are mixed subforms and subreports in the navigation control, everything seems to load fine.

Access Navigation Subform, recordsource

I am working with MS Access and I am currently trying out the navigation sub-forms. However I am finding it difficult to understand how to simply change the recordsource of a sub form. One of the tabs within my "NavigationSubform" is called "nbCustomerList", which has the target navigation name "CustomerList". Within the CustomerList form, there is a button which when clicked opens a popup which allows you to filter the query on CustomerList. How do I achieve a change to recordsource from an event like this?
Private Sub btnSearch_Click()
On Error GoTo HandleError
If CurrentProject.AllForms("MainMenu").IsLoaded Then
[Forms]![CustomerList].RecordSource = CustomerListFilter()
[Forms]![MainMenu]![NavigationSubform].Requery
End If
''ErrorHandling'''''''''''''''''''''''''''''''''''''''''''''''''''''''
HandleExit:
Exit Sub
HandleError:
MsgBox (Err.Number & ": " & Err.Description)
Resume HandleExit
End Sub
The following test worked for me:
Forms![Navigation Form].NavigationSubform.Form.RecordSource = "SELECT * FROM Rates WHERE ID=2"
Assuming your form design has the default names of [Navigation Form] and NavigationSubform assigned by Access, in your db try:
[Forms]![Navigation Form].NavigationSubform.Form.RecordSource = CustomerListFilter()
Requery command was not necessary.
I don't use Navigation Form design. Keep in mind that no matter how many tabs are set up, only one subform is available at any time. Nature of Navigation Form is that it loads and unloads subforms as called by click on tabs.

How to reference the users current textbox?

In access I am trying to not let the user exit a textbox unless they have filled it out (i.e. not left it null). This is to be done in a function in order to cut down on code. In VBA is there some way to stop the user from exiting the function? (I know about setting the focus but I need the code to work from a multitude of different textboxes)
For reference my current code is as follows;
Function Reload()
If IsNull(EmployeeID.Value) Or IsNull([First Name].Value) Or IsNull([Surname].Value) Or IsNull(DOB.Value) Or IsNull(Position.Value) Or IsNull(Position.Value) Or IsNull(Mobile.Value) Or IsNull(Email.Value) Or IsNull(Address.Value) Or IsNull(Suburb.Value) Or IsNull(Postcode.Value) Or IsNull([Start Date].Value) Or IsNull(UserLogin.Value) Or IsNull(UserPassword.Value) Then
MsgBox "Please fill out all fields"
Else
DoCmd.RunCommand acCmdSaveRecord
Form.Refresh
End If
End Function
Thanks
The best technique I know is to use the BeforeUpdate event to run data validation. Using your if statements if the data is not valid then set
Cancel = True
and the data will not write. You would probably prefer to disable the default record navigation and form close buttons and use custom buttons so that you can trap the invalid data error and prevent moving to a new record or closing the form. But even if you allow the user to use the built in navigation buttons or close button Access will throw up error messages about being unable to save the current record and prompt the user if he wants to proceed.
You can also go a step further and captue which fields are invalid and present a message box.
Msgbox ("The following fields were left blank and must be entered to save this record:" & vbCrLf & "Field 1" & vbCrLf & "Field 2")
A little modification of the text input limiter i use in our chat room should help. New function I have called it MustINPUT and call it on the LostFocus of the text boxes that you don't want to move from with out input.
Private Sub MyTextBox_LostFocus()
Call MustINPUT (Me.MyTextBox, 0)
End Sub
Just change the message for your users.
Sub MustINPUT(ctl As Control, iMaxLen As Integer)
'Call MustINPUT(Me.txtSayThis, 0)
If Len(ctl.Text) = iMaxLen Then
MsgBox "Please Type something", vbExclamation, "Input Needed"
ctl.Text = Left(ctl.Text, iMaxLen)
ctl.SelStart = iMaxLen
End If
End Sub
Hope this help. DR ,

Access 2013 - Save Value from Textbox and display again when form is opened after close

I want to save a value from textbox in a string for example and display it again when the form get's openend.
I have two textboxes PriceRangeOne and PriceRangeTwo .. The user enter here for example 20 and 40
The problem i have is that when the user switches between Form and Report the values in this textboxes are beeing deleted. How can i save them?
I tried adding a sourcecontrol to the fields but had name errors eventhough i used different names.
I tried adding this to on change and retrieve it in an onload
Dim eingabe As String = textBox1.Text or .Value
Still didn't worked. Does anyone know a way to do this?
Typically, the most efficient and reliable way to do this is to have some form auto-open when the database is opened. It could be a dashboard, or just some form with nothing else on it. Whatever you use, launch it when the database opens and then minimize it. Now you have a form that's always open, as long as the application is open. Add a couple of textboxes to this form/dashboard.
When you close your form referenced in this question, write the values of PriceRangeOne and PriceRangeTwo to the textboxes on the form I described above. Then, when you open a new form or report, you can reference the values in those textboxes. Since the form is always open, you can reference these values at any time from any form or report until you close your database.
Solved it with variables.
I declared global variables in my standart module
For example
Public PriceOne As Double
Public PriceTwo As Double
Than i did this in my form in Close() and Open():
Private Sub Form_Close()
PriceOne = Me.Field
PriceTwo = Me.FieldTwo
End Sub
Private Sub Form_Open(Cancel As Integer)
Me.Field = PriceOne
Me.FieldTwo = PriceTwo
End Sub
Works perfect!
Courtesy of How to save the last value of a textbox. | Access World Forums:
Private Sub Form_Close()
'Set the default value for textbox
DoCmd.SetWarnings False
DoCmd.RunSQL "UPDATE table SET table.field = [Forms]![FormName]![Textbox] " & vbCrLf & _
"WHERE (((table.ID)=1));"
DoCmd.SetWarnings True
End Sub
Private Sub Form_Load()
'Load the default value for textbox
Me.Textbox.Value = DLookup("[field]", "[table]", "[ID]=1")
End Sub