please kindly help me!!!
what i wanted is when i select items in the combobox and it is already in the database it will prompt that it is already in the database..tnx

Private Sub ComboBox3_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles ComboBox3.Validating
If ComboBox3.Validating Then
    con = New OleDbConnection("Provider= Microsoft.ACE.oledb.12.0; Data Source=C:\Users\edenzam\Desktop\CBFMNHS Enrollment System\CBFMNHS Enrollment System\bin\Debug\Enrollment System.accdb")
     Dim ewaaa As String = "Select * from SchedulingTwos where YearLevels = '" & ComboBox1.Text & "'"
     com = New OleDbCommand(ewaaa, con)
     con.Open()
     com.ExecuteNonQuery()
     rid = com.ExecuteReader
     rid.Read()
If (rid.HasRows) Then
ComboBox3.Text = rid(2)
MsgBox("Section Already Exist", MsgBoxStyle.Critical, "Error")
ComboBox3.SelectedIndex = -1
End If
End If
End Sub

Recommended Answers

All 17 Replies

So what seems to be the problem?

First. Get rid of the line com.ExecuteNonQuery(). You ARE executing a query.
Second. In order to compare the selected item in the combobox with the result from the query, you should actually perform a check on it.
Not assign it.
Replace Combobox3.Text = rid(2) with If Combobox3.Text = rid(2) Then

Also. Close the database connection when you're done.

try the below one

    Private Sub ComboBox3_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles ComboBox3.Validating
        If ComboBox3.Validating Then
            con = New OleDbConnection("Provider= Microsoft.ACE.oledb.12.0; Data Source=C:\Users\edenzam\Desktop\CBFMNHS Enrollment System\CBFMNHS Enrollment System\bin\Debug\Enrollment System.accdb")
             Dim ewaaa As String = "Select * from SchedulingTwos where YearLevels = '" & ComboBox1.Text & "'"
             com = New OleDbCommand(ewaaa, con)
             con.Open()
             rid = com.ExecuteReader
            if rid.reader=true Then
               ' ComboBox3.Text = rid(2)
                MsgBox("Section Already Exist", MsgBoxStyle.Critical, "Error")
                ComboBox3.SelectedIndex = -1
            End If
        End If
    End Sub

their is an error with this code If ComboBox3.Validating Then

@oxiegen how do i do this? Second. In order to compare the selected item in the combobox with the result from the query, you should actually perform a check on it.
Not assign it.

dont use the earlier if condition

use the below select query

"Select * from SchedulingTwos where YearLevels = '" & ComboBox1.Text & "'" and Sections = '" & ComboBox3.Text & '""

if this condition is true then u will get the alert message that the section is already added

    Private Sub ComboBox3_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles ComboBox3.Validating
   ' If ComboBox3.Validating Then
    con = New OleDbConnection("Provider= Microsoft.ACE.oledb.12.0; Data Source=C:\Users\edenzam\Desktop\CBFMNHS Enrollment System\CBFMNHS Enrollment System\bin\Debug\Enrollment System.accdb")
    Dim ewaaa As String = "Select * from SchedulingTwos where YearLevels = '" & ComboBox1.Text & "'" and Sections = '" & ComboBox3.Text & "'"
    com = New OleDbCommand(ewaaa, con)
    con.Open()
    rid = com.ExecuteReader
    if rid.reader=true Then
    ' ComboBox3.Text = rid(2)
    MsgBox("Section Already Exist", MsgBoxStyle.Critical, "Error")
    ComboBox3.SelectedIndex = -1
    End If
  '  End If
    End Sub

this code generates an error com = New OleDbCommand(ewaaa, con) it says that ewaaa is not declared

Private Sub ComboBox3_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles ComboBox3.Validating
If ComboBox3.Text = "'" Then
con = New OleDbConnection("Provider= Microsoft.ACE.oledb.12.0; Data Source=C:\Users\edenzam\Desktop\CBFMNHS Enrollment System\CBFMNHS Enrollment System\bin\Debug\Enrollment System.accdb")
Dim ewaaa As String = "Select * from Schedulings where YearLevels = '" & ComboBox3.Text & "' And Sections = '" & ComboBox1.Text & "' "
com = New OleDbCommand(ewaaa, con)
con.Open()
rid = com.ExecuteReader
rid.Read()
If (rid.HasRows) Then
ComboBox3.Text = rid(2)
MsgBox("Section Already Exist", MsgBoxStyle.Critical, "Error")
ComboBox3.SelectedIndex = -1
End If
End If
End Sub

i think the problem is in this part "If ComboBox3.Text = "'" Then" i just don't know how to manipulate combobox when the event is validating...please help me

Remove that if (If ComboBox3.Text = "'") condition y u need it???

and u have written double quotes then single quote and then double quotes in the if condition for combobox text....y u need that??? remove the single quote and keep it just If ComboBox3.Text = "" Then....i would recommend to remove that condition

anyways if this is the condition it wont enter the if loop anytime...

What poojavb is saying is that you do not need those single and double quotes.
Single quotes in SQL are used to represet a string DATA value. The double quotes you are using to creating a string
in VB.NET

Example:

Dim strn As String = "This is the correct way to declare a string."
Dim strn2 As String = "SELECT * FROM table WHERE columm='" & myString & "'"

thank you for the replies i'll try doing this an update if it is solved and if their are still problems

i change my codes to this:
Private Sub ComboBox3_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles ComboBox3.Validating
con = New OleDbConnection("Provider= Microsoft.ACE.oledb.12.0; Data Source=C:\Users\edenzam\Desktop\CBFMNHS Enrollment System\CBFMNHS Enrollment System\bin\Debug\Enrollment System.accdb")
Dim ewaaa As String = "Select * from Schedulings where YearLevels = '" & ComboBox3.Text & "' And Sections = '" & ComboBox1.Text & "' "
com = New OleDbCommand(ewaaa, con)
con.Open()
rid = com.ExecuteReader
rid.Read()
If (rid.HasRows = True) Then
ComboBox3.Text = rid(2)
MsgBox("Section Already Exist", MsgBoxStyle.Critical, "Error")
ComboBox3.SelectedIndex = -1
Else
' do nothing
End If
End Sub

bit still it is not working

Check the other thread of yours, I have answered you there.
And close this one, we will continue discussion in one thread only.
Ok?
thx in advance,
bye

U can use Debug.Print("text: "+ text) to check the value...if u dont know how to use debugger

it will show the value of the word declared as a variable but it should be string else convert it to String...

Try this below code...

Private Sub ComboBox3_SelectedIndexChanged(sender As System.Object, e As System.EventArgs)
con = New OleDbConnection("Provider= Microsoft.ACE.oledb.12.0; Data Source=C:\Users\edenzam\Desktop\CBFMNHS Enrollment System\CBFMNHS Enrollment System\bin\Debug\Enrollment System.accdb")
Dim ewaaa As String = "SELECT * FROM Schedulings WHERE YearLevels = '" & ComboBox3.SelectedItem.ToString() & "' AND Sections = '" & ComboBox1.SelectedItem.ToString() & "'"
com = New OleDbCommand(ewaaa, con)
con.Open()
rid = com.ExecuteReader()
If rid.Read = True Then
MsgBox("Section Already Exist", MsgBoxStyle.Critical, "Error");
else
MsgBox("Section does not Exist", MsgBoxStyle.Critical, "Error");
End If
End Sub

instead of & use + sign.....if still it does not work then try to convert SelectedItem.ToString to Text....hope it works for u...

tnx I already solve this problem tnx to everyone specialy to Mitja Bonca

You are welcome mate :)
bye

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.