May 24th, 2005, 10:10 PM
#81
Re: How to restrict users to enter first character as alphabet and rest as numeric.
I don't think seraphicmortal fully explored the class I uploaded. Since that allows you to change the popup menu that is used when you right click the textbox you can either remove the Paste command or implement your own Paste procedure in which you of course have full control of the Paste procedure. That means you can examine the clipboard content before you insert it and edit the content so you don't paste any illegal characters.
May 24th, 2005, 10:10 PM
#82
Re: How to restrict users to enter first character as alphabet and rest as numeric.
Sometimes I like annoying ppl }
Ok, this is better then
VB Code:
Private Sub Text1_Change()
Text1.Text = TestFormat(Text1.Text)
Text1.SelStart = Len(Text1.Text)
End Sub
Private Function TestFormat(ByRef Text As String) As String
Dim sAlpha As String
Dim lX As Long
Dim lMaxLen As Long
If Len(Text) = 0 Then Exit Function
lMaxLen = 4 ' or how ever long you want it to be
sAlpha = "abcdefghijklmnopqrstuvwxyz"
Do While InStr(1, sAlpha, Left$(Text, 1), vbTextCompare) = 0
Text = Mid$(Text, 2)
If Len(Text) = 0 Then Exit Function
Loop
For lX = 2 To Len(Text)
If Not IsNumeric(Mid$(Text, lX, 1)) Then
Mid$(Text, lX, 1) = Chr$(0)
End If
Next
Text = Replace$(Text, Chr$(0), vbNullString)
If Len(Text) > lMaxLen Then
Text = Left$(Text, lMaxLen)
End If
TestFormat = UCase$(Text)
End Function
Last edited by longwolf; May 24th, 2005 at 10:25 PM .
May 24th, 2005, 10:24 PM
#83
Re: How to restrict users to enter first character as alphabet and rest as numeric.
Well the thread started has marked it as resolved so we could just stop posting . BTW baja_yu, thanks for the rating.
May 24th, 2005, 11:07 PM
#84
Thread Starter
Member
Re: How to restrict users to enter first character as alphabet and rest as numeric.
Hey Joacim, I'm still gona try ur suggestion though!! Thanks for the inputs. =)
Yoroshiku,
seraphicmortal
______________________________________________________
Thirst for knowledge can never be quenched...Ask for more!!.
Oh! Please..Show your appreciation by clicking
if you deem this post
helpful.
Rate this Post!
May 25th, 2005, 09:37 AM
#85
Re: How to restrict users to enter first character as alphabet and rest as numeric.
@seraphicmortal: I got your PM but I don't understand what it is that gives you the error. Are you trying to assign a menu to the class?
May 25th, 2005, 07:31 PM
#86
Thread Starter
Member
Re: How to restrict users to enter first character as alphabet and rest as numeric.
I just pasted ur code to my program and change txtbox to the name of my textbox and placed the following in my global.bas :
VB Code:
Public mnuContext As New txtbox
Public mErrorColor As Long
then in my frm_field(form where my textbox are placed):
VB Code:
Private Sub txt_qty_Change()
' Call Validate_Entry(txt_qty)
Dim nSelStart As Long, nSelLen As Long
If Len(txt_qty.Text) Then
If (UCase$(txt_qty.Text) Like "[A-Z]" & String(Len(txt_qty.Text) - 1, "#")) Then
If UCase$(txt_qty.Text) <> txt_qty.Text Then
nSelStart = txt_qty.SelStart
nSelLen = txt_qty.SelLength
txt_qty.Text = UCase$(txt_qty.Text)
txt_qty.SelStart = nSelStart
txt_qty.SelLength = nSelLen
End If
txt_qty.BackColor = vbWindowBackground
Else
txt_qty.BackColor = mErrorColor
End If
Else
txt_qty.BackColor = vbWindowBackground
End If
End Sub
Private Sub txt_qty_KeyPress(KeyAscii As Integer)
Dim nSelLen As Long
nSelLen = txt_qty.SelLength
If txt_qty.SelStart = 0 And txt_qty.SelLength = 0 Then
'This will ensure that the first character is replaced if
'the user has moved the text caret to the beginning of the text
txt_qty.SelLength = 1
End If
KeyAscii = Asc(UCase$(Chr$(KeyAscii))) 'capitilize
Select Case KeyAscii
Case vbKeyBack
txt_qty.SelLength = nSelLen
Case vbKeyA To vbKeyZ
If txt_qty.SelStart <> 0 Then
KeyAscii = 0
txt_qty.SelLength = nSelLen
End If
Case vbKey0 To vbKey9
If txt_qty.SelStart = 0 Then
KeyAscii = 0
txt_qty.SelLength = nSelLen
End If
Case Else
KeyAscii = 0
txt_qty.SelLength = nSelLen
End Select
End Sub
Private Sub txt_qty_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Not mnuContext Is Nothing Then
If Button And vbRightButton Then
'surpress the default context menu and show our custom menu instead
txt_qty.Enabled = False
txt_qty.Enabled = True
txt_qty.Parent.PopupMenu mnuContext
End If
End If
End Sub
then in my class(txtbox.cls):
VB Code:
Option Explicit
Private WithEvents txtbox As TextBox
Private mnuContext As Menu
Private mErrorColor As Long
Public Sub Init(TextBox As TextBox, _
Optional ErrorColor As Long = vbRed, _
Optional ContextMenu As Menu = Nothing)
Set mnuContext = ContextMenu
m_ErrorColor = ErrorColor
Set txtbox = TextBox
End Sub
Public Property Get ErrorColor() As Long
ErrorColor = mErrorColor
End Property
Public Property Let ErrorColor(nNew As Long)
mErrorColor = nNew
End Property
Public Property Set TextBox(txt As TextBox)
Set txtbox = txt
End Property
Public Property Get TextBox() As TextBox
Set TextBox = txtbox
End Property
Public Property Set ContextMenu(mnu As Menu)
Set mnuContext = mnu
End Property
Public Property Get ContextMenu() As Menu
Set ContextMenu = mnuContext
End Property
Private Sub Class_Initialize()
mErrorColor = vbRed
End Sub
This is what I did(where I pasted your codes..). Can you tell me why it still had errors?
Thank you so much.
Yoroshiku,
seraphicmortal
______________________________________________________
Thirst for knowledge can never be quenched...Ask for more!!.
Oh! Please..Show your appreciation by clicking
if you deem this post
helpful.
Rate this Post!
May 25th, 2005, 07:37 PM
#87
Re: How to restrict users to enter first character as alphabet and rest as numeric.
This goes into frm_Field:
VB Code:
Option Explicit
Private oValidateText As CTextFormat
Private Sub Form_Load()
Set oValidateText = New CTextFormat
Set oValidateText.TextBox = Text1
Call oValidateText.Init(Text1, vbYellow, mnuPopUp)
End Sub
Now, just Add the Class to your project (the attached file from page 2)
Bruce.
Last edited by Bruce Fox; May 25th, 2005 at 07:44 PM .
May 25th, 2005, 09:02 PM
#88
Re: How to restrict users to enter first character as alphabet and rest as numeric.
Form code (one textbox called oValidateText is on it):
VB Code:
Option Explicit
Private oValidateText As CTextFormat
Private Sub Form_Load()
Set oValidateText = New CTextFormat
Set oValidateText.TextBox = Text1
Call oValidateText.Init(Text1, vbYellow, mnuPopUp)
End Sub
Class module code (class is called CTextFormat):
VB Code:
Option Explicit
Private WithEvents txtBox As TextBox
Private mnuContext As Menu
Private m_ErrorColor As Long
Public Sub Init(TextBox As TextBox, Optional ErrorColor As Long = vbRed, Optional ContextMenu As Menu = Nothing)
Set mnuContext = ContextMenu
m_ErrorColor = ErrorColor
Set txtBox = TextBox
End Sub
Public Property Get ErrorColor() As Long
ErrorColor = m_ErrorColor
End Property
Public Property Let ErrorColor(nNew As Long)
m_ErrorColor = nNew
End Property
Public Property Set TextBox(txt As TextBox)
Set txtBox = txt
End Property
Public Property Get TextBox() As TextBox
Set txtBox = txtBox
End Property
Public Property Set ContextMenu(mnu As Menu)
Set mnuContext = mnu
End Property
Public Property Get ContextMenu() As Menu
Set ContextMenu = mnuContext
End Property
Private Sub Class_Initialize()
m_ErrorColor = vbRed
End Sub
Private Sub txtBox_Change()
Dim nSelStart As Long, nSelLen As Long
If Len(txtBox.Text) Then
If (UCase$(txtBox.Text) Like "[A-Z]" & String(Len(txtBox.Text) - 1, "#")) Then
If UCase$(txtBox.Text) <> txtBox.Text Then
nSelStart = txtBox.SelStart
nSelLen = txtBox.SelLength
txtBox.Text = UCase$(txtBox.Text)
txtBox.SelStart = nSelStart
txtBox.SelLength = nSelLen
End If
txtBox.BackColor = vbWindowBackground
Else
txtBox.BackColor = m_ErrorColor
End If
Else
txtBox.BackColor = vbWindowBackground
End If
End Sub
Private Sub txtBox_KeyPress(KeyAscii As Integer)
Dim nSelLen As Long
nSelLen = txtBox.SelLength
If txtBox.SelStart = 0 And txtBox.SelLength = 0 Then
'This will ensure that the first character is replaced if
'the user has moved the text caret to the beginning of the text
txtBox.SelLength = 1
End If
KeyAscii = Asc(UCase$(Chr$(KeyAscii))) 'capitilize
Select Case KeyAscii
Case vbKeyBack
txtBox.SelLength = nSelLen
Case vbKeyA To vbKeyZ
If txtBox.SelStart <> 0 Then
KeyAscii = 0
txtBox.SelLength = nSelLen
Else
KeyAscii = Asc(UCase$(Chr$(KeyAscii))) 'capitilize
End If
Case vbKey0 To vbKey9
If txtBox.SelStart = 0 Then
KeyAscii = 0
txtBox.SelLength = nSelLen
End If
Case Else
KeyAscii = 0
txtBox.SelLength = nSelLen
End Select
End Sub
Private Sub txtBox_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Not mnuContext Is Nothing Then
If Button And vbRightButton Then
'surpress the default context menu and show our custom menu instead
txtBox.Enabled = False
txtBox.Enabled = True
txtBox.Parent.PopupMenu mnuContext
End If
End If
End Sub
May 25th, 2005, 09:51 PM
#89
Hyperactive Member
Re: How to restrict users to enter first character as alphabet and rest as numeric.
VB Code:
Public MyChar
Function validity()
If Len(Text1.Text) = 1 Then
If InStr(MyChar, Left(Text1.Text, 1)) = 0 Then
'KeyAscii = 0
MsgBox "Invalid format"
Exit Function
End If
Exit Function
End If
On Error GoTo kulitag
If Not IsNumeric(Right(Text1.Text, Len(Text1.Text) - 1)) Then
MsgBox "Invalid format"
KeyAscii = 0
End If
kulitag:
End Function
Private Sub Form_Load()
MyChar = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
End Sub
Private Sub Text1_Change()
validity
End Sub
just wnt to try!
May 25th, 2005, 11:25 PM
#90
Thread Starter
Member
Re: How to restrict users to enter first character as alphabet and rest as numeric.
Ugghhh... I think I'll just use the code of Bruce Fox instead of using this method, w/c uses class....it just isn't working (maybe it's my fault...I'm just a newbie)....
Thank you so much Baja for answering my questions.. =)
Yoroshiku,
seraphicmortal
______________________________________________________
Thirst for knowledge can never be quenched...Ask for more!!.
Oh! Please..Show your appreciation by clicking
if you deem this post
helpful.
Rate this Post!
May 25th, 2005, 11:37 PM
#91
Hyperactive Member
Re: How to restrict users to enter first character as alphabet and rest as numeric.
ei sera have you tried mine?
May 25th, 2005, 11:42 PM
#92
Re: How to restrict users to enter first character as alphabet and rest as numeric.
Originally Posted by
seraphicmortal
Ugghhh... ....it just isn't working (maybe it's my fault...I'm just a newbie)....
Persist with it it's worth it.
Start a new Project, in Form1 place the code identified, and 'Add' a Class Module and paste that code in (ensure the Class Module name is cTextFormat , now add a TextBox.
Thats it
Edit: Rem out: "Call oValidateText.Init(Text1, vbYellow, mnuPopUp)" just to test it (as you wount have the Menu).
Bruce.
May 25th, 2005, 11:56 PM
#93
Thread Starter
Member
Re: How to restrict users to enter first character as alphabet and rest as numeric.
ooki dokie...will do as told Bruce! As for Kulitag, I think there's a bit error. Thanks for trying anyways.
Yoroshiku,
seraphicmortal
______________________________________________________
Thirst for knowledge can never be quenched...Ask for more!!.
Oh! Please..Show your appreciation by clicking
if you deem this post
helpful.
Rate this Post!
May 26th, 2005, 12:07 AM
#94
Thread Starter
Member
Re: How to restrict users to enter first character as alphabet and rest as numeric.
Originally posted by Bruce Fox
Edit: Rem out: "
Call oValidateText.Init(Text1, vbYellow, mnuPopUp)" just to test it (as you wount have the Menu).
Just did what you and Baja said.
The ones in red is the one having error....same thing happened when I pasted it in my program and when I made a new project.
mnuPopUp is variable not defined.
Yoroshiku,
seraphicmortal
______________________________________________________
Thirst for knowledge can never be quenched...Ask for more!!.
Oh! Please..Show your appreciation by clicking
if you deem this post
helpful.
Rate this Post!
May 26th, 2005, 10:17 AM
#95
Re: How to restrict users to enter first character as alphabet and rest as numeric.
Originally Posted by
seraphicmortal
Just did what you and Baja said.
The ones in red is the one having error....same thing happened when I pasted it in my program and when I made a new project.
mnuPopUp is variable not defined.
mnuPopUp would be a menu item that you want to have 'popup' when the user clicks(or right-click) an item on your form.
Check the VB help files for 'PopupMenu'
May 26th, 2005, 10:19 AM
#96
Re: How to restrict users to enter first character as alphabet and rest as numeric.
Originally Posted by
seraphicmortal
Just did what you and Baja said.
The ones in red is the one having error....same thing happened when I pasted it in my program and when I made a new project.
mnuPopUp is variable not defined.
To be able to assign a menu you have to create one. Start the menu editor and create a menu call mnuPopUp and create some sub menu items. Look at the attached Form for an example. If you don't want to assign a custom popup menu just call it like this instead:
VB Code:
Call oValidateText.Init(Text1)
Or you could use this code which does the same thing.
VB Code:
Set oValidateText.TextBox = Text1
Since you for some reason couldn't download ZIP files I've attached a form and the class. Create a new project and remove the empty form that is created. Now add this form and class instead.
Attached Files
May 26th, 2005, 01:14 PM
#97
Lively Member
Re: How to restrict users to enter first character as alphabet and rest as numeric.
cvmicheal has embarrassed me to no end... thx cv as this one liner has saved me carpel tunnel.
can i retract my reply?
May 26th, 2005, 03:22 PM
#98
Re: How to restrict users to enter first character as alphabet and rest as numeric.
What reply?
May 26th, 2005, 06:26 PM
#99
Thread Starter
Member
May 26th, 2005, 06:48 PM
#100
Re: How to restrict users to enter first character as alphabet and rest as numeric.
In that case you are (finally) welcome
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Click Here to Expand Forum to Full Width