|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
I have aCombo Box on the form in which i require values from "0001" to "9999" to be displayed as a drop down list.
Thus, i created a table called "Branch" in SQL which has just 1 column called "cCode" the code of which is as follows: create table Branch ( cCode char(5) null ) i imported data into this table i.e values from "0001" to "9999". now this table has values from 0001 to 9999 In a form in Visual Basic I have a Combo Box. I want that whenever the form is activated the combo box should pick up values from the SQL table. Thus in Forma Activation i write the following code: Set rsBranchCode = Nothing rsBranchCode.Open ("select * from Branch"), conBudget, adOpenDynamic, adLockBatchOptimistic cmbBranchCode.Text = rsBranchCode.DataMember rsBranchCode.Close On form activation the combo box has no values in the drop down list Please help me out !!! |
|
#2
|
|||
|
|||
|
you have to loop through the resultset and add the values to the combo box.
Something like: Code:
rsBranchCode.MoveFirst
while not EOF(rsBranchCode) {
cmbBranchCode.AddItem rsBranchCode!cCode
rsBranchCode.MoveNext
wend
|
|
#3
|
|||
|
|||
|
combo box and database table
I am new to VB. I have tried to create a program to work with a database.
I am using string variables to contain the names of various objects. I am giving you the portion of the code which works with a combo box and a Table. I hope this is similar to your problem. Try to adot this code in your program The VB program (VB 5) uses a MS Access database. The database is referenced in the program by a string variable 'dbFile'. The DB file contains a Clients table referenced by the string variable 'ClientsTbl'. The ClientsTable has afield named 'ClientName'. The values in this field are read into the combo box. A combo box named 'cbxClientnames' is on the VB Form. The values are read into this combo box from ClientsTbl as follows: Set dbs = OpenDatabase(dbFile) Set rstClients = dbs.OpenRecordset(ClientsTbl, dbOpenSnapshot) ... ... ... rstClients.MoveFirst Do cbxClientNames.AddItem (rstClients.Fields("ClientName")) rstClients.MoveNext If rstClients.EOF Then Exit Do End If Loop rstClients.Close ... ... ... This code works fine in my program. See if you can use a similar procedure in your program. Actually, I have made provision to add new names to the combo box during run time and the table gets updated on the fly! I have not included the code here. In fact, I have another combo box where I read values from a table. The values from this cmbo box can be selected and transferred to a list on the form. When a value is selected and transferred to the list, it is removed from the combo box. If a selected value is deleted from the list, it is added back to the combo box! Effectively, this means that a value can be chosen only once. The combo box can receive new values during run time and the corresponding table gets automatically updated. -Anant Navale |
|
#4
|
|||
|
|||
|
Quote:
Try This: replace the line in your code cmbBranchCode.Text = rsBranch..... with the following: do until rsBranchCode.eof cmdBranchCode.addnew rsBranchCode.fields(0) rsBranchCode.movenext loop |
![]() |
| Viewing: ASP Free Forums > Programming > Visual Basic Programming > Adding values from a table in SQL into a Combo Box in Visual Basic on Form Activation |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|