473,903 Members | 4,843 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

populate combobox using vba and ado

Hello Friends
I am asking a very silly question but i dont find any solution fo
this..
I am selectiong a recordset and want to populate a combobox where id
would be inviseble and the content would.
I am getting the recordset and the no of record but then i am unble to
populate the combobox.
I have already tried all the function starting form
for each x in ....
and while...wend and do....loop
I am providing my code here

Private Sub cmb_pc_GotFocus ()

Dim rs As ADODB.Recordset
Dim conn As ADODB.Connectio n
Dim sqlQr5 As String 'Our SQL query

Set conn = CurrentProject. Connection 'Access connection
Set rs = CreateObject("A DODB.Recordset" )
sqlQr5 = " SELECT dbo_ProfitCente r.Profit_Center _Code,
dbo_ProfitCente r.Profit_Center "
sqlQr5 = sqlQr5 & "FROM dbo_ProfitCente r "
sqlQr5 = sqlQr5 & "ORDER BY dbo_ProfitCente r.Profit_Center "

rs.CursorType = adOpenKeyset
rs.LockType = adLockOptimisti c
rs.Open sqlQr5, conn
Value = rs.RecordCount
If Not rs.EOF Then
Do
*#*#*#*#*#*#*#* #*#*#*i want code for this place
rs.MoveNext
Loop Until rs.EOF
my combox name is "cmb_pc"
I tried additem but it dont works in vba
Any help would be great

Mar 22 '06 #1
16 10543
You could use GetString() and RowSource. I like this because it's easy
to modify the RowSource (a string) in code; This is an example of how I
have done this. It's more complicated than needs be for most cases but
you can see what I did although it does not show modifying the
RowSource string after creating it.

