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 23 Replies

i needed the answer badly..please help me

What does ur combobox1 contains and what is in combobox3

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.Read=True then
                MsgBox("Section Already Exist", MsgBoxStyle.Critical, "Error")
                ComboBox3.SelectedIndex = -1
        End If
    End If
End Sub

combobox1 contains the items First Year, Second Year, Third Year, Fourth Year and combobox3 contains sections 1 - 15

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

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

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

paste ur code again so that we can help u....if already fixed mark the thread as solved....
Remove the if condition of validating...see if com is declared....check the connections

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...

ok i'll remove it and update if tis works..thanks

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

but still it is not working

1st, why use use Validating event? Why not SelectedIndexChnaged (of comboBox(es))?
2nd, change comboBoxX.Text with comboBoxX.SelectedItem.tostring()
3rd, remove "if(rid.HasRows)" out of the code, change it with "if(rid.Read())" statement
and last,:
You cannot use comboBox3.Text, this will do nothing. What you can do, is to ADD a new Item to this comboBox. And then select it (if you want to show it on top (in "textBox" of comboBox area).
So change ComboBox3.Text = rid(2) to: ComboBox3.Items.Add(rid(2).ToString())

--
Hope it helps,
bye

@Mitja this is my new code that i based from what you said i hope i did it the way you wanted because it still didn't work

Private Sub ComboBox3_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox3.SelectedIndexChanged
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.Read()) Then
ComboBox3.Items.Add(rid(2).ToString())
MsgBox("Section Already Exist", MsgBoxStyle.Critical, "Error")
ComboBox3.SelectedIndex = -1
Else
' do nothing
End If
End Sub

and by the way where do i add this code? 2nd, change comboBoxX.Text with comboBoxX.SelectedItem.tostring()
tnx

Ok, I did renovate your code, and it you have data inside your table (and it connection string is the right one), is must work:

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() Then
        Dim text As String = rid(2).ToString()
        ComboBox3.Items.Add(text)
        '2 means 3rd column! do you have 3 columns inside table?
        'show this on top:
        ComboBox1.Text = text
            'Interaction.MsgBox("Section Already Exist", MsgBoxStyle.Critical, "Error");               
        ComboBox3.SelectedIndex = -1
            ' do nothing
    Else
    End If
End Sub

yes i have three columns...i'm sorry but it still didn't work

Put a break point before this line of code:
Dim text As String = rid(2).ToString()

and you will see by putting a mouse cursor over "text" variable if there is any text iside.
If it is , it code should put text into comboBox, else its something wrong with query statement.

what does that mean..i'm confused..sorry

Untitled58 it works tnx but when i click the message box the attach image that shows the problem occurs

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...

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.