Solved

adding to combo

Posted on 2002-03-25
4
172 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…
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…

710 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