Solved

adding to combo

Posted on 2002-03-25
4
167 Views
Last Modified: 2010-05-02
i'm adding fields from a database into about 10 combos. Most of the code is the same so i wanted to have one procedure and pass the name of the combo, the table and the field to add.

This is what i have at the moment..

Private Sub fillCboVoltage()

    CboVoltage.Clear
    CboVoltage.AddItem "All"
    With rs
        .openRecord ("select distinct Electrical.[Voltage] from electrical"), adOpenForwardOnly, adLockReadOnly
        .movefirst
        Do While Not .eof
            If .Voltage > "" Then
                CboVoltage.AddItem .Voltage
            End If
            .movenext
        Loop
    End With
   
End Sub
0
Comment
Question by:jlymn
  • 2
  • 2
4 Comments
 
LVL 43

Accepted Solution

by:
TimCottee earned 50 total points
ID: 6893719
Private Sub FillIt(ByRef CBox As ComboBox,ByVal Table As String, ByVal Field As String)
   CBox.Clear
   CBox.AddItem "All"
   With rs
       .openRecord ("select distinct Electrical.[" & Field & "] from " & Table ), adOpenForwardOnly, adLockReadOnly
       .movefirst
       Do While Not .eof
           If .Fields(Field).Value > "" Then
               CBox.AddItem .Fields(Field).Value
           End If
           .movenext
       Loop
   End With
End Sub

Call like:

FillIt CboVoltage,"Electrical","Voltage"
0
 
LVL 3

Expert Comment

by:Bahnass
ID: 6893724
Private Sub fillCboVoltage(sTable as string,sField as string)
   dim rs as adodb.recordset
   set rs as new adodb.recordset
   CboVoltage.Clear
   CboVoltage.AddItem "All"
   With rs
       .openRecord ("select distinct " & sTable & "."  &sField &" from electrical"), adOpenForwardOnly, adLockReadOnly
       .movefirst
       Do While Not .eof
           If .Voltage > "" Then
               CboVoltage.AddItem .Voltage
           End If
           .movenext
       Loop
   End With
   rs.close
   set rs=nothing
End Sub


then call it like

fillCboVoltage("Electrical","[Voltage]")
0
 
LVL 3

Expert Comment

by:Bahnass
ID: 6893729
Tim is right only correct this line

      .openRecord ("select distinct " & sTable & "."  &sField &" from " & sTable &")", adOpenForwardOnly,
adLockReadOnly

Also closing rs is Ur choice
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 6893733
Actually Bahnass, putting the closing bracket as ")" will cause a syntax error because the intial ( is surrounding the entire statement. I nearly got caught out on that one.

You haven't picked up your free points here http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=visualbasic&qid=20280267 yet either!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now