Results 1 to 8 of 8
  1. #1
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    670

    Get Current Textbox Name And Previous Textbox Name

    I am attempting to create a re-usable function, but in order to do such I need a way to programmatically capture the current text box name (the one that called the


    Code:
     OnExit()
    function and the previous text box. I tried this syntax, but it does not display the text box names, did I use it incorrectly or is there a different way to do such?

    And just for further explenation, crackalackin is the current field so in my syntax I would expect that to equal
    Code:
    Screen.ActiveControl.Name

    Code:
    Private Sub Crackalackin_Exit(Cancel As Integer)
        FunctionUpdateCalcs(Screen.PreviousControl, Screen.ActiveControl.Name)
    End Sub
    
    
    FunctionUpdateCalcs(string pc, string ac)
        If ISNULL(ac.Value) Then
            'Do Nothing
        Else
            pc.Value = pc - ac
        End If
    End Function

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,521
    there is no current / previous text box. There is only 1 text box name. The record can change from current to previous.
    Is this what you mean? A form can only see the current record. It cannot see previous data.
    This can only be done via VB code.
    or
    do you mean textBox1 and textBox2 as current / previous?
    both are on the form , showing a record, but 2 different fields.

  3. #3
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    670
    Quote Originally Posted by ranman256 View Post
    there is no current / previous text box. There is only 1 text box name. The record can change from current to previous.
    Is this what you mean? A form can only see the current record. It cannot see previous data.
    This can only be done via VB code.
    or
    do you mean textBox1 and textBox2 as current / previous?
    both are on the form , showing a record, but 2 different fields.
    Yes, textBox1 and textBox2 are both on the same form and exist for the same record.

    For example, if you are on a form, and you press tab you are taken to textbox1, then you press tab again you are taken to textbox2.

    Does that help clarify?

  4. #4
    Bulzie is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    Nov 2015
    Posts
    1,463
    What are you trying to do? From what you listed seems like you may be doing it the hard way. Do you just want to subtract the values in those 2 text box fields?

  5. #5
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    670
    Quote Originally Posted by Bulzie View Post
    What are you trying to do? From what you listed seems like you may be doing it the hard way. Do you just want to subtract the values in those 2 text box fields?
    if textbox2 is not null then I want to update the value of textbox1 to
    Code:
    textbox1 = textbox1 - textbox2
    And I was attempting to use a re-usable function since I will need to do this for roughly 10 textboxes, rather than re-writing the code multiple times and just change the textbox names.

  6. #6
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,389
    Here's an example that works with 4 textboxes. Should work with 10 or more OK.

    Code:
    Option Compare Database
    Option Explicit
    Private Sub txtbox1_Exit(Cancel As Integer)
        On Error GoTo txtbox1_Exit_Error
        'On Error Resume Next
        Call FunctionUpdateCalcs(Screen.PreviousControl, Screen.ActiveControl)
        On Error GoTo 0
        Exit Sub
    txtbox1_Exit_Error:
        Select Case Err
            Case 2483
                Resume Next
            Case Else
                MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure txtbox1_Exit of Sub Form_frmCalcs"
        End Select
    End Sub
    Private Sub txtbox2_Exit(Cancel As Integer)
        On Error GoTo txtbox2_Exit_Error
        Call FunctionUpdateCalcs(Screen.PreviousControl, Screen.ActiveControl)
        On Error GoTo 0
        Exit Sub
    txtbox2_Exit_Error:
        Select Case Err
            Case 2483
                Resume Next
            Case Else
                MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure txtbox1_Exit of Sub Form_frmCalcs"
        End Select
    End Sub
    Private Sub txtbox3_Exit(Cancel As Integer)
        On Error GoTo txtbox3_Exit_Error
        Call FunctionUpdateCalcs(Screen.PreviousControl, Screen.ActiveControl)
        On Error GoTo 0
        Exit Sub
    txtbox3_Exit_Error:
        Select Case Err
            Case 2483
                Resume Next
            Case Else
                MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure txtbox1_Exit of Sub Form_frmCalcs"
        End Select
    End Sub
    Private Sub txtbox4_Exit(Cancel As Integer)
        On Error GoTo txtbox4_Exit_Error
        Call FunctionUpdateCalcs(Screen.PreviousControl, Screen.ActiveControl)
        On Error GoTo 0
        Exit Sub
    txtbox4_Exit_Error:
        Select Case Err
            Case 2483
                Resume Next
            Case Else
                MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure txtbox1_Exit of Sub Form_frmCalcs"
        End Select
    End Sub
    Private Sub txtbox5_Exit(Cancel As Integer)
        On Error GoTo txtbox5_Exit_Error
        Call FunctionUpdateCalcs(Screen.PreviousControl, Screen.ActiveControl)
        On Error GoTo 0
        Exit Sub
    txtbox5_Exit_Error:
        Select Case Err
            Case 2483
                Resume Next
            Case Else
                MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure txtbox1_Exit of Sub Form_frmCalcs"
        End Select
    End Sub
    Public Function FunctionUpdateCalcs(pctl As Control, actl As Control)
        'Debug.Print pctl.Name, actl.Name
        If IsNull(actl.Value) Then
            'Do Nothing
        Else
                pctl.Value = pctl - actl
        End If
    End Function
    Added error handling. When form is first initialized, there is NO previous control, resulting in an error.
    Last edited by davegri; 10-04-2016 at 10:39 PM. Reason: clarity, added some error handling,

  7. #7
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,521
    Use a 3rd text box to do this calculation.

  8. #8
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    670
    Quote Originally Posted by ranman256 View Post
    Use a 3rd text box to do this calculation.
    Using a 3rd text box I would need this text box to have the exact location as textbox1. And would need to change visibility. The only problem I would have would be either 1 adding the 3rd text box by the designer and aligning perfectly with textbox1 OR (and maybe this is the more complicated route) using VBA to add textbox3 to the exact location of textbox1 and (here is the easy part) assigning it the calculation value.

    Something like
    Code:
    If ISNULL(textbox1.Value) Then
       'Do Nothing
    Else
      textbox1.Visble = false;
      textbox3.Visible = true;
      textbox3.Value = textbox1 - textbox2
    End If

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 2
    Last Post: 06-15-2016, 01:32 PM
  2. Replies: 2
    Last Post: 04-29-2016, 09:06 PM
  3. Replies: 2
    Last Post: 12-22-2015, 09:09 PM
  4. Replies: 8
    Last Post: 11-19-2012, 09:22 AM
  5. Set textbox field to depend on current date
    By lilanngel in forum Queries
    Replies: 6
    Last Post: 03-10-2011, 08:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums