Solved

adding to combo

Posted on 2002-03-25
4
169 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

813 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

10 Experts available now in Live!

Get 1:1 Help Now