We help IT Professionals succeed at work.

vb.net

gary chiam
gary chiam asked
on
99 Views
Last Modified: 2017-04-16
As mentioned,
I'm working with VB.NET Windows Form and an Access DataBase. A table in the database stores room numbers and the reservation dates for these rooms (checkin and checkout) among other things.


Here's the code used in the search function:
 provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source ="
        'Change the following to your access database location
        dataFile = "C:\Users\fongf\Desktop\FYP_Database\FYP.accdb"
        connString = provider & dataFile
        myConnection.ConnectionString = connString

        myConnection.Open()
        'the query:
        Dim cmd As String = "SELECT * FROM Reservation WHERE (checkin > " & "#" & DateTimePicker1.Text & "#" & " and checkin > " & "#" & DateTimePicker2.Text & "#" & ")" & " or " & "(checkOut < " & "#" & DateTimePicker2.Text & "#" & " and checkOut < " & "#" & DateTimePicker1.Text & "#" & ")"
        Dim dr As OleDbDataReader = cmd.ExecuteReader



        If reservation = True Then
            MsgBox("Sorry, this room was booked", MsgBoxStyle.OkOnly, "Invalid Login")
        Else
            MsgBox("all is ok")
        End If
        myConnection.Close()

Sorry if the question seems silly, but I haven't had much experience dealing with databases and time. Your help or pointing me to a source with good information on this would be greatly appreciated. Thank you.
Comment
Watch Question

Éric MoreauSenior .Net Consultant
CERTIFIED EXPERT
Top Expert 2016

Commented:
I see 2 things.

The first one is that your query (a string) is named cmd (probably for Command) but a string does not have a ExecuteReader method. A command has. Have a look at https://msdn.microsoft.com/en-us/library/979byfca(v=vs.110).aspx

The second thing is about the query. With all the concatenation you are doing, it is somewhat hard to read. Also the BETWEEN operator would make it clearer (at least for me):

        Dim sql As String = "SELECT * FROM Reservation WHERE (checkin BETWEEN #" + DateTimePicker1.Value.Tostring("yyyy/MM/dd") & "# and #" & DateTimePicker2.Value.Tostring("yyyy/MM/dd") & "# ) or (checkOut BETWEEN #" & DateTimePicker2.Value.Tostring("yyyy/MM/dd") & "# and #" & DateTimePicker1.Value.Tostring("yyyy/MM/dd") & "#)"

Open in new window

Author

Commented:
Mr.Eric , can you show me some example about how to do the availability of hotel room with room number , checkIn (datetimepicker1)  and checkOut(datetimepicker2) ?
Your help i really  appreciated.
Senior .Net Consultant
CERTIFIED EXPERT
Top Expert 2016
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
CERTIFIED EXPERT

Commented:
How's your window form's interface looks like?

can you provide the table structures with sample data?
Éric MoreauSenior .Net Consultant
CERTIFIED EXPERT
Top Expert 2016

Commented:
I received a Thumbs up on that comment from the asker

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions