Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with "me"

Status
Not open for further replies.

slugo

Programmer
Dec 16, 2001
15
US
Hi All,

I seem to be having some fits of stupidity. I'm trying to populate the calendar dates. So I have created many text boxes each with the naming convention of txtD0...txtD31 etc.. I wish to go through with a for loop and put the dates in them corresponding with the right date and day. To do this I have:

Me."txtD" & curDay = corresponding date etc...

This doesn't work of course. How might i go about doing this. Thanks in advance.
 
Hi slugo, have you considered using a control array of
text boxes?
Control arrays are very good when you want to perform the same operation on many controls.

 
As nonguru said, a control array is the best way to do this. Just check that the index property matches the date

But to answer your question directly, the code would be something like

Dim ctlControl as Control, strDate as string

For each ctlControl in Me.Controls
If TypeOf CtlControl is textbox then
strdate = right(ctlcontrol.name ,2)
ctlcontrol.text = strdate
end if
next

But I don't really recommend it. You'll have other text boxes as well on your form which you don't want to treat this way. One way to still work this is to use the tag property set to "Date" or something for text boxes which you want to treat this way, but believe me, this does get very messy

Mark
 
As the previous suggestions have said, consider using a control array.

However, if you really want to do it the way you have suggested:
[tt]
Option Explicit

Private Sub Command1_Click()
' Example call that locates a TextBox called Text1
RefTextByName("Text1").Text = Now()
End Sub


Private Function RefTextByName(strControl As String) As TextBox
Dim tmpTextBox As Control

For Each tmpTextBox In Form1.Controls
If tmpTextBox.Name = strControl Then
Set RefTextByName = tmpTextBox
Exit Function
End If
Next

End Function
 
OK, I'll bite...

Whats wrong with: [tt]
Debug.Print Form1.Controls("Text1").Text[/tt]
?

Seems it should beat the heck out of a for each loop.

It also permits [tt]
Me.Controls("txtD" & curDay).Text = corresponding date etc...[/tt]



Wil Mead
wmead@optonline.net

 
Thanks guys. I used the control array and was finished in about 5 minutes. Thanks for the help. I should really eat my wheaties in the morning :).
 
Wil,

Nothing is wrong with your versions.I was just building on previous answers...
 
Strongm, can you tell I trust you?


Wil Mead
wmead@optonline.net

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top