Hi,

I have 2 text box, Text1 is to display the week no and Text2 is to display start date of the week. Do anyone know what function I should use in VB6?

Thanks,
Annie

Annie, I almost missed this post, sorry.;)

To determine the week number, use something like -

Private Sub getWeekNumber()

Dim iNumberOfTheWeek As Integer

iNumberOfTheWeek = DatePart("ww", Now())

End Sub

To get the day currently active -

Dim y As Integer
Dim w As Integer
Dim d As Integer
Dim sDate As Date
' What Year is it?
y = 2010
' What week is it?
w = 49
' Start with Jan 01 of the current year
sDate = "01/01/" & y
' Get the date the first Sunday is on
d = 8 - Weekday(sDate, vbMonday)
' Add the number of weeks to it (minus 1).
sDate = DateAdd("ww", w - 1, CDate("01/" & d & "/" & y))
' This is the Sunday in the week you have for the given year
MsgBox sDate

This will now find the beginning/End of the returned week...

Function FindDateOfWeek(Week As Long, Optional lngYear As Long, Optional FirstDayOfWeek As VbDayOfWeek = vbSunday) As Date
    If lngYear = 0 Then lngYear = Year(Date)
    FindDateOfWeek = DateSerial(lngYear, 1, 1)
    If DatePart("ww", FindDateOfWeek, FirstDayOfWeek) <> 1 Then FindDateOfWeek = FindDateOfWeek + 7
    FindDateOfWeek = FindDateOfWeek - Weekday(FindDateOfWeek, FirstDayOfWeek) + 7 * (Week - 1)
End Function

Dear AndreRet,

Lucky u didn't miss my post ;o)

I need the date show 11-Dec-2010, how i can set the mmm in the code below?
sDate = DateAdd("ww", w - 1, CDate("01/" & d & "/" & y))

thanks,

It's a pleasure.:)

Just change the "ww" to "mm". Year will be "yy".;)

I change already, it got problem run-time error '13', type mismatch.

Paste us your code and tell us on which line the error occurs. There seems to be a mismatch call in the code, something like returning a date when a string is dimensioned...

The one in red colour code is the error occurs.

Private Sub Form_Load()
Dim Mydb As ADODB.Connection
 
Set Mydb = New ADODB.Connection
 
GetDB Mydb

Dim y As Integer
Dim w As Integer
Dim d As Integer
Dim sDate As Date
Dim iNumberOfTheWeek As Integer
Dim RS As ADODB.Recordset
Set RS = New ADODB.Recordset

Dim strName As String

strName = frmLogin.txtUserName.Text
 
RS.Open "Select * from UserTable WHERE UserName =" & "'" & strName & "'", Mydb, adOpenKeyset
   
txtEmpNo.Text = RS!EmpID
txtPosition.Text = RS!Position
txtPrjCode.Text = frmMainMenu.PrjCode
txtPrjName.Text = frmMainMenu.PrjName
txtRegisterDate.Text = Date

y = 2010
w = 50
sDate = "01/01/" & y
d = 7 - Weekday(sDate, vbMonday)
sDate = DateAdd("mm", w - 1, CDate("01/" & d & "/" & yy))
txtStartDate = sDate

iNumberOfTheWeek = DatePart("ww", Now())
txtWeekNo = iNumberOfTheWeek




  RS.Close

  Mydb.Close

End Sub

your suntax is completely incorrect.:)

Try the following -

sDate = DateAdd("m", -1, Now)
'm = the current month
'-1 will deduct a month which will return November the 13th
'Now = todays date to calculate from.

you mean the date syntax is total wrong? Could you guide me more details?

I try to use the code you give, but the result is not correct.

I'm find out the way...I use the same code I send to you and just change the format.

I want to count each Sat of each week. Last line is the code I add to change the format.

Dim y As Integer
Dim w As Integer
Dim d As Integer
Dim sDate As Date

y = 2010
w = 50
sDate = "01/01/" & y
d = 7 - Weekday(sDate, vbMonday)
sDate = DateAdd("ww", w - 1, CDate("01/" & d & "/" & y))
txtStartDate = Format$(sDate, "dd/mmm/yyyy")

Well done, your code is perfect for what you want.:)

Happy coding.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.