PDA

View Full Version : [SOLVED] Textbox to regain focus



oldman
10-04-2013, 09:22 AM
This is a userform textbox designated for month of delivery. The user must enter two digits (01 through 12) in order to proceed to the next textbox for day of delivery. This is a bit tricky for me.




Private Sub txtdpm_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
If Len(Me.txtdpm.Value) < 2 Then
MsgBox "Two Digits Required"

txtdpm = ""
txtdpm.SetFocus

End If
End Sub

Kenneth Hobs
10-04-2013, 11:11 AM
Use a combobox and avoid those issues.

oldman
10-04-2013, 01:06 PM
Mr Hobs!!!!

This had ocurred once in the past and the remedy was the same. Good Lord I should have remembered.

Thank you.

Kenneth Hobs
10-04-2013, 01:14 PM
Private Sub UserForm_Initialize()
Dim i As Integer
For i = 1 To 12
ComboBox1.AddItem Format(i, "00"), i - 1
Next i
End Sub

oldman
10-04-2013, 01:30 PM
That saves me much time. Thank you.

snb
10-05-2013, 04:44 AM
I'd prefer:


Private Sub UserForm_Initialize()
combobox1.list=[row(1:12)]
End Sub


Especially for you I created:

http://www.snb-vba.eu/VBA_Fill_combobox_listbox_en.html

oldman
10-05-2013, 07:26 AM
Kenneth:

I used the script as you recommended for month and calendar date but I cannot get it to work for year.

oldman
10-05-2013, 07:27 AM
How does your solution differ from that of Kennenth Hobs? His formats the months for two characters.

oldman
10-05-2013, 07:31 AM
I saved your wedsite to my favorites bars. Most excellent! Thank you

snb
10-05-2013, 08:10 AM
If you want 01,02, etc.


Private Sub UserForm_Initialize()
combobox1.list=[text(row(1:12),"00")]
End Sub

Additem hasn't been designed to populate a list.
.List has.

oldman
10-05-2013, 08:19 AM
If you want 01,02, etc.


Private Sub UserForm_Initialize()
combobox1.list=[text(row(1:12),"00")]
End Sub

Additem hasn't been designed to populate a list.
.List has.


I do recall reading that "additem" is not the preferred method of populating a combobox and using "list" has far less issues. Correct?

Can I use this for calendar date and year as well assuming I modify the script accordingly?

snb
10-05-2013, 09:33 AM
dates:


Private Sub UserForm_Initialize()
combobox2.list=[text(row(1:31),"00")]
End Sub

years:


Private Sub UserForm_Initialize()
combobox3.list=[row(2013:20120)]
End Sub

oldman
10-05-2013, 09:51 AM
Thank you my friend

oldman
10-05-2013, 12:26 PM
Invalid property array index error.