Click to See Complete Forum and Search --> : Filling text boxes on a form
gknierim
February 1st, 2000, 12:45 PM
I have asked this before and didn't really get any help and it was left unanswered. Any help would be greatly appreciated.
I have a dropdown list that is populated with a field from my recordset. I also have text boxes on my form from the same recordset that compose of the other fields. When I select a value from the dropdown, it doesn't populate(update) the rest of the text boxes on the form. I don't have any code written because I am under the assumption that once I pick a record from the dropdown-combolist box, it should populate the rest of the fields. Is this a wrong assumption? If it is, could someone tell me in detail(code) how to populate those other fields.
Aaron Young
February 1st, 2000, 01:27 PM
Use the FindFirst Method of the Recordset in the DBCombo_Click() Event to Update the Recordset Cursor Location, ie.
private Sub DBCombo1_Click(Area as Integer)
If Area = dbcAreaList then Call Data1.Recordset.FindFirst("Au_ID=" & DBCombo1)
End Sub
This Example Lists the Author IDs in the Combo and Author Name in the Textbox, (using the BIBLIO.MDB).
Aaron Young
Analyst Programmer
ajyoung@pressenter.com
aarony@redwingsoftware.com
Johnny101
February 1st, 2000, 01:37 PM
The forms in VB aren't as "smart" as those you might have in Access. So when you choose an entry in the drop down box, you will have to manually populate those other text boxes.
Here is an example:
Assumptions:
1) The drop down box is named cboList.
2) There are two text boxes on the form names txtFirstName and txtLastName.
3) There is a global recordset object that is open named gRS.
4) The items in the drop down list are in the exact order in which they sit in the recordset.
5) You are using ADO for your data access.
private Sub cboList_Click()
'this is fired when the user chooses an entry from the dropdown box
Dim iItemIndex as integer
iItemIndex = cboList.Listindex 'the index of the item chosen
gRS.MoveFirst 'move the first record
gRS.Move iItemIndex 'move the item corresponding to the item chosen
txtLastName.Text = gRS!LastName
txtFirstName.Text = gRS!FirstName
End Sub
This may not be the best way to navigate through the recordset, but it will work if you open a recordset supports backwards movements. If you're using Access as the database on the backend, then you have to open a dynamic type recordset to be able to move backwards. Check the ADO documentation for more information.
Hope this helps,
John
John Pirkey
MCSD
www.ShallowWaterSystems.com
gknierim
February 1st, 2000, 03:37 PM
I think that this will do what I need except when I compile it, I receive an error on the line that contains the .ListIndex . Also, I commented it out and then it doesn't like the MoveFirst either. My ComboBox is named cboDrvName. Here is my code:
Dim iItemIndex as integer
iItemIndex = cboDrvName.Listindex 'the index of the item chosen
datPrimaryRS.MoveFirst 'move the first record
datPrimaryRS.Move iItemIndex 'move the item corresponding to the item chosen
txtCarNo.Text = datPrimaryRS!Driver_Car_No
I don't think that the Listindex is a function of the combobox or maybe there is some other syntax. What am I doing wrong here? Also, do I need to have the ADO on the form? Currently, I have it on the form but hidden at runtime. And I am also using a Access database on the backend.
Thanks for the help!!
Greg
Johnny101
February 1st, 2000, 03:58 PM
Are you using an ADO Data Control or any sort of Data Control? If so, then the move methods wont work. I think you can use this method instead:
datPrimaryRS.Recordset.MoveFirst
datPrimaryRS.Recordset.Move
Also, i forgot to populate the dropdown box, try something like this:
private Sub Form_Load()
While Not datPrimaryRS.Recordset.EOF
cboDrvName.AddItem datPrimaryRS.Recordset.Fields(0).Value
datPrimaryRS.Recordset.MoveNext
Wend
End Sub
I just used the above code with an ADO data control and regular combo box to go against an Access database and populate the drop down box with some entries. Then i used the modified code above that to get the other fields i wanted to show up in the text boxes.
Hope this helps,
John
John Pirkey
MCSD
www.ShallowWaterSystems.com
gknierim
February 2nd, 2000, 08:12 AM
I have an ADODC named datPrimaryRS on my form. Do I really need this? I have made it hidden at runtime. Basically, to summarize what I'm trying to do is this. I have a table in my Access database that has a list of names. I want the dropdown list to contain all of the names in the table. However, the form is designed for the user to select a name from the list and then delete that name from the table via the menu or button. The other fields on the form are there just to verify they have chosen the right name (i.e. there were 2 names of Smith). Sorry if I haven't made this clear before. Just didn't want to get to detailed but I guess I really should have.
As far as the code, I was using a DataCombo control and not a regular ComboBox. The code for Form Load works great but what if I want to show the first record on load? I have tried
datPrimaryRS.Recordset.MoveFirst
and that didn't work. I have also tried some other variations. I think I understand the ADO control its just the syntax I'm having a hard tiem with.
Thanks again,
Greg
Johnny101
February 2nd, 2000, 10:57 AM
The code from my second post was written to work with an ADO control, so yes you need this. It's a good thing to hide those controls at runtime, it makes things easier for the user to have objects they are familiar with.
I generally use the regular ComboBox just because I can get it to do just about anything I need it to, so I haven't used the DataCombo control (that I assume is also new with VB6).
To get the first record to show on start up, place this line as the last line of code in the form load:
cboDrvName.Listindex = 0
Setting the listindex property fires the click event just as if the user had chosen the first record in the list.
Once the data shows up, and you delete the record you want, be sure to refresh the recordset, so that name doesn't show up any more.
Good luck,
John
John Pirkey
MCSD
www.ShallowWaterSystems.com
d.paulson
February 2nd, 2000, 12:34 PM
If you have the ado control on the form, you can
also use a datacombo control that is bound to the datasource. your text boxes can also be bound to the datasource and the appropriate data field that you want to show. if text boxes are bound code is not required to display this info all though code is required to find the record that is picked from the datacombo such as
Private Sub dcInventory_Change()
Dim strSQL As String
adoInventory.Recordset.Find "ItemID = " & dcInventory.BoundText, , , adBookmarkFirst
End Sub
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.