8 Replies - 1123 Views - Last Post: 26 April 2012 - 02:14 PM Rate Topic: -----

#1 Dictator_Rich  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 29-March 12

im not really sure how to work with databases in vb

Posted 21 April 2012 - 01:26 PM

I am fairly new to programming, and even more new to using database objects. I am creating a program that will calculate a user's BMI, and that will let the user know how many calories are in a food that they choose. I found a database that has a bunch of foods, and each of their calories, so I am using that. I have the program set up so that the user selects each food from a drop down list. The user then adds each of the food to a list box after pressing a button. The program is then suppose to calculate all of the calories after the user presses another button to come up with the total calories. I got all the foods on the drop down list no problem, but now, I'm stuck on figuring out how to get the program to add all the calories for each cooresponding food. I have no idea where to start with it. I know that I'm going to have to create a variable to hold the total calories, but I just don't know how to get the calories from each food. Any help? Thank you!

Is This A Good Question/Topic? 0
  • +

Replies To: im not really sure how to work with databases in vb

#2 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5675
  • View blog
  • Posts: 12,192
  • Joined: 02-June 10

Re: im not really sure how to work with databases in vb

Posted 21 April 2012 - 02:25 PM

See FAQ # 22. (Click the SHOW button below)


TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated Apr 2012
Spoiler



Was This Post Helpful? 1
  • +
  • -

#3 Dictator_Rich  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 29-March 12

Re: im not really sure how to work with databases in vb

Posted 25 April 2012 - 11:13 AM

That doesn't really help me all that much :-\ Maybe I'm just dumb. I just need to figure out how to retrieve specific data from a table from a data source, depending on what food the user selects. I'm sure it's short code, I just can't figure it out at all. The name of data source is Foods. There is a table in Foods called food, and a table called calories, and I want all of the calories from the Foods table to be added up, depending on what food they select. It's hard to explain, that's why I'm trying to explain it in different ways. I tried searching for many tutorials, but I can't find exactly what I'm looking for.
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5675
  • View blog
  • Posts: 12,192
  • Joined: 02-June 10

Re: im not really sure how to work with databases in vb

Posted 25 April 2012 - 11:27 AM

View PostDictator_Rich, on 25 April 2012 - 12:13 PM, said:

That doesn't really help me all that much :-\


Really? An entire sub-section of database tutorials... A couple dozen tutorials minimum... And none of it taught you anything?

Quote

Maybe I'm just dumb.

Please forgive how this sounds but... I don't think you're dumb... I think you're trying to find one block of code to copy/paste into your project rather than take an hour out of your day to actually learn some new material/concepts. That makes for swiss-cheese learning: Lots of holes.

Today its looking for this one bit... Tomorrow its looking for one other bit... Next week its one other bit...

You're going to eat up as much OR MORE hours doing all these searches than if you would just slow down, and work the tutorials from start to finish.
Was This Post Helpful? 1
  • +
  • -

#5 Dictator_Rich  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 29-March 12

Re: im not really sure how to work with databases in vb

Posted 25 April 2012 - 11:49 AM

I understand, but it's hard to comprehend the tutorial, since it's in C#.
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5675
  • View blog
  • Posts: 12,192
  • Joined: 02-June 10

Re: im not really sure how to work with databases in vb

Posted 25 April 2012 - 12:09 PM

I'll admit its a stretch from a dead language like VB6 to a .NET language like C#. I'm surprised you're developing in VB6 as it mostly for supported of legacy applications. At the very least new apps should be done in VB.NET.

But anyway...

The databases still work the same. The SQL statements should be the same.
The tutorials section has tutorials that don't use any primary language and concentrate on how to format the statements you send to the database:
http://www.dreaminco...-part-1-design/

http://www.dreaminco...-in-sql-server/

You don't mention what database you are using (mySQL, SQL ce, Access, etc.) so it makes it more difficult to give advice. But I would think a google for the terms "VB6 mysql tutorial" for example would get you pretty targeted examples.
Was This Post Helpful? 0
  • +
  • -

#7 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: im not really sure how to work with databases in vb

Posted 25 April 2012 - 02:31 PM

Suppose you also post what you've done so far. There are plenty of ways to get the calorie count for foods, but if you're using a VB6 Listbox, you'll want to have a look at the .ItemData property.
Was This Post Helpful? 0
  • +
  • -

#8 Dictator_Rich  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 29-March 12

Re: im not really sure how to work with databases in vb

Posted 26 April 2012 - 01:40 PM

Here is the code I have so far:

I have these declared globally:

    Public foodAmount As Single = 0
          Public temp1(50) As Single


This is the method that adds each item to the first list box, which is activated when the user presses a button. The calories of the currently highlighted item is shown in another listbox (listbox1)::
    Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click
        Foods_Selected.Items.Add(ComboBox1.Text)

        Dim i As Single
        temp1(i) = ListBox1.SelectedValue

        foodAmount += 1
        i += 1


End Sub


And this is the code I have for the button that adds all of the calories.
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

        Dim x As Single
        Dim TotalCalories As Single

        For x = 0 To foodAmount Step 1
            TotalCalories = TotalCalories + temp1(x)
        Next

        TextBox3.Text = TotalCalories

    End Sub


Everything works except for the total calories. For some reason, the total calories is always the amount of calories for the last food that was selected. I can't find the .ItemData property, and I am using access database
Was This Post Helpful? 0
  • +
  • -

#9 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: im not really sure how to work with databases in vb

Posted 26 April 2012 - 02:14 PM

All right. This is in VB.Net, which is why there isn't an .ItemData property; those are only in VB6. Your problem is on line 4 of your Button4_Click procedure. If you had stepped through the code you would have found this easily, so I suggest that you need to learn how to set breakpoints in your code, step through it line by line, and evaluate variables as you go.

In this case, every time you Dim i as Single, it gets initialized to 0. Therefore, you keep adding values to temp1(0), overwriting the old values as you go. Then, when you run through your temp1 array, temp1(0) is equal to the last value you've plugged into it (i. e. the last food that was selected), and all the other values are equal to 0. You get what you ask for. :)

I would recommend that you use two things: a static variable for i--please read up on it so you understand why--and a dynamic array. While the latter isn't strictly necessary to get your code to work (the former is, unless you want to make it a global definition which is less precise), it's just bad design to make sure you have enough room in your array to cover any situation, rather than continually adding values. Investigate "dynamic array vb.net" and "redim preserve".
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1