[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 229
  • Last Modified:

How to get a date and time from a database?

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
DavidMcGarry
Asked:
DavidMcGarry
  • 4
  • 2
1 Solution
 
ramesh12Commented:
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
 
DavidMcGarryAuthor Commented:
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
 
DavidMcGarryAuthor Commented:
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
ramesh12Commented:
How are you entering time in your txtStartTimeRooms.text

I dont think declaring startrttime to string to be a problem.
0
 
DavidMcGarryAuthor Commented:
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
 
DavidMcGarryAuthor Commented:
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
 
KavarCommented:
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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now