Results 1 to 13 of 13

Thread: Visual Basic 2010 and Excel

  1. #1
    Junior Member
    Join Date
    Oct 11
    Posts
    20

    Visual Basic 2010 and Excel

    I have a small form in which I need to query an Excel sheet with data in it. I have an application where I need to look up vehicles by either tag number, vehicle number, or radio id. Entering any one of the 3 inputs, returns all the other data assigned to that vehicle. So if I type Tag # ABC123, it returns data to all the other fields like vehicle color, make, model, radio id, car #, and its assignment, etc.

    I have built the form in visual basic, but for the life of me cannot figure out the code to do this.

    Can someone help me?

    Thanks

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 05
    Location
    Sydney, Australia
    Posts
    80,781

    Re: Visual Basic 2010 and Excel

    How many records do you have in total? It may be easiest to simply load the whole sheet into a DataTable and then query that using LINQ.

  3. #3
    Junior Member
    Join Date
    Oct 11
    Posts
    20

    Re: Visual Basic 2010 and Excel

    I have approximately 400-500 rows of data, and need to display about 8-10 columns of data per vehicle/resource. Ive never heard of LINQ, ill have to look that up.

  4. #4
    Fanatic Member KGComputers's Avatar
    Join Date
    Dec 05
    Location
    www.vbforums.com
    Posts
    550

    Re: Visual Basic 2010 and Excel

    Have you added reference to MS Excel object library? there you can access workbook, worksheet, range and other classes..There you can perform record retrieval/queries based on your
    requirements..

  5. #5
    Junior Member
    Join Date
    Oct 11
    Posts
    20

    Re: Visual Basic 2010 and Excel

    No I have not. I am still new to this, so I am trying to learn as I go.

  6. #6
    Unmoderated abhijit's Avatar
    Join Date
    Jun 99
    Location
    Chit Chat Forum.
    Posts
    3,117

    Re: Visual Basic 2010 and Excel

    Quote Originally Posted by jmcilhinney View Post
    How many records do you have in total? It may be easiest to simply load the whole sheet into a DataTable and then query that using LINQ.
    Quote Originally Posted by nov0798 View Post
    I have approximately 400-500 rows of data, and need to display about 8-10 columns of data per vehicle/resource. Ive never heard of LINQ, ill have to look that up.
    This is a relatively easy thing to do.

    First establish a connection to the Excel File.
    Connection string will look something like this.
    Next write a query to fetch records from an excel worksheet.

    VB Code:
    1. Dim myexcelFile As String = "c:\temp\myfile.xls"
    2. Dim connectStr As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & myexcelFile & ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";"
    3. Dim dataTable As DataTable = PopulateDataTable(connectStr)
    4. Dim con As New OleDbConnection(connectStr)
    5. Const  qry As String = "SELECT * FROM [Sheet1$]"
    6. Dim odp As New OleDbDataAdapter(qry, con)
    7. odp.Fill(dt)

    Now you have your basic data table. You can use LINQ to manipulate the contents of this table every which way you want.


    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text File

  7. #7
    Junior Member
    Join Date
    Oct 11
    Posts
    20

    Re: Visual Basic 2010 and Excel

    Thanks for the reply. I am still new to this. So how do I create the query, for data in one table, then have it fill in the rest of the form with the proper data? Can you show me an example?

    Thanks again
    Brian

  8. #8
    Junior Member
    Join Date
    Oct 11
    Posts
    20

    Re: Visual Basic 2010 and Excel

    I actually have 428 rows of data, and each row has 11 columns associated with it. I need to be able to type data into any of the forms cells, and have it return all the other data associated with it. Name:  form.JPG
Views: 453
Size:  19.5 KB

    Ive been reading, but am totally lost at this point. Can anyone help me out with this?

    Thanks
    Brian

  9. #9
    Unmoderated abhijit's Avatar
    Join Date
    Jun 99
    Location
    Chit Chat Forum.
    Posts
    3,117

    Question Re: Visual Basic 2010 and Excel

    Quote Originally Posted by nov0798 View Post
    I actually have 428 rows of data, and each row has 11 columns associated with it. I need to be able to type data into any of the forms cells, and have it return all the other data associated with it. Name:  form.JPG
Views: 453
Size:  19.5 KB

    Ive been reading, but am totally lost at this point. Can anyone help me out with this?

    Thanks
    Brian
    I am not sure if your form design is capable of handling multiple results. For instance, if I were to search on year and there are 50 vehicles for the year 1998, how do you propose to display them?


    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text File

  10. #10
    Junior Member
    Join Date
    Oct 11
    Posts
    20

    Re: Visual Basic 2010 and Excel

    Oh sorry, the only searchable fields would be only the ones across the top of the form. These are all unique numbers. I was able to connect the DB to the form in VB2010, but I cannot figure out how to get the form to search the DB, and return a result. I have followed the VB2010 book, but cant get it to "search". Any direction would be wonderful. Also, since these vehicles change throughout the year, can I simply just replace the excel sheet when needed, and all the connections and lookup will remain the same?

    Thanks

  11. #11
    Fanatic Member KGComputers's Avatar
    Join Date
    Dec 05
    Location
    www.vbforums.com
    Posts
    550

    Re: Visual Basic 2010 and Excel

    If your data is changing by year (dynamic), perhaps you should migrate your data into a database. Excel File corruption due to virus and other reasons can likely occur.

  12. #12
    Unmoderated abhijit's Avatar
    Join Date
    Jun 99
    Location
    Chit Chat Forum.
    Posts
    3,117

    Re: Visual Basic 2010 and Excel

    Quote Originally Posted by nov0798 View Post
    Oh sorry, the only searchable fields would be only the ones across the top of the form. These are all unique numbers. I was able to connect the DB to the form in VB2010, but I cannot figure out how to get the form to search the DB, and return a result. I have followed the VB2010 book, but cant get it to "search". Any direction would be wonderful. Also, since these vehicles change throughout the year, can I simply just replace the excel sheet when needed, and all the connections and lookup will remain the same?

    Thanks

    Yes, you can replace your excel sheet every year. The key to remember is you should query the correct sheet and connect to the correct workbook.

    As far as search goes, you'll have to figure that one out on your own. Querying a datatable using LINQ is a simple process.
    You need to add a reference to System.Data.DataSetExtensions and start writing LINQ queries on your object.

    Along the lines of something like this.

    VB Code:
    1. Dim vehicleCounts= From c In dt.AsQueryable()
    2.                   New With
    3.                   {
    4.                       c.VehicleId,
    5.                       Key.OrderCount = c.Vehicles.Count()
    6.                   }


    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text File

  13. #13
    Junior Member
    Join Date
    Oct 11
    Posts
    20

    Re: Visual Basic 2010 and Excel

    Thanks for the info, but unfortunately, I am still WAY to new to completeley understand this. Can anyone help (talk me through) step by step on how to get info entered in to one text box, have it query the excel sheet, and return all the data associated with that vehicle number? If I can see it done once, I may be able to understand. Even if we use a simple set of data, such as type in BLUE in one box, and have it return YES to the other box. A simple 2 text box form that only quries a limited set of data.

    BLUE YES
    RED NO
    GOLD MAYBE

    Sorry, totally lost.

    Also my data actually changes monthly, but I will keep the same Excel sheet and workbook name, just add/delete the rows I dont need, and add new ones as needed.

    Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •