Getting data from a datatable

Hi all,

Im trying to retrieve data from one of my datatables but i dont know how to reference the location of the data i want. Basically i have a table as follows:

Joint Type Thickness M8 M10 M16

JD1 35 100 200 300

JD1 45 150 250 350

JD2 35 400 500 600

JD2 45 450 550 650

The actual table has a lot more data in it in reality but you get the idea.

On my userform i have a combobox with a list of joint types i.e. JD1,JD2 etc, a combobox with bolts sizes i.e. M8,M10,M16 and a textbox where thickness is entered.

I want to create code that will search the datatable and return a value at the location when all the conditions are met. For example if i selected JD1 and M10 from the comboboxes and also entered a thickness of 45, the value returned would be 250.

This is a bit complicated but the real trouble im having is understanding how to return a value from any point in the datatable. For example if i wanted my code to return the value at column(3) and row(3).. how do i do that It sounds simple enough to do but im a novice at VB2005 so it a bit tricky for me. Im used to the range and offset properties in excels VB.

I created a very similar application in excels version of VB but i used lots of arrays and IF loops to fill them and it seems a rather inefficient way of doing it.

I'll be grateful for any help.

Cheers



Answer this question

Getting data from a datatable

  • smmoraco

    Thanks that was very helpful. It makes a lot more sense now. Im actually not get some form of error or warning now... yey! Thx champ!

    Im not exacty sure i did the whole datasource = the dataset thing correctly. Could you please tell me how you would do that. My code works so im guessing i did it right but it was probably by accident!

    Thx again

    Aaron


  • ThE_mASk

    if its not correct it will not work, so i guess you have it correct

    best regards



  • Jim.C

    hi, Aaron

    you can take a look to this thread if you using dataset you can take a look to this thread

    http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=256883&SiteID=1

    if you asking about how you can retrieve data from your database to your dataset then you must know some sql there is a good tutorial in this link http://www.sqlcourse.com/

    hope this helps



  • Gunilla

    Hey again,

    Those links were interesting but they didnt really help me out that much. Im having trouble referencing my data. At this stage all i want to know is how to simply retrieve data from a point in my datatable and put it in a message box to see if it works! That sounds simple but i cant even do that.. i dont really understand the SQL stuff.

    Ive tried stuff like:

    Private Sub...etc

    Dim column1 As ColumnData

    column1 = aarondataset.datatable1.columns(1)

    msgbox(column1)

    End Sub

    but i keep getting.. aarondataset is not defined. I dont know how to get info out of datasets/tables.. thats my major problem.

    What do i have to define and how to i do it I think it involves using the New statement but i dont know what it does. Like i said earlier... im used to VB6 and VB2005 is a lot harder to understand.

    Ive created a database and connected it to my application.. i just need someone to show me a simple sub procedure code sample that will get a piece of data in the above datatable and put it in a msgbox. Once i understand how to do this i should be able to work out the rest.

    I would be really grateful if someone could help me out.

    Thanx :)


  • Brian Knight

    hi,

    first of all connect database to your project is not every thing you have to add dataset to your project in solution explorer to retrieve data from your database (i think you did that also)

    next step is to add instance of this dataset to your form, go to your tools scroll down till you find data tab you will find dataset there drag one to your form go to property rename it and set the datasource = the dataset in your solution explorer, don't name the instance in your form by the same name as the one in the solution explorer its recomended

    now you can reference to the instance that you have

    Dim dr As DataRow

    dr = Me.MyDataSet.Tables("mytablename").Rows.Find("columnName = value")

    messagebox.show(dr("columnName"))

    also you can't get data by useing columns just you can use rows to get data

    hope this helps



  • Getting data from a datatable