Date and Time problem in query in VB.NET

Hi,

This is my code to check if a date and a time both exist in a table:
------------------------------------------------------------------
        Dim rsVal2 As ADODB.Recordset
        Dim sSQL2 As String
       
        Dim strDate As Date
        Dim strRoom As String
        Dim strSubjectName As String
        Dim strSubjectID As String
        Dim strDay As String
        Dim strStartTime As String
        Dim strEndTime As String
        Dim i As Short
        Dim strYear As String

        strDate = txtDayDateRooms.Text
        strRoom = cmbViewRooms.Text
        strSubjectName = cmbSubjectRooms.Text
        strSubjectID = txtSubjectIDRooms.Text
        strDay = cmbDayRooms.Text
        strStartTime = cmbStartTimeRooms.Text
        strEndTime = cmbEndTimeRooms.Text

'**** CHECK IF TIMETABLE TIME ALREADY EXISTS ******

'Query check if new time already exists in time table.
sSQL2 = "SELECT tblSubjectSchedule.Subject_ID, tblSubjectSchedule.Date_Sch, tblSubjectSchedule.Time_Start, tblSubjectSchedule.Location,
tblSubjectSchedule.Day_Sch " & _
"FROM tblSubjectSchedule " & _
"WHERE tblSubjectSchedule.Subject_ID='" & strSubjectID & "' " & _
"AND tblSubjectSchedule.Location='" & strRoom & "' " & _
"AND tblSubjectSchedule.Day_Sch='" & strDay & "' "
'"AND tblSubjectSchedule.Date_Sch=#" & Format(strDate) & "#"
'"AND tblSubjectSchedule.Time_Start= #" & Format(strStartTime) & "#" <----THESE TWO LINES ARE WHAT ARE
CAUSING THE PROBLEM

rsVal2 = m_Conn.Execute(sSQL2) 'Execute query.

if Not rsVal2.EOF Then
            i = 0
            rsVal2.MoveFirst() 'Move to the first record.

            While Not rsVal2.EOF

                rsVal2.MoveNext() 'Move to the next record.
                i = i + 1
            End While
        End If

        If i = 0 Then
            MsgBox("THERE ARE NO RECORDS")
        End If

        If i > 0 Then
            MsgBox("THERE ARE RECORDS")
            Exit Function
        End If

------------------------------------------------------------------

If the date and time exist then i cant add new details in. If there are no records for the date and time, then i can add in.The query is where the problem is, but i think my date and time formatting is incorrect

How do i do this? My code doesnt seem to recognise the date or time when looping through, and always works as if there are no records for date and time, when i know for
a fact in my Access DB that there is!!

Thanks, im in dire straits trying to get this working, hence the 500 points.

Thanks
DavidMcGarryAsked:
Who is Participating?
 
SRigneyConnect With a Mentor Commented:
You put a breakpoint on the sSQL2 = "SELECT...." line and then look to see what the values of the 2 fields are.

You could also add MessageBox code that would show them.  Just so we can see the actual value and see what is being passed in after the format statement in your SQL.
0
 
SRigneyCommented:
If this is SQL Server then date and time need to be surrounded with single quote.

If this is for Access then the pound sign is used.
0
 
DavidMcGarryAuthor Commented:
Im using an access database,

I tried what you suggested + its not working

"AND tblSubjectSchedule.Time_Start= £" & Format(strStartTime) & "£"


It displays the time in the error handler,but has the time surrouned by two pound signs+says its incorrect data type?
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
SRigneyCommented:
Not the pound sign as in 5 pounds money, the # is called the pound sign.  Which is what you already had

What type of field is it in Access?
0
 
DavidMcGarryAuthor Commented:
It is a date/time field in Access.

I have strStartTime declared as a string at the top of the code and then set it to

strStartTime = txtCmbStartTimeRooms.text

Then im running the query+then i have problems

0
 
SRigneyCommented:
What are the values of strDate and strStartTime when you get to the line?

sSQL2 = "SELECT ...
0
 
DavidMcGarryAuthor Commented:
strDate = txtDayDateRooms.text

strStartTime = cmbStartTimeRooms.text

sSQL2 = "SELECT....

How do i check the values?
0
 
DavidMcGarryAuthor Commented:
Ok i used a break point.

strDate = "06/02/2004"

strStartTime = "09:00:00"
0
 
DavidMcGarryAuthor Commented:
Ok i just sorted, i dont even know how i did it???

how do i give ya the points?? First time using this thing!
0
 
SRigneyCommented:
I think you have an option to grade the question then you choose the answer that you want.  You can probably find the exact procedures in the community support room.

I've never asked a question so I'm not sure about how to give points.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.