Solved

How to get a date and time from a database?

Posted on 2004-03-23
7
222 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
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Accepted Solution

by:
ramesh12 earned 500 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

696 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