Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

List field names in a Combobox at run time 1

Status
Not open for further replies.

giyer555

Programmer
Sep 8, 2003
75
IN
Hi!!! friends

Need ur help for the following..
How do I list field names from a table into a combo box list at run time?
The combobox style is Dropdownlist, datasource = dataenvironment1 & datamember = cmdfieldlist.

Also, pls let me know which event is fired when a ComboBox list item is selected and how to right align text in a maskeditbox.

Thanks
 
Get an ado recordset, and then

dim fld as Field
for each fld in rs.Fields

combo.additem fld.name

next fld

That should be a good pointer for you...

mmilan
 
Thanx mmilan

U got it right & with the ado recordset i was able to get the field names as desired in the cbo list

By using the dataenvironment command it gives an error
Object doesn't support this property or method.

Also, pls let me know which event is fired when a ComboBox list item is selected and how to right align text in a maskeditbox.

Thanx & regards,
 
Generally speaking, it's the CLICK event that fires on a combobox list selection. It also fires if you select an item from code (ie. combo1.listindex = 1).

I'm no expert with MaskedEditControls, but I don't think they support text allignment themselves. You could do it by intercepting the Keypress or Change events and inserting whitespace at the start of the string, but that's hardly very elegant...

mmilan
 
Thanx Mmilan the click event works perfectly when the user selects an item from the list.

Also, if u could tell me how to list a field rows into a combo box list at run time using ado. if i use
cbobranch.additem rs.fields(0) only a single row is added to the list.
I need to list all the rows of field(0)

The combobox style is Dropdownlist.

Pls guide

Thanx

 
The field object is basically there to describe a field in the database. Most of the object is concerned with things that never vary from one row to the next (Datatype, Size etc.), but the Value property only relates to the value of the field on the current record. Therefore, what you need to do is loop through the available records.

Try this: (I'm assuming you will have Rs defined as an open ADO recordset by the time you get to this stage in your code...)

if not(rs.eof or rs.bof) then

rs.MoveFirst
do until rs.eof
cboBranch.AddItem cstr(rs.fields(0).value)
loop

endif

I presume the recordset is set to look at a "Branches" table... If this is not the case, consider writing a function to do that. If not, and you really have to use the "Whatever" table (Be that employees, productivity - whatever...), have a look at Select Distinct instead...




 
Thanx Mmilan

Using ADO recordset i tried to loop the rs but am receiving an error

invalid procedure call or argument

Running the form in debug mode i found that while looping the rs field value is showing the first record only

I am using the following code

Set cn = New ADODB.Connection
cn.Open "dsn=saibaba;uid=sa;pwd="
Set rs = New ADODB.Recordset
rs.Open "select br_code, br_name from branch order by br_code", cn, adOpenKeyset

if not(rs.eof or rs.bof) then
rs.MoveFirst
do until rs.eof
cboBranch.AddItem cstr(rs.fields(0).value)
loop
endif

Pls let me know whats causing the error

Thanx

 
My apologies - I missed a MoveNext call when I wrote the loop. Try this:

Code:
Set cn = New ADODB.Connection
cn.Open "dsn=saibaba;uid=sa;pwd="
Set rs = New ADODB.Recordset
rs.Open "select br_code, br_name from branch order by br_code", cn, adOpenKeyset

if not(rs.eof or rs.bof) then 
    rs.MoveFirst
    do until rs.eof
        cboBranch.AddItem cstr(rs.fields(0).value)
        rs.movenext
    loop
endif

Something else you might want to consider is how you are opening the recordset. At the moment, you're using Keyset, but Snapshot or ForwardOnly might be better - they open the table as "read only", and so don't leave locks in place...


mmilan
 
Something else you ought to know is that it's not a good idea to refer to fields by their field number. The first muppet who comes along and changes your table design will mess up the entire program...

Try replacing the line:

cboBranch.AddItem cstr(rs.fields(0).value)

with

cboBranch.AddItem cstr(rs.fields("br_name").value)

or even (and this is the way I would usually do it...)

cboBranch.AddItem cstr(rs.fields!br_name)

mmilan
 
Thanx Mmilan

Its good to get expert comments from u. I will surely make changes as instructed & will let u know.

Once again thank u in a big way.

I have got another Q related to data report grouping.

I retrieve the data using the following query in a commands click event

Ssql = "select a.emp_code, b.emp_name, a.br_code, b.account_no, b.bank_code, c.bank_name, a.total_sal from emp_earnded a, emp_mast b, bank_mast c where a.emp_code = b.emp_code and b.bank_code = c.bank_code and b.pay_mode = 0 and a.Month = " & cbomonth.Text & " and a.Year = " & cboyear.Text & " and a.posted = 1 order by b.bank_code, a.br_code, a.emp_code"
DataEnvironment1.Commands(11).CommandText = ssql
dtrbankstmt.Sections("pqr").Controls("lblmonth").Caption = strmonth & " " & cboyear.Text
dtrbankstmt.Show

I need to print the report as per individual bank_code in separate pages

The report should be as follows

Bank_code 555 Bank_name xyz

Header
Emp_code, emp_name, br_code, account_no, total_sal

Detail
Emp_code, emp_name, br_code, account_no, total_sal

Report Footer
No of record 50 Total Sal 555555
 
Unfortunately, I don't know anything at all about datareports - I've never used them.

I would suggest starting a fresh thread on this topic, but before you do, I'd havea search through the forums history to see if you can find the answer there...

mmilan
 
Thanx mmilan for the help extended

bye & all the best
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top