?
Solved

How to get a date and time from a database?

Posted on 2004-03-23
7
Medium Priority
?
226 Views
Last Modified: 2010-04-17
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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 7

Expert Comment

by:ramesh12
ID: 10659694
How are you storing date and time in your DB.

if your time format in db is hh:mm:ss then change

Format(strStartTime)  to Format(strStartTime,"hh:mm:ss")

What i mean is make sure the formats of data stored in db and the format of data you entered and chechking against db i.e., strStarttime is in same format
0
 

Author Comment

by:DavidMcGarry
ID: 10660260
In my Access database strStartTime is a date/time data type.

below is the code i tried for the time bit you suggested:

"AND tblSubjectSchedule.Time_Start =
#" & Format(strStartTime,"hh:mm:ss") & "#"

It comes up with and error,and instead of displaying a time in the error handler, it displays hh:mm:ss
0
 

Author Comment

by:DavidMcGarry
ID: 10660377
I have strStartTime = txtStartTimeRooms.text

could this be the problem?

I also have strStartTime declared as a string

Is this the problem?

And how do i format it correctly?
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.

 
LVL 7

Accepted Solution

by:
ramesh12 earned 1000 total points
ID: 10660406
How are you entering time in your txtStartTimeRooms.text

I dont think declaring startrttime to string to be a problem.
0
 

Author Comment

by:DavidMcGarry
ID: 10660700
cmbStartTimeRoom.text is a combo box that is populated with times.

When i select a time, this is the time i want to check!!
0
 

Author Comment

by:DavidMcGarry
ID: 10660728
Thanks i just sorted it,How do i give ya the points?

Im doing a thesis+my programming skills are very amateurish, if thats a word!!
0
 
LVL 10

Expert Comment

by:Kavar
ID: 10673284
on the screen by everyones comments you should see the "Accept answer button" click on the button associated with the answer that you feel helped you.  
0

Featured Post

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.

Question has a verified solution.

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

Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
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 …
Six Sigma Control Plans

762 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