How to add the content of a variable to a ComboBox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • letobale1982
    New Member
    • Jan 2008
    • 4

    How to add the content of a variable to a ComboBox

    Hello everybody,

    I would like to know how do I add the content of a variable to a combobox.

    I have the following code

    For i = 1 to 10
    ComboBox1.AddIt em Data, i
    Next i

    Where I declared previously:
    Public Data As String
    Public i As Integer

    When I run my code mi combo box just shows empty.

    Hope someone can help me out.
  • QVeen72
    Recognized Expert Top Contributor
    • Oct 2006
    • 1445

    #2
    Hi,

    You need to Initialize DATA variable somewhere (eg.. FormLoad)
    are you doing that..?

    Data = "My Data"

    Regards
    Veena

    Comment

    • letobale1982
      New Member
      • Jan 2008
      • 4

      #3
      Yes, I am using the variable Data to store the content of a .txt file that is read first. Code below.

      Public Data As String
      Public i As Integer

      Sub ImportRange()
      Dim ImpRng As Range
      Dim FileName As String
      Dim r As Long
      Dim c As Integer

      Dim Char As String * 1
      Set ImpRng = ActiveCell
      On Error Resume Next
      FileName = "C:\textfile.tx t"
      Open FileName For Input As #1
      If Err <> 0 Then
      MsgBox "Not found: " & FileName, vbCritical, "ERROR"
      Exit Sub
      End If
      r = 0
      c = 0
      txt = ""
      Do Until EOF(1)
      Line Input #1, Data 'Here is where I store the content of the .txt file in Data

      Comment

      • tropix100
        New Member
        • Nov 2008
        • 12

        #4
        It appears you want to add 2 lots of information.
        One being the variable Data, and the other being the variable I.

        Combo1.additem Data & I

        Comment

        • QVeen72
          Recognized Expert Top Contributor
          • Oct 2006
          • 1445

          #5
          Hi,

          May be your ComboBox is populated.. Click on the
          "DROP DOWN ARROW" and check...

          Regards
          Veena

          Comment

          • tropix100
            New Member
            • Nov 2008
            • 12

            #6
            I dont understand why you would want to add a text file to a combobox?
            A combobox is for listing items, not file contents.
            You may be better using a listbox.

            Comment

            • smartchap
              New Member
              • Dec 2007
              • 236

              #7
              Dear letobale
              Your code is okay. It seems it is a part of some other program. Please add following lines to your code and run it.

              Me.Combo1.AddIt em Data
              Loop
              Me.Combo1.ListI ndex = 0 'so that filling of combo box is displayed.
              End Sub

              Private Sub Command1_Click( )
              ImportRange
              End Sub

              Private Sub Form_Load()
              Me.Combo1.Clear
              End Sub

              If it works as per your expectations please acknowledge here.

              Comment

              • letobale1982
                New Member
                • Jan 2008
                • 4

                #8
                Hey guys,

                I finally got it working the last part of the code finishes with:

                EplySel.ComboBo x1.AddItem Data
                Loop
                EplySel.Show
                Close #1
                End Sub

                Thanks you all.

                Comment

                Working...