Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Making a Public Function without know the textbox name that has focus 2

Status
Not open for further replies.

waubain

Technical User
Dec 13, 2011
200
US
I am trying to create a Public Function unsuccessfully. I am converting parts of my database to allow barcode scanning into textboxes. Most companies that I receive bottles from the barcode matches the bottle number, but some have excess characters that I need to capture only a portion of the barcode. My form is Unbound and can add up to 12 records at a time (one case with 12 bottle each with a different barcode number). I added this code to the first bottle number textbox. This code works, but occurs 12 times on this form and about 30 times throughout the database, so I am trying to make a public function. I really don't know where to start since I don't know is how to capture the value of the current textbox without using its Name? The barcode scanner returns a carriage return after scanning and moves to the next field.

Code:
Private Sub txtUnit1_Exit(Cancel As Integer)

    Dim intStyle As Integer

    intStyle = Me.txtBCStyle   'autopopulates in form with a DLookup based on manufacturer combobox

    If Len(txtUnit1.Value) > 0 Then
        Select Case intStyle
        Case 1     'leaves barcode as scanned
            Exit Sub
        Case 2     'keeps right 6 characters
            txtUnit1.Value = Right(txtUnit1.Value, 6)
        End Select
    End If
End Sub

Any suggestions? Also suggestions for better code is also always appreciated.

You don't know what you don't know...
 
Code:
Private function commonProcedure()
    Dim intStyle As Integer
    dim ctrl as access.control
    set ctrl = screen.activecontrol
    intStyle = Me.txtBCStyle   'autopopulates in form with a DLookup based on manufacturer combobox

    If trim(ctrl & " ") = "" Then
        Select Case intStyle
        Case 1     'leaves barcode as scanned
            Exit Sub
        Case 2     'keeps right 6 characters
            ctrl = Right(ctrl, 6)
        End Select
    End If
End Sub
[/code}
Now select all the controls. In the exit event type the name of the procedure. =CommonProcedure()
 
Thanks MajP. I was needing a Public Function that I could call from any form. Taking your lead I created this Public Function:
Code:
Public Function CommonProcedure(frm As Form, ctrl As Access.Control, intStyle As Integer)
    Select Case intStyle
        Case 1     'leaves barcode as scanned
            Exit Function
        Case 2     'keeps right 6 characters
            ctrl = Right(ctrl.Value, 6)
    End Select
End Function

in each of the exit event I enter =BCStyle() which is the Function that I placed in each form where it is needed.

Code:
Private Function BCStyle()

    Dim intStyle As Integer
    Dim ctrl As Access.Control
    Dim frm As Form
    Set ctrl = Screen.ActiveControl
    Set frm = Screen.ActiveForm
    intStyle = Me.txtBCStyle
    
    If Trim(ctrl & " " <> "") Then
       Call CommonProcedure(frm, ctrl, intStyle)
    End If
End Function

I am ok with this, but is there a different way to skip the Private Function BCStyle and enter the Public Function directly in the Exit event in each control?
Thank you.

You don't know what you don't know...
 
Perhaps this ?
Code:
Public Function CommonProcedure()
Dim frm As Form, ctrl As Access.Control, intStyle As Integer
Set ctrl = Screen.ActiveControl
Set frm = Screen.ActiveForm
intStyle = frm.txtBCStyle
If Trim(ctrl & "") <> "" Then
    Select Case intStyle
        Case 1     'leaves barcode as scanned
            Exit Function
        Case 2     'keeps right 6 characters
            ctrl = Right(ctrl.Value, 6)
    End Select
End If
End Function

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Works perfert. Thanks ever so much.
Waubain

You don't know what you don't know...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top