2

Ok, hopefully I explain this correctly. I have had about 3 months professional experience working with Access databases, and none with Vb.net, but I am working on a project to get my degree. The goal of this project was to create a Windows Form for my parent's company, All Keyed Up Lock & Safe.

This form was going to link to an Access database with 4 tables that was a listing of some of the company's customers. The form would then allow you to search that database for a specific customer and then return all the information on that specific customer.

This would be useful for billing and also would let us view any special notes we have on the customer before leaving. The four tables are [McDonald's-Corporate Stores], [McDonald's-Independently Owned], [Sonic-Corporate Stores], [Sonic-Independently Owned].

Basically the problem I am having is with the search function. I cannot for the life of me figure out a way to get this to work. I have tried 4 or 5 different solutions and still nothing. I am using Visual Studio 2013. Here is my code I have at the moment, for the search button:

  Private Sub SearchButton_Click(sender As Object, e As EventArgs) Handles SearchButton.Click
        Dim Connection As New SqlClient.SqlConnectionStringBuilder
        Connection.DataSource = "file:///C:\Users\thelukester145\Documents\All%20Keyed%20Up\All%20Keyed%20Up,%20Lock%20&%20Safe,%20LLC.accdb"
        Dim objSQLConnection = New SqlClient.SqlConnection(Connection.ConnectionString)
        Dim cmd As New SqlCommand()
        Dim myTable As New DataTable()
        Dim SearchFor = SearchBox.Text
        If AddressButton.Checked Then
            cmd.Connection = objSQLConnection
            cmd.CommandText = "SELECT * FROM [McDonald's-Corporate Stores] WHERE [Address] LIKE '%" & SearchFor & "%'"
            Dim myAdapter As New SqlDataAdapter(cmd)
            myAdapter.Fill(myTable)
            DataGrid.DataGridView1.DataSource = myTable
        ElseIf NumberButton.Checked Then
            cmd.Connection = objSQLConnection
            cmd.CommandText = "SELECT * FROM [McDonald's-Corporate Stores] WHERE [McOpCo#] LIKE '%" & SearchFor & "%'"
            Dim myAdapter As New SqlDataAdapter(cmd)
            myAdapter.Fill(myTable)
            DataGrid.DataGridView1.DataSource = myTable
        End If
        DataGrid.Show()


    End Sub

As you can see I have just been trying to get this to work for just one table and can't even do that. I would prefer not to use the datagrid method to display the information but rather display it in textboxes that are positioned on the form.

Anyways any help would be MUCH appreciated and sorry for such a long question.

5
  • 1
    So what exactly is the issue? Is the datagrid not getting filled? Or is it that you dont know how to display the data in form fields instead of a grid? I don't think you're asking a specific question, per-se. Commented Nov 29, 2013 at 18:25
  • I'm sorry I guess I didn't really get specific enough. The problem is I honestly have no idea where to even begin with this. I have never tried to link a vb.net program to an Access database to begin with. Tho I did figure that out in the end. But I'm not sure how to run queries from Vb.net either, and I'm not really sure what logic to use in order to perform the search. If that helps Commented Nov 29, 2013 at 18:33
  • Is the above code working? Commented Nov 29, 2013 at 20:37
  • No the code I have is crashing. Let me see if I can get the exact error. Commented Nov 29, 2013 at 20:41
  • Here is the exact error: An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll occurring at Private Sub SearchButton_Click(sender As Object, e As EventArgs) Handles SearchButton.Click Commented Nov 29, 2013 at 20:47

1 Answer 1

1

Where are you opening the connection. Can you try the following code to establish a connection:

Dim cnnOLEDB As New OleDbConnection
Dim cmdOLEDB As New OleDbCommand
Dim strConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & System.Environment.CurrentDirectory & "\URDataBaseName.mdb"
cnnOLEDB.ConnectionString = strConnectionString
cnnOLEDB.Open()

' your code goes here

cnnOLEDB.Close()
Sign up to request clarification or add additional context in comments.

3 Comments

Well that defi has got me farther than before. Now it's crashing at the cnnOLEDB.Open(). It says the problem is that the database is an unrecognized database format
It appears that the issue is caused by a database engine problem, I believe i can fix it. Thank you so much!
@thelukester hey did you get it fixed. If not what is the error message?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.