Private Sub Form_Load()
Dim TSQL As String
Dim ID As Long
TSQL = "SELECT fldSchoolID, fldSchoolName FROM dbo.tblSchools ORDER
BY fldSchoolName"
ID = LoginID()
If ID <> 0 Then
TSQL = Replace(TSQL, "ORDER BY", "WHERE fldSchoolID = " & ID &
" ORDER BY")
End If
lstSchools.RowS ource =
CurrentProject. Connection.Exec ute(TSQL).GetSt ring(, , ",", ",")
lstPrograms.Row Source = CurrentProject. Connection.Exec ute("SELECT
fldProgramID, fldProgramName FROM dbo.tblPrograms ORDER BY
fldProgramName" ).GetString(, , ",", ",")
' snips
End Sub

Mar 22 '06 #2
My problem is not solved !

Mar 22 '06 #3
If I have got the idea right, you have a table Profitcenter with a code
field and name field and you want to populate a combo box with them.
Before you loop through the table set the rowsource to nothing and then
get the pairs of fields from each record in the table.

combo.rowsource = ""
rs.movefirst
while not rs.eof
combo.rowsource = combo.rowsource & rs.fields("1stf ield") & ";" &
rs.fields("2ndf ield") & ";"
rs.movenext
wend

This should populate your rowsource with pairs of fields assuming you
have set numberofcolumns to 2 and set the widths to 0 and something and
set bound column to 1

Hope that helps?



*** Sent via Developersdex http://www.developersdex.com ***
Mar 22 '06 #4
"dev.amit" <ag************ ***@gmail.com> wrote
My problem is not solved !


At best, with all the information laid out in detail, remote debugging is
not easy. With uninformative responses such as this, you make it either much
more difficult, or impossible, for those who are trying to assist you.

Larry Linson
Microsoft Access MVP
Mar 23 '06 #5
"Larry Linson" <bo*****@localh ost.not> wrote in
news:qvmUf.3651 $4N1.2823@trndd c06:
"dev.amit" <ag************ ***@gmail.com> wrote
My problem is not solved !


At best, with all the information laid out in detail, remote
debugging is not easy. With uninformative responses such as this,
you make it either much more difficult, or impossible, for those
who are trying to assist you.


My question is:

Why would anyone use ADO to populate a combobox? I've never needed
to do that, so can't quite see why I should attempt to wade through
the code to figure out why it's not working. Use SQL to populate
your combobox and be done with it.

In any event, it seemed like the main problem had nothing to do with
the rowsource, but with a failure to understand the basic of Access
combo boxes, where you bind a column to a field and hide that value
by setting that column's width to 0.

--
David W. Fenton http://www.dfenton.com/
usenet at dfenton dot com http://www.dfenton.com/DFA/
Mar 23 '06 #6
Sorry Grath After ur help also still i am no getting the sintax right
it is not working properly
if u can make it more specific i would be happy
I am able to select the rows to record sheet
I am able to populate if i am taking single value at a time but unable
to populate both id and its content
Please also specify how i would get the id if i choose one form the
combobox
Take Care
Thanks Dear
Garth Harvey wrote:
If I have got the idea right, you have a table Profitcenter with a code
field and name field and you want to populate a combo box with them.
Before you loop through the table set the rowsource to nothing and then
get the pairs of fields from each record in the table.

combo.rowsource = ""
rs.movefirst
while not rs.eof
combo.rowsource = combo.rowsource & rs.fields("1stf ield") & ";" &
rs.fields("2ndf ield") & ";"
rs.movenext
wend

This should populate your rowsource with pairs of fields assuming you
have set numberofcolumns to 2 and set the widths to 0 and something and
set bound column to 1

Hope that helps?



*** Sent via Developersdex http://www.developersdex.com ***


Mar 23 '06 #7
Dear Mr. David
You might be right why anyone take so much pain to populate a combo by
using code.
We can do it very easily using access queary.
But the problem i am having is the performance
I am dealing with huge database from sqlserver and oracle
simultaneously
for the better performance shake i need it
Love
Amit

Mar 23 '06 #8
It does seem that you are not familiar with the properties of a combo
box. the columncount must be 2, the columnwidths should look like
0cm;2cm (or inches) and the boundcolumn should be 1. The rowsourcetype
needs to be valuelist.
It has already been pointed out that this is a big hammer for a small
nail.
Why not set the rowsource type to table/query and put the sql in the
rowsource.
Garth
*** Sent via Developersdex http://www.developersdex.com ***
Mar 23 '06 #9
Thanks Dear
But i want to run it from the vba
and i am not having problem in the combox properties
i am having problem in the vba syntex
after having the recordset and then putting it into the combobox.
Take cate
love
Amit

Mar 23 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
12871
by: godber | last post by:
I need to populate text boxes for instance with employee information using their unique employee works number selected from a combo box. Can anyone help, I am told thru visual basic this can be achieved.
4
14654
by: Marc Jennings | last post by:
Hi there, I'm new to C#, and I have a small problem. I have created a new DataSet using the code below, and I would like to be able to populat a drop-down list with the results of the "name" field. Can anyone help me, please? *************************************************
4
3014
by: Mike L | last post by:
I'm open for any suggestions on how to better program this. I want the user to select a license from a combo box, cboPrivilege and then the user will click the add button, then a record will be added to the data grid. My current code is coming up with errors, dt and drv are unknown in the else part of the if statement. if (this.dgPrivileges.DataSource == null) {
16
1791
by: Mike Fellows | last post by:
when i load my windows form i populate a combobox i use the code below Dim conn As New System.Data.SqlClient.SqlConnection(strConn) Dim sql As String = "AllLenders" Dim da As New System.Data.SqlClient.SqlDataAdapter(sql, conn) Dim ds As New DataSet() da.Fill(ds) ComboBox1.DataSource = ds.Tables(0) ComboBox1.DisplayMember = "Lender" da.Dispose()
0
1619
by: masterej | last post by:
I'm trying to populate a ComboBox (dropdown style) with strings of text from an ArrayList. The ComboBox is actually being populated with the correct items, however, they appear only as blank entries on the drop down list. When I click on one of the blank entries, the editable region displays the correct text, and subsequent functions relying on this string execute properly. My question is why can't I see any of the values that have been...
5
5927
by: Rich | last post by:
Hello, I have a search application to search data in tables in a database (3 sql server tables). I populate 2 comboboxes with with data from each table. One combobox will contain unique CompanyID's. The second combobox will contain unique memberID's. Each of the tables that I have to search contain a CompanyID and a memberID field, and these fields are not unique in the respective tables. Like CompanyID, MemberID
4
28236
by: polaris431 | last post by:
All the examples I've seen showing how to populate a combobox using the DataSource property and an ArrayList show the ArrayList object containing objects with at least two properties. I want to create an ArrayList that contains only rows of string data and want to assign this to the DataSource but I can't figure out what to set the ValueMember or DisplayMember to. Example ArrayList myData = new ArrayList(); myData.Add("abc");
1
6009
by: freekedoutfish | last post by:
Hi. New member here Im sat at work, pounding my head off the desk because this tiny bit of simple code refuses to work. The sub is intended to pull data from the "companyname" column in the "cmSelectAllCompanyNames" table, and put all the company names it finds into the "cbFilterCompanyName" combo box. Now the confusing aspect is this: It works!!!
0
2783
TonFrere
by: TonFrere | last post by:
Hello, I'm building a windows form application in Visual C# using VS 2005. On my form I need to populate a combobox with Invoices# linked to the current reccord's Order# value. This means that: - The combobox should repopulate if the user changes the value of Order#. - The combobox should repopulate if the user moves through records (using the binding navigator. My problem is that whenever I repopulate the combobox, it's value is reset to...
0
9847
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11283
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10875
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10986
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10501
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9685
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8049
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7206
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5894
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.