Solved

adding to combo

Posted on 2002-03-25
4
166 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
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…

743 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

16 Experts available now in Live!

Get 1:1 Help Now