使用vba和ado填充组合框 [英] populate combobox using vba and ado

查看:60
本文介绍了使用vba和ado填充组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友

我问的是一个非常愚蠢的问题,但我没有找到任何解决方案

这个..

我选择了一个记录集,并希望填充一个组合框,其中id

将是可邀请的内容。

我得到记录集和记录的但是我不能

填充组合框。

我已经尝试了每个x的所有函数起始形式

....

和while ... wend and do ....循环

我在这里提供我的代码


Private Sub cmb_pc_GotFocus()


Dim rs As ADODB.Recordset

Dim conn As ADODB.Connection

Dim sqlQr5 As String''我们的SQL查询


设置conn = CurrentProject.Connection''访问连接

设置rs = CreateObject(" ADODB.Recordset")

sqlQr5 =" SELECT dbo_ProfitCenter.Profit_Center_Code,

dbo_ProfitCenter.Profit_Center"

sqlQr5 = sqlQr5& FROM dbo_ProfitCenter

sqlQr5 = sqlQr5& " ORDER BY dbo_ProfitCenter.Profit_Center"

rs.CursorType = adOpenKeyset

rs.LockType = adLockOptimistic

rs.Open sqlQr5,conn

Value = rs.RecordCount

如果不是rs.EOF那么



*#* #*#*#*#*#*#*#*#*#*我想要这个地方的代码

rs.MoveNext

循环直到rs.EOF

我的combox名称是cmb_pc

我试过additem但它在vba中不起作用

任何帮助都会很棒

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.Connection
Dim sqlQr5 As String ''Our SQL query

Set conn = CurrentProject.Connection ''Access connection
Set rs = CreateObject("ADODB.Recordset")
sqlQr5 = " SELECT dbo_ProfitCenter.Profit_Center_Code,
dbo_ProfitCenter.Profit_Center "
sqlQr5 = sqlQr5 & "FROM dbo_ProfitCenter "
sqlQr5 = sqlQr5 & "ORDER BY dbo_ProfitCenter.Profit_Center "

rs.CursorType = adOpenKeyset
rs.LockType = adLockOptimistic
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

推荐答案

您可以使用GetString()和RowSource。我喜欢这个,因为在代码中修改RowSource(一个字符串)很容易

;这是我如何做b
$ b的一个例子。对于大多数情况来说,这比需要的要复杂得多,但是你可以看到我做了什么虽然它没有显示创建它后修改

RowSource字符串。 />

Private Sub Form_Load()

Dim TSQL As String

Dim ID As Long

TSQL =" ; SELECT fldSchoolID,fldSchoolName FROM dbo.tblSchools ORDER

BY fldSchoolName"

ID = LoginID()

如果ID<> 0然后

TSQL =替换(TSQL,ORDER BY,WHERE fldSchoolID ="& ID&

" ORDER BY)
lstSchools.RowSource =

CurrentProject.Connection.Execute(TSQL).GetString(,,, ")

lstPrograms.RowSource = CurrentProject.Connection.Execute(" SELECT

fldProgramID,fldProgramName FROM dbo.tblPrograms ORDER BY

fldProgramName" ;)。GetString(,,",",",")

''剪辑

End Sub

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.RowSource =
CurrentProject.Connection.Execute(TSQL).GetString( , , ",", ",")
lstPrograms.RowSource = CurrentProject.Connection.Execute("SELECT
fldProgramID, fldProgramName FROM dbo.tblPrograms ORDER BY
fldProgramName").GetString(, , ",", ",")
'' snips
End Sub


我的问题没有解决!

My problem is not solved !


如果我有了正确的想法,你有一个带有代码的表Profitcenter

字段和名称字段,你想用它们填充一个组合框。

在循环表之前将rowsource设置为空,然后

从表中的每条记录中获取字段对。


combo。 rowsource =""

rs.movefirst

而不是rs.eof

combo.rowsource = combo.rowsource& rs.fields(1stfield)&英寸;" &

rs.fields(" 2ndfield")& " ;;"

rs.movenext

wend


这应该用你自己的字段填充你的rowsource />
已将numberofcolumns设置为2并将宽度设置为0和某些内容和

将绑定列设置为1


希望有帮助吗?<
***通过Developersdex发送 http://www.developersdex.com ***
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("1stfield") & ";" &
rs.fields("2ndfield") & ";"
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 ***


这篇关于使用vba和ado填充组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