?
Solved

Date and Time problem in query in VB.NET

Posted on 2004-03-23
10
Medium Priority
?
563 Views
Last Modified: 2012-08-14
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
0
Comment
Question by:DavidMcGarry
  • 5
  • 5
10 Comments
 
LVL 15

Expert Comment

by:SRigney
ID: 10659805
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
 

Author Comment

by:DavidMcGarry
ID: 10660321
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
 
LVL 15

Expert Comment

by:SRigney
ID: 10660393
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:DavidMcGarry
ID: 10660437
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
 
LVL 15

Expert Comment

by:SRigney
ID: 10660495
What are the values of strDate and strStartTime when you get to the line?

sSQL2 = "SELECT ...
0
 

Author Comment

by:DavidMcGarry
ID: 10660601
strDate = txtDayDateRooms.text

strStartTime = cmbStartTimeRooms.text

sSQL2 = "SELECT....

How do i check the values?
0
 

Author Comment

by:DavidMcGarry
ID: 10660622
Ok i used a break point.

strDate = "06/02/2004"

strStartTime = "09:00:00"
0
 
LVL 15

Accepted Solution

by:
SRigney earned 150 total points
ID: 10660666
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
 

Author Comment

by:DavidMcGarry
ID: 10660755
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
 
LVL 15

Expert Comment

by:SRigney
ID: 10660801
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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Simple Linear Regression

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question