VBA Code for Input Masks

  • Thread starter Thread starter Glenn
  • Start date Start date
G

Glenn

Here is the scenario:

I want to change the Input Mask on a textbox every time a
new category is chosen in a combobox. This mask is an
asset tracking number where all I want the user to enter
is the number of the product.

example - Asset Number: BN-0000-COM
User would enter the number - 0023
The data entered into the database would be: BN-0023-COM

I can create an inputmask on the form in the textbox, but
I don't want to create 40 different textboxes to be
displayed when a certain option is selected.

Does anyone know how to do the above in VBA code so that
all I have is a bunch of IF statements under the
Combobox????

Thanx for your time,

Glenn
 
Try using a SelectCase to set the mask based on the
selection:

Private Sub cboMyCombo_AfterUpdate()

' Verify a value has been entered
' and stop the code if it hasn't
If IsNull([cboMyCombo]) Or [cboMyCombo]="" Then
Exit Sub
End If

' Otherwise...

Dim strMask as String

' set the mask based on the selection
Select Case cboMyCombo
Case "Asset Number"
strMask = "BN-0000-COM"
Case "Employee Number"
strMask = "E000000"
Case "Next Selections"
strMask = "AppropriateMasks"
End Select

' Apply mask to TextBox
txtMyTextBox.InputMask = strMask

End Sub

Haven't tested this code but it should work ... or steer
you in the right direction if it doesn't.

Hope this helps!

Howard Brody
 
Thanx for the help.

Just had to make a few modifications(Outlined Below) for
it to work!!

Thanx again for the help


' Updated Code
Dim strMask As String

' set the mask based on the selection
Select Case cboCategory
Case "Desktop Computer"
' "BN-"0000"-COM";0;_
strMask = "BN-""####""-COM"";0"";_"
Case "Monitor"
strMask = "BN-""####""-MON"";0"";_"
Case "PDA"
strMask = "BN-""####""-PDA"";0"";_"

End Select

' Apply mask to TextBox

Asset_Number.InputMask = strMask
 
Please disregard my last post - your code does get me in
the right track, but not completly...

I have the following problem:

This string is used by the InputMask Wizard to create a
useable mask - "BN-"0000"-COM";0;_

It is broken down into the following components

"BN-"0000"-COM"; - Input String
0; - Characters in string stored with input
_ - placeholder for input (The 0's)

The current code inputs the following code:
""BN-"0000\-C"OM""

How do I get the two codes to look the same? Do I use
seperate strings for each part of the code and then mesh
them together?
 
Funny how you think things through on a newsgroup in one
of your replies when the answer comes to ya...

Anyway I found may way around it...

Here is the solution

strMask = "BN-0000""-COM"";0;_"


The mask works like a charm - Now onto the coding of all
the Scenario's

Thanx for the help

Glenn
 
Back
Top