Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

adding to combo

Posted on 2002-03-25
4
Medium Priority
?
175 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 200 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

876 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