How to reference the users current textbox? - vba

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 ,

Related

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.

Subform OnCurrent Error on Main Form Open

I have the following code on a subform's on current, but it errors on open of the main form:
If IsNull([Forms]![frmMContacts]!ID) Then
With Forms!frmMContacts!frmMContacts_SubPeopleContacts.Form
.Enabled = False
End With
Else
With Forms!frmMContacts!frmMContacts_SubPeopleContacts.Form
.Enabled = True
End With
If Me.NewRecord Then
Me.txtCurrRec = "New Contact Role Record"
Else
Me.txtCurrRec = CStr(Me.CurrentRecord) & " of " & _
DCount("ID", "tblContactPeople", "FKClient = " & Me.Parent.CM_CID) & " Contact Roles"
End If
End If
The point of it is, simply to update a text box in the form's footer, to provide a count of records for the sub form. The issue is that when a user opens the main (parent) form, from the main menu, the main record has no record, and the sub form is still loading. In reality, I'm hiding the detail of the main form, and hence this sub form is hidden. The user has to hit 1 of 2 buttons in the main form header. Either find a record (pop-up form) or start a new one (another pop-up form).
When I try to open this main form, I get the error "run-time error '2465' application-defined or object-defined error".
I'm not sure how to trap this and just either make the sub-form disabled or prevent this on-current code form firing, until the parent has a record.
Thanks for any help!

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

Microsoft Access applying 1 function to all fields automatically

I have a form that keeps track of assigned patient equipment. I have it set so that any changes made to text fields on the form automatically move down to the "comments" section of the form (this is done so that any changes made are documented in case the user forgets to manually document changes). I have a sub that I wrote that accomplishes this that I am currently calling for every single text field. This works but is messy.
Is there a way to apply the sub to all the fields in one procedure without calling it for every individual field? Code is below, please let me know if I can clarify anything.
Private Sub pPEMoveValue(sField)
'Moves the old field value down to the comments section automatically
Dim sOrigValue As String
Dim sCommentValue As String
sOrigValue = sField
sCommentValue = Nz(mPEComments, "")
Me.mPEComments = sCommentValue & vbNewLine & sOrigValue
End Sub
Private Sub sPEBatCharger_Dirty(Cancel As Integer)
pPEMoveValue (Nz(Me.sPEBatCharger.OldValue, ""))
End Sub
This is the solution I came up with to do what you are looking to do. I took advantage of the MS Access Tag system. You can add tags to your controls so you can sort of "Group" them.
First put the form in design view and adjust the tag for all of the fields you want to record to say "Notes".
Then in the Form's BeforeUpdate even you would add this:
Private Sub Form_BeforeUpdate(Cancel As Integer)
Call FindControlsForComments(Me.Form)
End Sub
Then you would use this function to find any fields that have the "Notes" tag and run it through the function you created:
Public Function FindControlsForComments(frm As Form)
Dim ctrl As Access.Control
For Each ctrl In frm
'If the control is tagged for notes
If ctrl.Tag = "Notes" Then
'If the old value is different than the current value
If Nz(ctrl.OldValue, "") <> Nz(ctrl.Value, "") Then
'Add to comment
Call pPEMoveValue(Nz(ctrl.Value, ""))
End If
End If
Next ctrl
End Function
You may have to adjusted this slightly to work with your system but this has worked well for me.

How to Set Focus in Access Continuous Form (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.