Log inUsernamePassword
Log me on automatically each visit    
Register
Register
Log in to check your private messages
Log in to check your private messages
Visual Basic Forum for Visual Basic Programmers VB Forum Index » Database & Reporting

Post new topic   Reply to topic
ComboBoc and DBGrid Problem
View previous topic :: View next topic  
Author Message
felixg
Newbie


Joined: 29 Aug 2005
Posts: 1

PostPosted: Aug 29th, 2005 09:44 PM    Post subject: ComboBoc and DBGrid Problem Reply with quote

I 'm not sure if this has been disscussed before or anything, and if it has been, I aplogize and would PLEASE ask for a link to take me to that particular topic, but here's my problem:

---

I am new to Visual Basic, and am using Visual Basic 6.0 and have a database, (I have a DBGrid) and I have two fields; "Description," and "Price."

I want "Description" to appear in a Combo Box, and "Price" to appear in a label or text box. BUT, the price will only be displayed AFTER you select the Description from the combo box. This is my main problem, I have no idea how to do it.

This part is not as important:
I want to be able to add more descriptions and prices into the database, and have the description of the newly added item instantly placed into the combo box, along with the others, and the price to appear when that description is selected (as I said).

---

I would really appreciate any help I can get. Thanks a lot!

-felixg
Back to top
View user's profile Send private message
dougthomas
Moderator


Joined: 27 Jul 2005
Posts: 271
Location: Essex, UK

PostPosted: Aug 31st, 2005 05:11 AM    Post subject: Reply with quote

Hi and welcome,

Without knowing what method of Database access you are using it's difficult to solve the problem. But on a general level and as a bit of a tutorial.....

I assume you have a DBGrid control (DBGrid1) and a DataControl (Data1) as its DataSource on your Form (Form1). When you start the Program DBGrid1 will automatically be populated from Data1.

The key to the whole thing is the Recordset property of Data1. This contains the records from the Database which are selected from the Table defined in the Data1.RecordSource property, in the Database defined by the Data1.DatabaseName property.The link between Data1 and the DBGrid is the DBGrid's DataSource property which is Data1.

So the link to the data is:
DBGrid.DataSource -> Data1
Data1.DatabaseName -> Database (eg."C:\MyDatabase.mdb")
Data1.RecordSource -> DatabaseTable (eg. Products)
(Where -> = "Points to")

Data1.Recordset Contains Records from the database table.


Your first task is, I believe, to also populate a ComboBox (Combo1) with the Descriptions from the database table. This can be done in the Forms's Initialise event vis:
Code:

Private Sub Form_Initialize()
Data1.Recordset.MoveFirst
Do Until Data1.Recordset.EOF
    Combo1.AddItem Data1.Recordset![Description]
    Data1.Recordset.MoveNext
Loop
Combo1.Text = Combo1.List(0)
Data1.Recordsset.MoveFirst
End Sub

What this is doing is moving through each record in the Recordset and adding each value of Description into the ComboBox.(That's what the AddItem method of the ComboBox does)

When you now start the Program, the Grid will be populated, with Description and Price and the ComboBox with all the Descriptions. The first Description will be displayed in the ComboBox. (That's what the "Combo1.Text= Combo1.List(0)" does)

The next task is to display the corresponding Price in a Label, when the user selects a Description from the ComboBox. This can be done in the ComboBox_Click event. vis:
Code:

Private Sub Combo1_Click()
Dim strFind as String
strFind = "[Description] = " & "'" & Combo1.Text & "'"
Data1.Recordset.FindFirst strFind
Label1.Caption = Data1.Recordset![Price]
Data1.Recordset.MoveFirst
End Sub

What this is doing is positioning the RecordSet pointer to the record where the Description is equal to the value in the ComboBox Text property (ie the one the user clicked on). The Label's Caption Property is then set to the corresponding value of Price. The Recordset is then positioned back to the first record ready for the user to select another Description.

Regarding your secondary task, that of populating the ComboBox with new descriptions and prices. When you get to that bit, you will have some code that adds the new record to the Database, after that has been successful all you need to do is to add the new item to the ComboBox and requery the Database to refresh the descriptions and prices.

Hope this helps, if you have any problems please come back and ask,

Regards
Doug
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Visual Basic Forum for Visual Basic Programmers VB Forum Index » Database & Reporting All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Visual Basic Forum runs phpBB | Forum Template © iOptional
VB Resources | SSL | Visual Basic