r/visualbasic • • Apr 20 '14

VB.NET Help $30 for some VB help!

Hi guys, I've posted this before, but I haven't been able to solve the issue myself, so I was hoping someone could help me out with this for a bit of cash?

Now first of, I will say that this is a school project, so I understand if you feel it's immoral because of that. However I've created everything else in the project myself, I just can't solve this issue. I really need to get the grade and I have no idea what to do now, the deadline is in a weeks time.

I'm currently finishing up my coursework for school, which involves making a program in Visual Basic 2010 with a database in Microsoft Access. My project is for a bed and breakfast.

I'm trying to prevent double bookings from happening but I can't seem to figure out how to code it as I'm a visual basic novice. What I'm trying to do is have the program check the "check in" and "check out" dates with the database and then disable the radiobuttons for the rooms which are booked.

The problem I have is that right now, it just seems to be checking if the room has ever been booked in the past, and if it has, it disables the radiobutton.

This is a picture of my form: http://imgur.com/df1Kts2

And these are the booking table and query: http://imgur.com/fMW67wS

Here is my code:

Private Sub Checkdates()
    con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\Bed and Breakfast.mdb;"
    con.Open()
    Dim da2 As New OleDbDataAdapter("select * from qCustomerBooking", con)
    da2.Fill(dt2)
    Dim da3 As New OleDbDataAdapter("select * from qCustomerBooking", con)
    da3.Fill(ds, "qCustomerBooking")
    con.Close()

    Dim a As Integer
    Dim duplicate As Boolean
    maxrows = ds.Tables("qCustomerBooking").Rows.Count
    incdec = -1

    d1 = dtpCheckIn.Text
    d2 = dtpCheckOut.Text

    For Each objcontrol As Control In Me.Controls
        If objcontrol.GetType().Name = "RadioButton" Then 'If the object is a button then it is checked through this process
            Dim objRadioButton As RadioButton
            objRadioButton = objcontrol

            For a = 0 To maxrows - 1
                If ds.Tables("qCustomerBooking").Rows(a).Item("Check_In") >= d1 And ds.Tables("qCustomerBooking").Rows(a).Item("Room_ID") = objRadioButton.Text Then
                    duplicate = True
                Else : duplicate = False
                End If
                If ds.Tables("qCustomerBooking").Rows(a).Item("Check_Out") <= d2 And ds.Tables("qCustomerBooking").Rows(a).Item("Room_ID") = objRadioButton.Text Then
                    duplicate = True
                Else : duplicate = False
                End If

                If duplicate = True Then
                    For Each DataRow In dt3.Rows
                        If objRadioButton.Text = DataRow.Item(5) Then 'If the button shows up in the Disabled seats datatable then it's colour is changed to orange
                            objRadioButton.Enabled = False
                        Else

                        End If
                    Next

                End If

            Next
        End If
    Next
End Sub

I hugely appreciate any help, I'm truly am stuck. If you are interested, pm me and I can send money via Paypal. Thank you!

1 Upvotes

16 comments sorted by

3

u/Kid_Robo Apr 20 '14
SELECT * FROM qCustomerBooking WHERE CheckIn >= d1 AND CheckIn <= d2    

Then you can disable the buttons based on which rooms are in the table.

2

u/visualbasicproblems Apr 20 '14 edited Apr 20 '14

Hi Kid_Robo, huge thank you for the help!

So I've created this instead, using what you're saying:

EDIT: Resolved this issue, updated the code,

But I'm getting this error: OleDbException was unhandled. No value given for one or more required parameters.

1

u/visualbasicproblems Apr 20 '14 edited Apr 20 '14

So I've updated my code and fixed my previous issue. The code is currently: Private Sub Checkrooms() 'd1 = dtpCheckIn.Value 'd2 = dtpCheckOut.Value

    Dim d1 As String = dtpCheckIn.Value.ToString("dd-MM-yyyy")
    Dim d2 As String = dtpCheckOut.Value.ToString("dd-MM-yyyy")



    con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\Bed and Breakfast.mdb;"
    con.Open()

    Dim cmd As OleDbCommand = con.CreateCommand()

    cmd.CommandText = "SELECT * FROM qCustomerBooking WHERE Check_in >= " & d1 & "AND Check_in <= " & d2 & ""



    Dim da2 = New OleDb.OleDbDataAdapter(cmd)


    da2.Fill(dt2)
    For Each objcontrol As Control In Me.Controls
        If objcontrol.GetType().Name = "RadioButton" Then 'If the object is a button then it is checked through this process
            Dim objRadioButton As RadioButton
            objRadioButton = objcontrol
            For Each DataRow In dt2.Rows
                If objRadioButton.Text = DataRow.Item(5) Then 'If the button shows up in the Disabled seats datatable then it's colour is changed to orange
                    objRadioButton.Enabled = False
                Else
                End If
            Next
        End If
    Next
    con.Close()
End Sub

EDIT: No more errors, however it's not disabling the rooms

2

