excel vba: condensing with select case

jedwardo

Board Regular
Joined
Aug 21, 2012
Messages
122
Hi Everyone, I'm trying to simplify this code but am not sure how to go about it or where to put it. I thought maybe a select case in userform_click event but am not having any luck.

These all just call an on screen keyboard when textbox is selected and set the variants for returning the data from the keyboard to the userform textboxes.

Code:
Private Sub TextBoxAddress_Enter()
    tPar = ActiveControl.Parent
    tField = ActiveControl.Name
    UFKeyboard.Show
End Sub


Private Sub TextBoxAddressS_Enter()
    tPar = ActiveControl.Parent
    tField = ActiveControl.Name
    UFKeyboard.Show
End Sub


Private Sub TextBoxCellPhone_Enter()
    tPar = ActiveControl.Parent
    tField = ActiveControl.Name
    UFKeyboard.Show
End Sub


Private Sub TextBoxCellPhoneS_Enter()
    tPar = ActiveControl.Parent
    tField = ActiveControl.Name
    UFKeyboard.Show
End Sub


Private Sub TextBoxCity_Enter()
    tPar = ActiveControl.Parent
    tField = ActiveControl.Name
    UFKeyboard.Show
End Sub


Private Sub TextBoxCityS_Enter()
    tPar = ActiveControl.Parent
    tField = ActiveControl.Name
    UFKeyboard.Show
End Sub


Private Sub TextBoxEmail_Enter()
    tPar = ActiveControl.Parent
    tField = ActiveControl.Name
    UFKeyboard.Show
End Sub


Private Sub TextBoxEmailS_Enter()
    tPar = ActiveControl.Parent
    tField = ActiveControl.Name
    UFKeyboard.Show
End Sub


Private Sub TextBoxFirstName_Enter()
    tPar = ActiveControl.Parent
    tField = ActiveControl.Name
    UFKeyboard.Show
End Sub


Private Sub TextBoxFirstNameS_Enter()
    tPar = ActiveControl.Parent
    tField = ActiveControl.Name
    UFKeyboard.Show
End Sub


Private Sub TextBoxHomePhone_Enter()
    tPar = ActiveControl.Parent
    tField = ActiveControl.Name
    UFKeyboard.Show
End Sub


Private Sub TextBoxHomePhoneS_Enter()
    tPar = ActiveControl.Parent
    tField = ActiveControl.Name
    UFKeyboard.Show
End Sub


Private Sub TextBoxLastName_Enter()
    tPar = ActiveControl.Parent
    tField = ActiveControl.Name
    UFKeyboard.Show
End Sub


Private Sub TextBoxLastNameS_Enter()
    tPar = ActiveControl.Parent
    tField = ActiveControl.Name
    UFKeyboard.Show
End Sub


Private Sub TextBoxSecondEmail_Enter()
    tPar = ActiveControl.Parent
    tField = ActiveControl.Name
    UFKeyboard.Show
End Sub


Private Sub TextBoxSecondEmailS_Enter()
    tPar = ActiveControl.Parent
    tField = ActiveControl.Name
    UFKeyboard.Show
End Sub


Private Sub TextBoxState_Enter()
    tPar = ActiveControl.Parent
    tField = ActiveControl.Name
    UFKeyboard.Show
End Sub


Private Sub TextBoxStateS_Enter()
    tPar = ActiveControl.Parent
    tField = ActiveControl.Name
    UFKeyboard.Show
End Sub


Private Sub TextBoxWorkPhone_Enter()
    tPar = ActiveControl.Parent
    tField = ActiveControl.Name
    UFKeyboard.Show
End Sub


Private Sub TextBoxWorkPhoneS_Enter()
    tPar = ActiveControl.Parent
    tField = ActiveControl.Name
    UFKeyboard.Show
End Sub


Private Sub TextBoxZip_Enter()
    tPar = ActiveControl.Parent
    tField = ActiveControl.Name
    UFKeyboard.Show
End Sub


Private Sub TextBoxZipS_Enter()
    tPar = ActiveControl.Parent
    tField = ActiveControl.Name
    UFKeyboard.Show
End Sub

It's working just fine and I'd leave it as is but there are a lot more textboxes to add into the mix once I find a solution and the code page is already quite long.

Thanks, Jordan
 
I was toying around with this code to try and group them together. Unfortunately it works fine for a Change event but does not let me modify it as an Enter event to call the keyboard. Do you know if there's something similar that can be done with Enter events for a collection?

On Userform:
Code:
Private m_colTBoxes As Collection


Private Sub UserForm_Initialize()


    Dim lngIndex As Long
    Dim clsTBox As Class1
    
    Set m_colTBoxes = New Collection
    For lngIndex = 1 To 3
        Set clsTBox = New Class1
        Set clsTBox.TBox = Me.Controls("Textbox" & lngIndex)
        m_colTBoxes.Add clsTBox, CStr(m_colTBoxes.Count + 1)
    Next
    
End Sub

And this in the Class Modules:
Code:
Public WithEvents TBox As MSForms.TextBox


Private Sub TBox_Change()
    MsgBox "Validate " & Me.TBox.Name
End Sub
 
Upvote 0
I was toying around with this code to try and group them together. Unfortunately it works fine for a Change event but does not let me modify it as an Enter event to call the keyboard. Do you know if there's something similar that can be done with Enter events for a collection?

That was what I had in mind when I asked you about the functionality applying to all TextBoxes... and I too found that the Enter event could not be Class'ed, which I really do not understand as it is a normal, legitimate event for a TextBox.
 
Upvote 0
No problem, thanks for looking into it. It's functioning fine I'm just having fun with it now.
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top