Click here to Skip to main content
15,073,507 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i am trying a code i saw in the internet and it seems understandable
but im having a dificulty how to declare the variable used
i am using 2010 , i think the code i saw is from another version
can i improvise a code?
or i can declare it on

im having a problem on the dtgresult

What I have tried:

con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\plan.mdb"

        Label1.Enabled = False
        TextBox1.Visible = False

        sql = "Select * from Elementary where SchoolName like '%" & TextBox1.Text & "%' or SY LIKE '%" & TextBox1.Text & "%'"

        dt = New DataTable
            'open the connection
            'bind the SQL and the connection through OleDBDataAdaoter and stored to da
            da = New OleDb.OleDbDataAdapter(sql, con)
            'and whatever the value of da will be fill into dt our imaginary data table
            'get the datasource of datagridview from our data table
            dtgresult.DataSource = dt

        Catch ex As Exception
            'will throw an error if something went wrong.
            MsgBox(ex.Message, MsgBoxStyle.Information)
        End Try
        'close connection
    End Sub
Updated 6-Jul-21 21:15pm

Never concatenate values from user to the SQL statement. It leaves you open to SQL injection, see SQL injection - Wikipedia[^]

I would suggest going through Properly executing database operations[^]. It should give you ideas how to work with SQL from VB. Even though SqlConnection is used in the article, the principle is the exact same for OleDbConnection.
I am guessing form the statement dtgresult.DataSource = dt that dtgresult is a data grid of some kind. Create a DatagridView on your form and give it the name dtgresult and see what happens.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900