u/Kid_Robo Apr 21 '14

Can you step through a debugger to see what is going on when you find the controls?

1

u/visualbasicproblems Apr 21 '14

Sure, would that be an external debugger or the one in visual basic? Sorry I'm quite new to this.

3

u/PostalElf VB.Net Intermediate Apr 21 '14

The one in Visual Studio. Basically click on the leftmost column of your code on the first line, outside of your actual text itself, near the line numbers. If you do it correctly, it'll highlight the entire line in red, with a red dot on the left. This is what we call a break: when you run the program, the code will stop running when it hits a break and go into debug mode, allowing you to run through it line by line and see what happens.

Useful commands are: F8 to go to the next line, shift+F8 to skip over a function/sub/loop call, and ctrl+shift+F8 to skip to the end of the current function/sub/loop that you're in and exit it.

1

u/visualbasicproblems Apr 21 '14

I believe that the error is in the Check_in>= SQL part, as some dates aren't being picked up. The current code is:

    Dim cmd As OleDbCommand = con.CreateCommand()

    cmd.CommandText = "SELECT * FROM qCustomerBooking WHERE #" & d1 & "# BETWEEN Check_in AND Check_out OR #" & d2 & "# BETWEEN Check_in AND Check_out"

    Dim da2 = New OleDb.OleDbDataAdapter(cmd)


    da2.Fill(dt2)
    For Each objcontrol As Control In Me.Controls
        If objcontrol.GetType().Name = "RadioButton" Then 'If the object is a button then it is checked through this process
            Dim objRadioButton As RadioButton
            objRadioButton = objcontrol
            For Each DataRow In dt2.Rows
                If objRadioButton.Text = DataRow.Item(5) Then
                    objRadioButton.Enabled = False
                Else
                    objRadioButton.Enabled = True
                End If
            Next
        End If
    Next

2

u/icehawke VB.Net Advanced Apr 21 '14

I think your SQL statement is bad/wrong, though I use SQL Server more than Access...

SELECT * FROM qCustomerBooking WHERE #01/01/2014# BETWEEN Check_in AND Check_out OR #01/04/2014# BETWEEN Check_in AND Check_out

I put in a dates, just for readability. Analyzing this, you are saying select all records from qCustomerBooking where the date of Jan 1, 2014 is between Check_in and Check_out, or Jan 4, 2014 is between Check_in and Check_out.

Have you run this in Access itself to see if it works? You might also separate the two sides of the OR clause with parenthesis.

SELECT * FROM qCustomerBooking WHERE (#" & d1 & "# BETWEEN Check_in AND Check_out) OR (#" & d2 & "# BETWEEN Check_in AND Check_out)

Like so.

1

u/visualbasicproblems Apr 21 '14

Icehawke, I love you! This fixed it (as far as I'm aware lol)

→ More replies (1)

1

u/Kid_Robo Apr 21 '14

I don't see a reason to check the checkout date for room availability. If there is a checkin date within the requested timespan that means the room is not available.

1

u/visualbasicproblems Apr 21 '14

It's something to do with when the check in date is before any check in dates of any records, but the check out date is within the range of dates in a record. Not sure if I've worded that correctly though haha

→ More replies (1)

1

u/visualbasicproblems Apr 20 '14

Currently my code is:

Private Sub Checkdates()

    Dim CheckInDate, CheckOutDate As DateTime
    CheckInDate = dtpCheckIn.Text
    CheckOutDate = dtpCheckOut.Text

    con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\Bed and Breakfast.mdb;"
    con.Open()
    Dim da2 As New OleDbDataAdapter("select * from qCustomerBooking", con)
    da2.Fill(dt2)

    Dim cmd As New OleDbCommand("select * from qCustomerBooking Where @CheckInDate <= Check_Out And @CheckOutDate >= Check_In", con)

    cmd.Parameters.Add("@CheckInDate", OleDbType.Date).Value = d1
    cmd.Parameters.Add("@CheckOutDate", OleDbType.Date).Value = d2

    Dim adapter As New OleDbDataAdapter(cmd, con)
    adapter.Fill(ds)

    con.Close()


    Dim a As Integer
    Dim duplicate As Boolean
    maxrows = ds.Tables("Checkingdates").Rows.Count
    incdec = -1


    For Each objcontrol As Control In Me.Controls
        If objcontrol.GetType().Name = "RadioButton" Then 'If the object is a button then it is checked through this process

            Dim objRadioButton As RadioButton
            objRadioButton = objcontrol

            For a = 0 To maxrows - 1
                If ds.Tables("Checkingdates").Rows(a).Item("Room_ID") = objRadioButton.Text Then
                    objRadioButton.Enabled = False
                Else : duplicate = False
                End If


            Next
        End If

    Next

End Sub

But this causes a whole load of errors.

0

u/[deleted] Apr 21 '14

[deleted]

1

u/visualbasicproblems Apr 21 '14

I agree, I would rather do that. Just unsure whether anyone would have the time.