Solved

Date and Time problem in query in VB.NET

Posted on 2004-03-23
10
523 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:DavidMcGarry
Comment Utility
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
Comment Utility
What are the values of strDate and strStartTime when you get to the line?

sSQL2 = "SELECT ...
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:DavidMcGarry
Comment Utility
strDate = txtDayDateRooms.text

strStartTime = cmbStartTimeRooms.text

sSQL2 = "SELECT....

How do i check the values?
0
 

Author Comment

by:DavidMcGarry
Comment Utility
Ok i used a break point.

strDate = "06/02/2004"

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

Accepted Solution

by:
SRigney earned 50 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
This article will show, step by step, how to integrate R code into a R Sweave document
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

771 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now