Visual Basic Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsProgrammingVisual Basic Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old September 17th, 2003, 05:04 AM
Preethyk Preethyk is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 1 Preethyk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question Adding values from a table in SQL into a Combo Box in Visual Basic on Form Activation

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 !!!

Reply With Quote
  #2  
Old September 21st, 2003, 04:02 PM
Onslaught Onslaught is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 34 Onslaught User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 m 44 sec
Reputation Power: 6
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

Reply With Quote
  #3  
Old October 4th, 2003, 10:23 AM
asnavale asnavale is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Location: India
Posts: 5 asnavale User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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

Reply With Quote
  #4  
Old October 18th, 2004, 07:50 AM
KenVandergriff KenVandergriff is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 23 KenVandergriff User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 48 m 8 sec
Reputation Power: 0
Smile

Quote:
Originally Posted by Preethyk
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 !!!

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

Reply With Quote
Reply

Viewing: ASP Free ForumsProgrammingVisual Basic Programming > Adding values from a table in SQL into a Combo Box in Visual Basic on Form Activation


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway