hi there everyone this sounds sily but i am trying to make a calendar which will show attendance on the calander

what i want is to stop on the todays date txtbox

txtboxes are arreys so the name is txtDay(0), txtDay(1), txtDay(2)........... txtDay(41)

    Dim strDate As String
    Dim strStartDate As String
    Dim strDayStop As String
    strDate = Format$(Now,"D")   'if todays date is july 7th 2015
    iFIRSTDAY = 1
    sSelectedMonth = Format$(Date,"MMMM")
    sSelectedYear = Formate$(Date,"YYYY")
    iStartIndex = (Weekday(sSelectedMonth & " " & iFIRSTDAY & ", " & sSelectedYear) - 1) 'July 1 2015 - so first day to mark
    strStartDate = iStartIndex
    strStartDate = Val(strStartDate) + Val(strDate)
    strDayStop = "txtDay(" & strStartDate & ")" 'this what i want to calculate 
    strDayStop.SetFocus 'this what i want to calculate

now as you can see in the code i want to find out which txtbox to get focus accourding to todays date

Sunday Monday Tuesday Wednesday Thursday Friday Saturday
txtDay(0) txtDay(1) txtDay(2) txtDay(3) txtDay(4) txtDay(5) txtDay(6)
txtDay(7) txtDay(8) txtDay(9) txtDay(10) txtDay(11) txtDay(12) txtDay(13)
txtDay(14) txtDay(15) txtDay(16) txtDay(17) txtDay(18) txtDay(19) txtDay(20)
txtDay(21) txtDay(22) txtDay(23) txtDay(24) txtDay(25) txtDay(26) txtDay(27)
txtDay(28) txtDay(29) txtDay(30) txtDay(31) txtDay(32) txtDay(33) txtDay(34)
txtDay(35) txtDay(36) txtDay(37) txtDay(38) txtDay(39) txtDay(40) txtDay(41)

as first day of this july is wednesday so txtDay(3) is the 1st july 2015 now as of today it is 7th july 2015 it should be highlighting or setfocus to txtDay(9).

    strDayStop = "txtDay(" & strStartDate & ")" 'this what i want to calculate 
    strDayStop.SetFocus 'this what i want to calculate

how can i get this result
Thank you in advance.
Asmat Ullah Khan

I'm assuming that you have this code in the Form_Load event? If so, you can't use SetFocus until the form itself is visible (Reference: Microsoft VB6 Documentation). One method to work around this is use a "sub Main" to show your form, then set all the relevant stuff afterward.

So, step by step:
1. Create a new module.
2. Create a new sub called "Main"
3. Put the following code in it (change names to suit your project)

Sub main()
Dim myForm As New Form1

myForm.Show

Dim strDate As String
Dim strStartDate As String
Dim strDayStop As String
strDate = Format$(Now, "D")
iFIRSTDAY = 1
For i = 0 To myForm.txtDay.Count - 1
    myForm.txtDay(i) = CStr(i)
Next i
sSelectedMonth = Format$(Date, "MMMM")
sSelectedYear = Format$(Date, "YYYY")
iStartIndex = (Weekday(sSelectedMonth & " " & iFIRSTDAY & ", " & sSelectedYear) - 1)
strStartDate = iStartIndex
strStartDate = Val(strStartDate) + Val(strDate)
myForm.txtDay(strStartDate).SetFocus

End Sub
  1. Go into the VB menu to Project/Properties and set the Startup Object drop-down to "Sub Main" and click "Okay"

You should now be able to run your form with focus set on the proper text box.

Hope this helps! Happy coding!

Edited 1 Year Ago by BitBlt

This article has been dead for over six months. Start a new discussion instead.