View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Gareth[_6_] Gareth[_6_] is offline
external usenet poster
 
Posts: 158
Default Entering a date in a textbox

Hi,

Firstly, to select all of the text in your textbox at start up, place
the following code in your userform's initialize procedure (modify as
necessary:

Private Sub UserForm_Initialize()
'copy from here
TextBox1.SelStart = 0
TextBox1.SelLength = Len(TextBox1.Text)
'end copy here
End Sub

Then, to effect some type of mask you could use either:

Private Sub TextBox1_Change()
Select Case Len(TextBox1.Text)
Case 2, 5
TextBox1.Text = TextBox1.Text & "/"
End Select
End Sub

which just inserts the slashes as you type. Alternatively, you could do
something like:

Private Sub TextBox1_Change()
Dim iPos As Integer
Const myMask As String = "--/--/----"

If Len(TextBox1.Text) = 1 Then
TextBox1.Text = TextBox1.Text & myMask
Exit Sub
End If

iPos = InStr(TextBox1.Text, "-")
If iPos = 0 Then Exit Sub
TextBox1.Text = Left$(TextBox1.Text, iPos - 1) _
& Mid(myMask, iPos)

iPos = InStr(TextBox1.Text, "-")
If iPos = 0 Then Exit Sub
TextBox1.SelStart = InStr(TextBox1.Text, "-") - 1

End Sub

which crudely writes a mask into the textbox.

HTH,

Gareth


johncassell wrote:
Hi,

I have a textbox which is linked to a cell on my spreadsheet. Lets say
cell A1 is "25/04/2005". When I open my form, the textbox will display
"25/04/2005" and the cursor will be sat to the right of the date.

I would like the cursor to start at the left so I don't have to key
back to the first number and then I would like my numbers to be
separated with slashes / .

At the moment the cursor is on the right, so I have to key back accross
and if i type 26052006 it just displays 2605200625/04/2005. When i start
to type the new date I would like my numbers to just override the
current numbers but also retain the "##/##/####" format without me
having to type in the /'s.

Please can anyone help??